diff --git a/src/handlers/implants.ts b/src/handlers/implants.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..182ac20c7fccb15cf90c6474d2fcd7c8198a5d90 100644 --- a/src/handlers/implants.ts +++ b/src/handlers/implants.ts @@ -0,0 +1,107 @@ +import { type Request, type Response } from 'express'; +import { implants } from '@/db/schema'; +import { db } from "@/db"; +import { eq } from 'drizzle-orm'; +import { implantSchema } from '@/validators/implantsValidator'; + +export default class implant{ + static implantRequestValidation (req: Request){ + const validation = implantSchema.safeParse(req.body); + return validation.success; + } + + static implantRequestUnzip (req: Request){ + return implantSchema.safeParse(req.body); + } + + static async getImplant (id: number){ + const searched = await db.select().from(implants).where(eq(implants.id, id)).limit(1); + return searched[0] || null; + } + + static async implantRead(id: number, req: Request, res: Response){ + if (!this.implantRequestValidation(req)) + { + return res.status(400).json({ error: "Invalid Request"}); + } + + const holder = await this.getImplant(id); + if (!holder) + { + return res.status(404).json({ error: "Not Found"}); + } + + return res.status(200).json(holder); + + } + + static async implantUpdate(id: number, req: Request, res: Response){ + if (!this.implantRequestValidation(req)) + { + return res.status(400).json({ error: "Invalid Request"}); + } + + const updated_implant = await this.getImplant(id); + if (!updated_implant) + { + return res.status(404).json({ error: "Not Found"}); + } + + const updates: Partial<typeof implants.$inferInsert> = { + name: req.body.name || updated_implant.name, + price: req.body.price || updated_implant.price, + cyberCost: req.body.cyberCost || updated_implant.cyberCost, + bodyPart: req.body.bodyPart || updated_implant.bodyPart, + }; + + try{ + await db.update(implants).set(updates).where(eq(implants.id, id)); + } catch (error) { + return res.status (500).json({ error: "Update User Error"}) + } + + return res.status(200).json({ message: "OK"}); + } + + static async implantDelete(id: number, req: Request, res: Response){ + if (!this.implantRequestValidation(req)) + { + return res.status(400).json("Invalid Request"); + } + + if (!this.getImplant(id)) + { + return res.status(404).json("Not Found"); + } + + try{ + await db.delete(implants).where(eq(implants.id, id)); + return res.status(200).json({ message: "Implant removed successfully"}); + } catch (error) { + return res.status(500).json({ error: "Implant Removal Error"}); + } + } + + static async implantCreate(req: Request, res: Response){ + const implant_package = this.implantRequestUnzip(req); + if (!implant_package.success) + { + return res.status(400).json("Invalid Request"); + } + + const { name, price, cyberCost, bodyPart } = implant_package.data; + + try{ + const brand_new = await db.insert(implants).values({ + name, + price, + cyberCost, + bodyPart, + }).returning(); + + return res.status(200).json({ message: "Implant Creation Successfull", implant: brand_new}); + } catch (error) { + return res.status(500).json({ error: "User Creation Error"}); + } + } +} diff --git a/src/handlers/user.ts b/src/handlers/user.ts index 1b8651e5e5e777e7bda6c7d68db1dc325381ce3b..9479ade6f759a2af28f0c61a3ad8e3265035a9fc 100644 --- a/src/handlers/user.ts +++ b/src/handlers/user.ts @@ -3,7 +3,7 @@ import { userSchema } from "@/validators/userValidator"; import { db } from "@/db"; import { eq } from 'drizzle-orm'; import { usersTable } from "@/db/schema"; -import bcrypt from "bcryptjs"; +import bcrypt from "bcrypt"; import { z } from 'zod'; export default class User{ @@ -19,7 +19,7 @@ export default class User{ static async getUser(id: number){ //retorna o usuário presente no db var user = await db.select().from(usersTable).where(eq(usersTable.id, id)).limit(1); - return user; + return user[0] || null; } @@ -43,14 +43,14 @@ export default class User{ const user_updated = await this.getUser(id); const updates: Partial<typeof usersTable.$inferInsert> = { - name: req.body.name || user_updated.name; - password: hashedPassword || user_updated.password; - email: req.body.email || user_updated.email; - birthday: req.body.birthday || user_updated.birthday; - cpf: req.body.cpf || user_updated.cpf; - money: req.body.money || user_updated.money; - cyberpsychosis: req.body.cyberpsychosis || user_updated.cyberpsychosis; - cyberLimit: req.body.cyberLimit || user_updated.cyberLimit; + name: req.body.name || user_updated.name, + password: hashedPassword || user_updated.password, + email: req.body.email || user_updated.email, + birthday: req.body.birthday || user_updated.birthday, + cpf: req.body.cpf || user_updated.cpf, + money: req.body.money || user_updated.money, + cyberpsychosis: req.body.cyberpsychosis || user_updated.cyberpsychosis, + cyberLimit: req.body.cyberLimit || user_updated.cyberLimit, }; await db.update(usersTable).set(updates).where(eq(usersTable.id, id)); diff --git a/src/validators/implantsValidator.ts b/src/validators/implantsValidator.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..70551fca9ee9201ecb5bd256d985257ee9ffec82 100644 --- a/src/validators/implantsValidator.ts +++ b/src/validators/implantsValidator.ts @@ -0,0 +1,9 @@ +import { z } from 'zod'; + +export const implantSchema = z.object({ + id: z.number().int().optional(), + name: z.string().min(1, "Name cannot be empty"), + price: z.number().nonnegative(), + cyberCost: z.number().nonnegative(), + bodyPart: z.string().min(1, "Must be part of something"), +}); \ No newline at end of file