Template API: create database schema
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import catchAsync from "../../utils/catch_async.js";
|
||||
import manageResponse from "../../utils/manage_response.js";
|
||||
import { template_service } from "./template.service.js";
|
||||
|
||||
const get_all_template = catchAsync(async (req, res) => {
|
||||
const result = await template_service.get_all_template_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "All template fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const get_single_template = catchAsync(async (req, res) => {
|
||||
const result = await template_service.get_single_template_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "Single template fetched successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const create_template = catchAsync(async (req, res) => {
|
||||
const result = await template_service.create_template_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "template created successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const update_template = catchAsync(async (req, res) => {
|
||||
const result = await template_service.update_template_into_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "template updated successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
const delete_template = catchAsync(async (req, res) => {
|
||||
const result = await template_service.delete_template_from_db(req);
|
||||
manageResponse(res, {
|
||||
success: true,
|
||||
statusCode: 200,
|
||||
message: "template deleted successfully.",
|
||||
data: result,
|
||||
meta: {},
|
||||
});
|
||||
});
|
||||
|
||||
export const template_controller = {
|
||||
get_all_template,
|
||||
get_single_template,
|
||||
create_template,
|
||||
update_template,
|
||||
delete_template,
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Router } from "express";
|
||||
import RequestValidator from "../../middlewares/request_validator.js";
|
||||
import { template_controller } from "./template.controller.js";
|
||||
import { template_validations } from "./template.validation.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", template_controller.get_all_template);
|
||||
router.post(
|
||||
"/",
|
||||
RequestValidator(template_validations.create_template),
|
||||
template_controller.create_template,
|
||||
);
|
||||
router.get("/:id", template_controller.get_single_template);
|
||||
router.patch(
|
||||
"/:id",
|
||||
RequestValidator(template_validations.update_template),
|
||||
template_controller.update_template,
|
||||
);
|
||||
router.delete("/:id", template_controller.delete_template);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Request } from "express";
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
const get_all_template_from_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const result = await prisma.template.findMany();
|
||||
return result;
|
||||
};
|
||||
|
||||
const get_single_template_from_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const result = await prisma.template.findUnique({ where: { id } });
|
||||
return result;
|
||||
};
|
||||
|
||||
const create_template_into_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const result = await prisma.template.create({ data: req.body });
|
||||
return result;
|
||||
};
|
||||
|
||||
const update_template_into_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const result = await prisma.template.update({
|
||||
where: { id },
|
||||
data: req.body,
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const delete_template_from_db = async (req: Request) => {
|
||||
// define your own login here
|
||||
const { id } = req.params;
|
||||
const result = await prisma.template.delete({ where: { id } });
|
||||
return result;
|
||||
};
|
||||
|
||||
export const template_service = {
|
||||
get_all_template_from_db,
|
||||
get_single_template_from_db,
|
||||
create_template_into_db,
|
||||
update_template_into_db,
|
||||
delete_template_from_db,
|
||||
};
|
||||
@@ -0,0 +1,106 @@
|
||||
export const templateSwaggerDocs = {
|
||||
"/api/template": {
|
||||
post: {
|
||||
tags: ["template"],
|
||||
summary: "Create new template",
|
||||
description: "",
|
||||
requestBody: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
example: JSON.stringify({}), // put your request body
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
201: { description: "template created successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
get: {
|
||||
tags: ["template"],
|
||||
summary: "Get all template",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "page",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
{
|
||||
name: "limit",
|
||||
in: "query",
|
||||
required: false,
|
||||
schema: { type: "number" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "template fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"/api/template/{id}": {
|
||||
get: {
|
||||
tags: ["template"],
|
||||
summary: "Get single template",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "template fetched successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
patch: {
|
||||
tags: ["template"],
|
||||
summary: "Update template",
|
||||
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: "template updated successfully" },
|
||||
500: { description: "Validation error or internal server error" },
|
||||
},
|
||||
},
|
||||
delete: {
|
||||
tags: ["template"],
|
||||
summary: "Delete template",
|
||||
description: "",
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "path",
|
||||
required: true,
|
||||
schema: { type: "string" },
|
||||
},
|
||||
],
|
||||
responses: {
|
||||
200: { description: "template delete successfully" },
|
||||
401: { description: "unauthorized" },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const create_template = z.object({});
|
||||
const update_template = z.object({});
|
||||
|
||||
export const template_validations = {
|
||||
create_template,
|
||||
update_template,
|
||||
};
|
||||
Reference in New Issue
Block a user