diff --git a/api/package-lock.json b/api/package-lock.json index 6890d62..b9c407a 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -18,6 +18,7 @@ }, "devDependencies": { "@types/express": "^5.0.3", + "@types/lodash": "^4.17.20", "drizzle-kit": "^0.31.4", "tsx": "^4.20.3", "typescript": "^5.8.3" @@ -944,6 +945,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/lodash": { + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-H3MHACvFUEiujabxhaI/ImO6gUrd8oOurg7LQtS7mbwIXA/cUqWrvBsaeJ23aZEPk1TAYkurjfMbSELfoCXlGA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/mime": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", diff --git a/api/package.json b/api/package.json index 57bdf1a..d7074ce 100644 --- a/api/package.json +++ b/api/package.json @@ -24,6 +24,7 @@ }, "devDependencies": { "@types/express": "^5.0.3", + "@types/lodash": "^4.17.20", "drizzle-kit": "^0.31.4", "tsx": "^4.20.3", "typescript": "^5.8.3" diff --git a/api/src/db/productsSchema.ts b/api/src/db/productsSchema.ts index 96b1750..537fe0d 100644 --- a/api/src/db/productsSchema.ts +++ b/api/src/db/productsSchema.ts @@ -1,4 +1,5 @@ import { int, mysqlTable, text, bigint, varchar, double } from 'drizzle-orm/mysql-core'; +import { createInsertSchema , createSelectSchema, createUpdateSchema } from 'drizzle-zod'; export const productsTable = mysqlTable('products', { id: int().autoincrement().primaryKey(), @@ -6,4 +7,15 @@ export const productsTable = mysqlTable('products', { description: text(), image: varchar({ length: 255 }), price: double().notNull(), -}); \ No newline at end of file +}); + + +// const createProductSchema = z.object({ +// name: z.string(), +// price: z.number({ message: "not a number you idiot."}), +// }); + +export const createProductSchema = createInsertSchema(productsTable).omit({ + id: true, +}); + diff --git a/api/src/middlewares/validationMiddleware.ts b/api/src/middlewares/validationMiddleware.ts index fe5ebbe..a9c3e62 100644 --- a/api/src/middlewares/validationMiddleware.ts +++ b/api/src/middlewares/validationMiddleware.ts @@ -1,11 +1,13 @@ import { Request, Response, NextFunction } from 'express'; import { z, ZodError, ZodTypeAny } from 'zod'; import { ZodObject } from 'zod/v4'; +import _ from 'lodash'; export function validateData(schema: ZodObject) { return (req: Request, res: Response, next: NextFunction) => { try { schema.parse(req.body); + req.cleanBody = _.pick(req.body, Object.keys(schema.shape)); next(); } catch (error) { // console.log(error); diff --git a/api/src/routes/products/index.ts b/api/src/routes/products/index.ts index 207dcf1..a614e9d 100644 --- a/api/src/routes/products/index.ts +++ b/api/src/routes/products/index.ts @@ -7,16 +7,9 @@ import { listProducts, } from "./productsController"; import { validateData } from "../../middlewares/validationMiddleware"; import { z, ZodObject, ZodTypeAny } from 'zod/v4'; -import { createInsertSchema , createSelectSchema, createUpdateSchema } from 'drizzle-zod'; -import { productsTable } from "../../db/productsSchema"; - -// const createProductSchema = z.object({ -// name: z.string(), -// price: z.number({ message: "not a number you idiot."}), -// }); +import { productsTable, createProductSchema } from "../../db/productsSchema"; -const createProductSchema = createInsertSchema(productsTable).omit({id: true}); type ProductType = z.infer; const router = Router(); diff --git a/api/src/routes/products/productsController.ts b/api/src/routes/products/productsController.ts index e964a85..31bad19 100644 --- a/api/src/routes/products/productsController.ts +++ b/api/src/routes/products/productsController.ts @@ -1,7 +1,10 @@ import {Request, Response} from 'express'; import {db} from '../../db/index'; import {eq} from 'drizzle-orm'; -import { productsTable } from '../../db/productsSchema'; +import { productsTable, createProductSchema } from "../../db/productsSchema"; +import _ from 'lodash'; + + export async function listProducts(req: Request, res: Response) { try{ @@ -33,9 +36,10 @@ export async function getProductById(req: Request, res: Response) { export async function createProduct(req: Request, res: Response) { try{ + const productId = await db .insert(productsTable) - .values(req.body) + .values(req.cleanBody) .$returningId() //const [product] = await db.insert(productsTable).values(req.body).Returning() //Postgresql can return series of products so we can capture the first one from array[] diff --git a/api/src/type/express/index.d.ts b/api/src/type/express/index.d.ts new file mode 100644 index 0000000..032dcab --- /dev/null +++ b/api/src/type/express/index.d.ts @@ -0,0 +1,11 @@ +// to make the file a module and avoid the TypeScript error +export {}; + +declare global { + namespace Express { + export interface Request { + userId?: Number; + cleanBody?: any + } + } +} \ No newline at end of file