From 2783cf196289ac7130927247a21a06d059e73085 Mon Sep 17 00:00:00 2001 From: arbezerra Date: Thu, 29 Jun 2023 08:59:51 -0300 Subject: [PATCH] feat: Generate uuid --- controllers/auth.ts | 2 ++ controllers/category.ts | 9 ++++----- controllers/post.ts | 7 +++---- package.json | 2 ++ tsconfig.prod.json | 3 +++ yarn.lock | 10 ++++++++++ 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/controllers/auth.ts b/controllers/auth.ts index 8e0acd3..80d5b4d 100644 --- a/controllers/auth.ts +++ b/controllers/auth.ts @@ -5,6 +5,7 @@ import User from "../models/user"; import jwt from "jsonwebtoken"; import { ValidateMethod } from "../middlewares/validate"; import { z } from "zod"; +import { v4 as uuid4 } from "uuid"; const AuthController = { index: async (req: Request, res: Response) => { @@ -32,6 +33,7 @@ const AuthController = { { ...req.body, password: await hash(password), + id: uuid4(), }, "id" ); diff --git a/controllers/category.ts b/controllers/category.ts index 78f9f29..43c984c 100644 --- a/controllers/category.ts +++ b/controllers/category.ts @@ -1,11 +1,9 @@ -import { NextFunction, Request, Response } from "express"; +import { Request, Response } from "express"; import pool from "../lib/db"; -import { verify } from "../lib/hash"; -import User from "../models/user"; -import jwt from "jsonwebtoken"; import Category from "../models/category"; import { ValidateMethod } from "../middlewares/validate"; import { z } from "zod"; +import { v4 as uuid4 } from "uuid"; const CategoryController = { index: async (req: Request, res: Response) => { @@ -25,7 +23,8 @@ const CategoryController = { return res.status(200).json(item); }, store: async (req: Request, res: Response) => { - const item = await pool("categories").insert(req.body); + req.body.id = uuid4(); + const item = await pool("categories").insert(req.body, "id"); return res.status(200).json(item); }, update: async (req: Request, res: Response) => { diff --git a/controllers/post.ts b/controllers/post.ts index c4c68ab..aa40175 100644 --- a/controllers/post.ts +++ b/controllers/post.ts @@ -1,11 +1,9 @@ import { Request, Response } from "express"; import pool from "../lib/db"; -import { verify } from "../lib/hash"; -import User from "../models/user"; -import jwt from "jsonwebtoken"; import Post from "../models/post"; import { ValidateMethod } from "../middlewares/validate"; import { z } from "zod"; +import { v4 as uuid4 } from "uuid"; const PostController = { index: async (req: Request, res: Response) => { @@ -23,7 +21,8 @@ const PostController = { return res.status(200).json(item); }, store: async (req: Request, res: Response) => { - const item = await pool("posts").insert(req.body); + req.body.id = uuid4(); + const item = await pool("posts").insert(req.body, "id"); return res.status(200).json(item); }, update: async (req: Request, res: Response) => { diff --git a/package.json b/package.json index bf80765..34c7e2f 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "passport": "^0.6.0", "passport-jwt": "^4.0.1", "sqlite3": "^5.1.6", + "uuid": "^9.0.0", "zod": "^3.21.4" }, "devDependencies": { @@ -34,6 +35,7 @@ "@types/passport": "^1.0.12", "@types/passport-jwt": "^3.0.8", "@types/supertest": "^2.0.12", + "@types/uuid": "^9.0.2", "jest": "^29.5.0", "nodemon": "^2.0.22", "superagent": "^8.0.9", diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 608b2eb..91033e1 100644 --- a/tsconfig.prod.json +++ b/tsconfig.prod.json @@ -4,4 +4,7 @@ "sourceMap": false, "removeComments": true }, + "exclude": [ + "tests", + ] } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index eed77fc..7d0880d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -862,6 +862,11 @@ dependencies: "@types/superagent" "*" +"@types/uuid@^9.0.2": + version "9.0.2" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.2.tgz#ede1d1b1e451548d44919dc226253e32a6952c4b" + integrity sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ== + "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" @@ -3681,6 +3686,11 @@ utils-merge@1.0.1, utils-merge@^1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== +uuid@^9.0.0: + version "9.0.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" + integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== + v8-compile-cache-lib@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"