From 75209cc43551c4ef9cbe337c9b5c0e1cc6a6b417 Mon Sep 17 00:00:00 2001 From: Md Sharafat Hassain Date: Wed, 8 Apr 2026 21:06:42 +0600 Subject: [PATCH] api: get all api, post api, get signle data api and delete api was created --- src/app/modules/plan/plan.service.ts | 38 ++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/app/modules/plan/plan.service.ts b/src/app/modules/plan/plan.service.ts index 434e90e..e9dde6a 100644 --- a/src/app/modules/plan/plan.service.ts +++ b/src/app/modules/plan/plan.service.ts @@ -1,6 +1,7 @@ import { Request } from "express"; import { prisma } from "../../lib/prisma"; +import { AppError } from "../../utils/app_error"; const get_all_plan_from_db = async (req: Request) => { // define your own login here @@ -10,14 +11,21 @@ const get_all_plan_from_db = async (req: Request) => { const get_single_plan_from_db = async (req: Request) => { // define your own login here - const { id } = req.params ; - const result = await prisma.plan.findUnique({ where: { id } }); + const { id } = req.params; + const result = await prisma.plan.findUnique({ + where: { + id: id as string + } + }); return result; }; const create_plan_into_db = async (req: Request) => { // define your own login here - console.log(req.body) + const user = req.user + if (user?.role !== "ADMIN") { + throw new AppError("You don’t have permission to create plan information.!!!", 401) + } const result = await prisma.plan.create({ data: req.body }); return result; }; @@ -25,14 +33,34 @@ const create_plan_into_db = async (req: Request) => { const update_plan_into_db = async (req: Request) => { // define your own login here const { id } = req.params; - const result = await prisma.plan.update({ where: { id }, data: req.body }); + console.log(req.body) + const isPlanExist = await prisma.plan.findFirst({ + where: { + id: id as string + } + }) + if (!isPlanExist) { + throw new AppError("The plan is not available!!!", 404) + } + const result = await prisma.plan.update({ + where: { + id: isPlanExist.id + }, + data: { + ...req.body + } + }); return result; }; const delete_plan_from_db = async (req: Request) => { // define your own login here const { id } = req.params; - const result = await prisma.plan.delete({ where: { id } }); + const user = req.user + if (user?.role !== "ADMIN") { + throw new AppError("You don’t have permission to delete plan information.!!!", 401) + } + const result = await prisma.plan.delete({ where: { id: id as string } }); return result; };