api: get all api, post api, get signle data api and delete api was created
This commit is contained in:
@@ -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
|
||||
@@ -11,13 +12,20 @@ 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 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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user