adding the plan post api
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
|
||||
import catchAsync from "../../utils/catch_async";
|
||||
import manageResponse from "../../utils/manage_response";
|
||||
import { plan_service } from "./plan.service";
|
||||
|
||||
const get_all_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.get_all_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "All plan fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const get_single_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.get_single_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "Single plan fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const create_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.create_plan_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan created successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const update_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.update_plan_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan updated successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const delete_plan = catchAsync(async (req, res) => {
|
||||
const result = await plan_service.delete_plan_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "plan deleted successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
export const plan_controller = {
|
||||
get_all_plan,
|
||||
get_single_plan,
|
||||
create_plan,
|
||||
update_plan,
|
||||
delete_plan,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
import { Router } from "express";
|
||||
import RequestValidator from "../../middlewares/request_validator";
|
||||
import { plan_controller } from "./plan.controller";
|
||||
import { plan_validations } from "./plan.validation";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", plan_controller.get_all_plan);
|
||||
router.post(
|
||||
"/",
|
||||
RequestValidator(plan_validations.create_plan),
|
||||
plan_controller.create_plan,
|
||||
);
|
||||
router.get("/:id", plan_controller.get_single_plan);
|
||||
router.patch(
|
||||
"/:id",
|
||||
RequestValidator(plan_validations.update_plan),
|
||||
plan_controller.update_plan,
|
||||
);
|
||||
router.delete("/:id", plan_controller.delete_plan);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
|
||||
import { Request } from "express";
|
||||
import { prisma } from "../../lib/prisma";
|
||||
|
||||
const get_all_plan_from_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const result = await prisma.plan.findMany();
|
||||
return result;
|
||||
};
|
||||
|
||||
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 } });
|
||||
return result;
|
||||
};
|
||||
|
||||
const create_plan_into_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
console.log(req.body)
|
||||
const result = await prisma.plan.create({ data: req.body });
|
||||
return result;
|
||||
};
|
||||
|
||||
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 });
|
||||
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 } });
|
||||
return result;
|
||||
};
|
||||
|
||||
export const plan_service = {
|
||||
get_all_plan_from_db,
|
||||
get_single_plan_from_db,
|
||||
create_plan_into_db,
|
||||
update_plan_into_db,
|
||||
delete_plan_from_db,
|
||||
};
|
||||
@@ -0,0 +1,123 @@
|
||||
|
||||
export const planSwaggerDocs = {
|
||||
"/api/plan": {
|
||||
post: {
|
||||
tags: ["plan"],
|
||||
summary: "Create new plan",
|
||||
description: "",
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({
|
||||
|
||||
"planName": "PRO Plan",
|
||||
"price": 12,
|
||||
"planType": "PRO",
|
||||
"planDesc": "The plan is only for pro users",
|
||||
"planFeatures": {
|
||||
"storage": "10GB",
|
||||
"projects": 5,
|
||||
"support": "Email Support"
|
||||
}
|
||||
|
||||
|
||||
|
||||
}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
201: { description: "plan created successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
get: {
|
||||
tags: ["plan"],
|
||||
summary: "Get all plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "page",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
{
|
||||
name: "limit",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"/api/plan/{id}": {
|
||||
get: {
|
||||
tags: ["plan"],
|
||||
summary: "Get single plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
patch: {
|
||||
tags: ["plan"],
|
||||
summary: "Update plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: { description: "plan updated successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
delete: {
|
||||
tags: ["plan"],
|
||||
summary: "Delete plan",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "plan delete successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
const create_plan = z.object({
|
||||
planName: z.string(),
|
||||
price: z.number(),
|
||||
planType: z.enum(["FREE", "STANDARD", "PRO"]),
|
||||
planDesc: z.string(),
|
||||
planFeatures: z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
z.null(),
|
||||
z.array(z.any()),
|
||||
z.record(z.string(), z.any())
|
||||
])
|
||||
});
|
||||
const update_plan = z.object({});
|
||||
|
||||
export const plan_validations = {
|
||||
create_plan,
|
||||
update_plan,
|
||||
};
|
||||
Reference in New Issue
Block a user