INTEGRATE EDIT AND DELETE API
Thu Feb 29 2024 07:19:38 GMT+0000 (Coordinated Universal Time)
Saved by @2018331055
controllers/project/projectAttributes/projectAttributes.controller.ts import { PrismaClient } from "@prisma/client"; import { NextFunction, Request, Response } from "express"; import catchAsync from "../../../utils/catchAsync"; import AppError from "../../../utils/appError"; const prisma = new PrismaClient(); const createProjectCategory = catchAsync( async (req: Request, res: Response, next: NextFunction) => { const { name } = req.body; const businessId = req.params.business_id; const business = await prisma.business.findFirst({ where: { id: businessId, }, }); if (!business) { return next(new AppError("This business doesn't exist", 400)); } if (!name || name.length === 0) { return next(new AppError("Project category name can't be empty", 400)); } const isProjectCategoryExit = await prisma.projectCategory.findFirst({ where: { businessId: businessId, name: name }, }); @@ -55,7 +55,114 @@ const createProjectCategory = catchAsync( res.status(200).json({ projectCategories: projectCategories, message: "Project Category added successfully", }); } ); const editProjectCategory = catchAsync( async (req: Request, res: Response, next: NextFunction) => { const { name } = req.body; const businessId = req.params.business_id; const categoryId = req.params.category_id; const business = await prisma.business.findFirst({ where: { id: businessId, }, }); if (!business) { return next(new AppError("This business doesn't exist", 400)); } if (!name || name.length === 0) { return next(new AppError("Project category name can't be empty", 400)); } const isProjectCategoryExist = await prisma.projectCategory.findFirst({ where: { businessId: businessId, id: categoryId }, }); if(!isProjectCategoryExist) { return next(new AppError("Project category doesn't exist", 400)); } const isProjectCategoryNameExit = await prisma.projectCategory.findFirst({ where: { businessId: businessId, name: name }, }); if (isProjectCategoryNameExit) { return next( new AppError( "This project category already exist in this business", 400 ) ); } await prisma.projectCategory.update({ where: { id: categoryId }, data: { name, business: { connect: { id: businessId, }, }, }, }); const projectCategories = await prisma.projectCategory.findMany({ where: { businessId: businessId }, }); res.status(200).json({ projectCategories: projectCategories, message: "Project category edited successfully", }); } ); const deleteProjectCategory = catchAsync( async (req: Request, res: Response, next: NextFunction) => { const businessId = req.params.business_id; const categoryId = req.params.category_id; const business = await prisma.business.findFirst({ where: { id: businessId, }, }); if (!business) { return next(new AppError("This business doesn't exist", 400)); } const isProjectCategoryExist = await prisma.projectCategory.findFirst({ where: { businessId: businessId, id: categoryId }, }); if(!isProjectCategoryExist) { return next(new AppError("Project category doesn't exist", 400)); } await prisma.projectCategory.delete({ where: { id: categoryId }, }); const projectCategories = await prisma.projectCategory.findMany({ where: { businessId: businessId }, }); res.status(200).json({ projectCategories: projectCategories, message: "Project category deleted successfully", }); } ); @@ -107,7 +214,112 @@ const createProjectTag = catchAsync( res.status(200).json({ projectTags: projectTags, message: "Project Tag added successfully", message: "Project tag added successfully", }); } ); const editProjectTag = catchAsync( async (req: Request, res: Response, next: NextFunction) => { const { name } = req.body; const businessId = req.params.business_id; const tagId = req.params.tag_id; const business = await prisma.business.findFirst({ where: { id: businessId, }, }); if (!business) { return next(new AppError("This business doesn't exist", 400)); } if (!name || name.length === 0) { return next(new AppError("Project tag name can't be empty", 400)); } const isProjectTagExist = await prisma.projectTag.findFirst({ where: { businessId: businessId, id: tagId }, }); if(!isProjectTagExist) { return next(new AppError("Project tag doesn't exist", 400)); } const isProjectTagNameExit = await prisma.projectTag.findFirst({ where: { businessId: businessId, name: name }, }); if (isProjectTagNameExit) { return next( new AppError("This project tag already exist in this business", 400) ); } await prisma.projectTag.update({ where: { id: tagId }, data: { name, business: { connect: { id: businessId, }, }, }, }); const projectTags = await prisma.projectTag.findMany({ where: { businessId: businessId }, }); res.status(200).json({ projectTags: projectTags, message: "Project tag edited successfully", }); } ); const deleteProjectTag = catchAsync( async (req: Request, res: Response, next: NextFunction) => { const businessId = req.params.business_id; const tagId = req.params.tag_id; const business = await prisma.business.findFirst({ where: { id: businessId, }, }); if (!business) { return next(new AppError("This business doesn't exist", 400)); } const isProjectTagExist = await prisma.projectTag.findFirst({ where: { businessId: businessId, id: tagId }, }); if(!isProjectTagExist) { return next(new AppError("Project tag doesn't exist", 400)); } await prisma.projectTag.delete({ where: { id: tagId }, }); const projectTags = await prisma.projectTag.findMany({ where: { businessId: businessId }, }); res.status(200).json({ projectTags: projectTags, message: "Project tag deleted successfully", }); } ); @@ -148,6 +360,10 @@ const getProjectAttributes = catchAsync( export default { createProjectCategory, editProjectCategory, deleteProjectCategory, getProjectAttributes, createProjectTag, editProjectTag, deleteProjectTag }; ////another file prisma/dbml/schema.dbml Table Invoice { address String phone String email String description String note String description Json note Json invoiceItems InvoiceItem [not null] subTotalPrice Float discountPrice Float ////ANPOTHER FILE router/business.router.ts @@ -229,11 +229,33 @@ business_router projectAttributesController.createProjectCategory ); business_router .route("/business/:business_id/project-category/:category_id/") .patch( checkAccess.businessActionRestrcition("create-project-category"), projectAttributesController.createProjectCategory ) .delete( checkAccess.businessActionRestrcition("create-project-category"), projectAttributesController.deleteProjectCategory ); business_router .route("/business/:business_id/project-tag/") .post( checkAccess.businessActionRestrcition("create-project-tag"), projectAttributesController.createProjectTag ); business_router .route("/business/:business_id/project-tag/:tag_id/") .patch( checkAccess.businessActionRestrcition("create-project-tag"), projectAttributesController.editProjectTag ) .delete( checkAccess.businessActionRestrcition("create-project-tag"), projectAttributesController.deleteProjectTag ); export { business_router };
Comments