-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/ComapanyImplementation'
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
}) | ||
} |