-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add migration for adding roles to User model and create Product…
… model - Added migration file for adding roles to the User model - Created a new migration file for creating the Product model and its corresponding table in the database - Updated the schema.prisma file to include the Role enum and the Product model definition - Added a new controller file for handling product-related operations - Implemented the createProduct and updateProduct methods in the Product controller - Created routes for product CRUD operations, protected by authentication and admin middleware
- Loading branch information
1 parent
bcc161e
commit 9bfefdf
Showing
9 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
prisma/migrations/20240619110956_add_roles_to_user/migration.sql
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,5 @@ | ||
-- CreateEnum | ||
CREATE TYPE "Role" AS ENUM ('ADMIN', 'USER'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "role" "Role" NOT NULL DEFAULT 'USER'; |
12 changes: 12 additions & 0 deletions
12
prisma/migrations/20240619132413_add_product_table/migration.sql
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,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "products" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"price" DECIMAL(65,30) NOT NULL, | ||
"tags" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "products_pkey" PRIMARY KEY ("id") | ||
); |
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
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,45 @@ | ||
import { Request,Response } from "express"; | ||
import { prismaClient } from "../lib/db"; | ||
import { NotFoundException } from "../exceptions/not-found"; | ||
import { ErrorCode } from "../exceptions/root"; | ||
|
||
export default class Product{ | ||
public static createProduct = async(req:Request,res:Response)=>{ | ||
|
||
const product = await prismaClient.product.create({ | ||
data:{ | ||
...req.body, | ||
tags:req.body.tags.join(',') | ||
} | ||
}) | ||
res.json(product) | ||
} | ||
public static updateProduct = async(req:Request,res:Response)=>{ | ||
try { | ||
const product = req.body; | ||
if(product.tags){ | ||
product.tags = product.tags.join(',') | ||
} | ||
const updateProduct = await prismaClient.product.update({ | ||
where:{ | ||
id:+req.params.id | ||
}, | ||
data:product | ||
}) | ||
res.json(updateProduct) | ||
} catch (error) { | ||
throw new NotFoundException('Product not found..!',ErrorCode.PRODUCT_NOT_FOUND) | ||
} | ||
} | ||
public static deleteProduct = async(req:Request,res:Response)=>{ | ||
|
||
} | ||
public static ListProducts = async(req:Request,res:Response)=>{ | ||
|
||
} | ||
public static getProductsById = async(req:Request,res:Response)=>{ | ||
|
||
} | ||
} | ||
|
||
|
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
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
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,15 @@ | ||
|
||
import { NextFunction,Request,Response } from "express"; | ||
import { UnauthorizedException } from "../exceptions/unauthorized"; | ||
import { ErrorCode } from "../exceptions/root"; | ||
const adminMiddleware = async(req:Request,res:Response,next:NextFunction)=>{ | ||
const user = req.user | ||
if(user?.role == 'ADMIN'){ | ||
next() | ||
|
||
}else{ | ||
next (new UnauthorizedException('Unauthorized',ErrorCode.UnauthorizedException_ERROR)) | ||
} | ||
} | ||
|
||
export default adminMiddleware; |
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
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,14 @@ | ||
import { Router } from "express"; | ||
import Product from "../controllers/products.controller" | ||
import { errorHandler } from "../error-handler"; | ||
import authMiddleware from "../middlewares/auth"; | ||
import adminMiddleware from "../middlewares/admin"; | ||
|
||
const routes:Router = Router(); | ||
|
||
routes.post('/',[authMiddleware,adminMiddleware],errorHandler(Product.createProduct)) | ||
routes.put('/:id',[authMiddleware,adminMiddleware],errorHandler(Product.updateProduct)) | ||
routes.delete('/:id',[authMiddleware,adminMiddleware],errorHandler(Product.deleteProduct)) | ||
routes.get('/',[authMiddleware,adminMiddleware],errorHandler(Product.ListProducts)) | ||
routes.get('/:id',[authMiddleware,adminMiddleware],errorHandler(Product.getProductsById)) | ||
export default routes; |