diff --git a/src/@types/companyTypes.ts b/src/@types/companyTypes.ts new file mode 100644 index 0000000..a73aa17 --- /dev/null +++ b/src/@types/companyTypes.ts @@ -0,0 +1,9 @@ +import { Product } from "./productTypes"; +import { unitType } from "./unitType"; + +export interface companyType { + id?: number | undefined; + name: string; + unit: unitType[] + products: Product[] +} \ No newline at end of file diff --git a/src/@types/unitType.ts b/src/@types/unitType.ts new file mode 100644 index 0000000..4a6ea45 --- /dev/null +++ b/src/@types/unitType.ts @@ -0,0 +1,8 @@ +import { companyType } from "./companyTypes"; + +export interface unitType { + id?: number; + name: string; + company: companyType; + companyId: number; +} \ No newline at end of file diff --git a/src/controllers/companyController.ts b/src/controllers/companyController.ts new file mode 100644 index 0000000..540ba5b --- /dev/null +++ b/src/controllers/companyController.ts @@ -0,0 +1,61 @@ +import { companyType } from '@/@types/companyTypes'; +import { Request, Response } from 'express'; +import * as companyService from '../services/companyService' + + +export const createCompany = async (req: Request, res: Response) => { + try { + const companyData: companyType | any = req.body; + + const createdCompany = await companyService.createCompany(companyData) + } catch (error) { + res.status(500).json({ message: (`Erro ao salvar company no banco de dados ${(error as Error).message}`)}) + } +} + +export const getCompany = async (req: Request, res: Response) => { + + try { + const company = await companyService.getCompany() + res.status(200).json(company) + } catch(error) { + res.status(500).json({ message: `Erro ao consultar listagem no banco de dados ${(error as Error).message}`}) + } +} + +export const getCompanyById = async (req: Request, res: Response) => { + + try { + const id = Number.parseInt(req.params.id, 10) + const company = await companyService.getCompanyById(id) + + if(company){ + res.status(200).json(company) + } else { + res.status(404).json({ message: `Company not found`}) + } + } catch (error){ + res.status(500).json({ message: `Erro ao consultar banco de dados ${(error as Error).message}`}) + } +} + +export const updateProduct = async (req: Request, res: Response) => { + + try { + const id = Number.parseInt(req.params.id, 10) + const companyData: Partial = req.body + const updatedCompany = await companyService.updateCompany(id, companyData) + } catch (error) { + res.status(500).json({ message: `Erro no banco de dados ao realizar update ${(error as Error).message}`}) + } +} + +export const deleteCompany = async (req: Request, res: Response) =>{ + + try { + const id = Number.parseInt(req.params.id, 10) + await companyService.deleteCompany(id) + } catch(error) { + res.status(500).json ({ message: `Erro no banco de dados ao deletar company ${(error as Error).message}`}) + } +} \ No newline at end of file diff --git a/src/env/index.ts b/src/env/index.ts new file mode 100644 index 0000000..b09cc31 --- /dev/null +++ b/src/env/index.ts @@ -0,0 +1,20 @@ +import 'dotenv/config' +import { z } from 'zod' + +const envSchema = z.object({ + NODE_ENV: z.enum(['dev', 'test', 'production']).default('dev'), + accessToken: z.string(), + DATABASE_URL: z.string(), + JWT_SECRET: z.string(), + IMGBB_API_KEY: z.string(), + PORT: z.coerce.number().default(3000), +}) + +const _env = envSchema.safeParse(process.env) + +if (_env.success === false) { + console.log('🚨❗ Invalid enviroment variable') + throw new Error('Invalid environment variables') +} + +export const env = _env.data diff --git a/src/services/companyService.ts b/src/services/companyService.ts new file mode 100644 index 0000000..53be722 --- /dev/null +++ b/src/services/companyService.ts @@ -0,0 +1,36 @@ +import {type Prisma, PrismaClient } from "@prisma/client"; +import { companyType } from '@/@types/companyTypes'; + +const prisma = new PrismaClient() + +export const createCompany = async (companyData: Prisma.CompanyCreateInput) => { + return prisma.company.create({ + data: companyData + }) +} + +export const getCompany = async () =>{ + return prisma.company.findMany() +} + +export const getCompanyById = async (id: number) => { + return prisma.company.findUnique({ + where: { id }, + }) +} + +export const updateCompany = async ( + id: number, + companyData: Partial + ) => { + return prisma.product.update({ + where: { id }, + data: companyData, + }) + } + +export const deleteCompany = async (id: number) => { + return prisma.company.delete({ + where: { id }, + }) +} \ No newline at end of file