Skip to content

Commit

Permalink
Merge branch 'feat/ComapanyImplementation'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Sep 24, 2024
2 parents 2e980b1 + d152ca7 commit 14a409c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/@types/companyTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Product } from "./productTypes";
import { unitType } from "./unitType";

export interface companyType {
id?: number | undefined;
name: string;
unit: unitType[]
products: Product[]
}
8 changes: 8 additions & 0 deletions src/@types/unitType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { companyType } from "./companyTypes";

export interface unitType {
id?: number;
name: string;
company: companyType;
companyId: number;
}
61 changes: 61 additions & 0 deletions src/controllers/companyController.ts
Original file line number Diff line number Diff line change
@@ -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 <companyType> = 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}`})
}
}
20 changes: 20 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions src/services/companyService.ts
Original file line number Diff line number Diff line change
@@ -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<companyType>
) => {
return prisma.product.update({
where: { id },
data: companyData,
})
}

export const deleteCompany = async (id: number) => {
return prisma.company.delete({
where: { id },
})
}

0 comments on commit 14a409c

Please sign in to comment.