Advertisement
djbob2000

problem router Max

Apr 28th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //src/routes/group.router.ts
  2. import Router from 'koa-router';
  3. import GroupController from '../controllers/group.controller';
  4. import { groupPaths } from '../consts';
  5. import { validateBody } from '../middlewares';
  6. import {
  7.   addStudentToGroupSchema,
  8.   groupSchema,
  9.   updateGroupSchema,
  10. } from '../schemas';
  11. import { ContextWithBody } from '../middlewares/validateBody';
  12. import { CreateGroupDto } from '../data/dto/GroupDto';
  13.  
  14. const controller = new GroupController();
  15. const router = new Router<object, ContextWithBody<unknown>>();
  16.  
  17. router
  18.   .get(groupPaths.BASE, controller.getAllGroups)
  19.   .post(
  20.     groupPaths.BASE,
  21.     validateBody<CreateGroupDto>(groupSchema),
  22.     controller.createGroup,
  23.   )
  24.   .get(groupPaths.ID, controller.getGroupById)
  25.   .patch(
  26.     groupPaths.ID,
  27.     validateBody(updateGroupSchema),
  28.     controller.updateGroupByID,
  29.   )
  30.   .delete(groupPaths.ID, controller.deleteGroupById)
  31.   .get(groupPaths.STUDENTS, controller.getAllGroupStudents)
  32.   .post(
  33.     groupPaths.STUDENTS,
  34.     validateBody(addStudentToGroupSchema),
  35.     controller.addStudentToGroup,
  36.   );
  37.  
  38. export const groupRouter = router;
  39.  
  40.  
  41.  
  42. //src/controllers/group.controller.ts
  43. import { groupService } from '../services/group.service';
  44. import { groupRes } from '../consts/index';
  45. import { ContextWithBody } from '../middlewares/validateBody';
  46. import { CreateGroupDto, UpdateGroupDto } from '../data/dto/GroupDto';
  47. import { ApiResponse } from '../helpers';
  48.  
  49. export default class GroupController {
  50.   async getAllGroups(ctx: ContextWithBody<unknown>) {
  51.     const data = await groupService.getAllGroups();
  52.     ApiResponse.success(ctx, { ...groupRes.OK, data });
  53.   }
  54.  
  55.   async getGroupById(ctx: ContextWithBody<unknown>) {
  56.     const data = await groupService.getGroupById(+ctx.params.id);
  57.     ApiResponse.success(ctx, { ...groupRes.OK, data });
  58.   }
  59.  
  60.   async createGroup(ctx: ContextWithBody<CreateGroupDto>) {
  61.     const groupObj = ctx.request.body;
  62.     const data = await groupService.createGroup(groupObj);
  63.     ApiResponse.success(ctx, { ...groupRes.CREATED, data });
  64.   }
  65.  
  66.   async updateGroupByID(ctx: ContextWithBody<UpdateGroupDto>) {
  67.     const groupObj = ctx.request.body;
  68.     const id = +ctx.params.id;
  69.     const data = await groupService.updateGroupById(groupObj, id);
  70.     ApiResponse.success(ctx, { ...groupRes.UPDATED, data });
  71.   }
  72.  
  73.   async deleteGroupById(ctx: ContextWithBody<unknown>) {
  74.     const id = +ctx.params.id;
  75.     const data = await groupService.deleteGroupById(id);
  76.     ApiResponse.success(ctx, { ...groupRes.DELETED, data });
  77.   }
  78.  
  79.   async addStudentToGroup(ctx: ContextWithBody<number>) {
  80.     const studentId = ctx.request.body;
  81.     const id = +ctx.params.id;
  82.     const data = await groupService.addStudentToGroup(studentId, id);
  83.     ApiResponse.success(ctx, { ...groupRes.OK, data });
  84.   }
  85.  
  86.   async getAllGroupStudents(ctx: ContextWithBody<unknown>) {
  87.     const id = +ctx.params.id;
  88.     const data = await groupService.getAllGroupStudents(id);
  89.     ApiResponse.success(ctx, { ...groupRes.OK, data });
  90.   }
  91.  
  92.   async removeStudentFromGroup(ctx: ContextWithBody<number>) {
  93.     const studentId = ctx.request.body;
  94.     const id = +ctx.params.id;
  95.     const data = await groupService.addStudentToGroup(studentId, id);
  96.     ApiResponse.success(ctx, { ...groupRes.OK, data });
  97.   }
  98. }
  99.  
  100.  
  101. //src/middlewares/validateBody.ts
  102. import { Context, Next, Request } from 'koa';
  103. import { Schema } from 'joi';
  104. import ApiError from '../helpers/ApiError';
  105.  
  106. interface RequestWithBody<T> extends Request {
  107.   body: T;
  108. }
  109.  
  110. interface ContextWithBody<T> extends Context {
  111.   request: RequestWithBody<T>;
  112. }
  113.  
  114. const validateBody = <T>(schema: Schema) => async (
  115.   ctx: ContextWithBody<T>,
  116.   next: Next,
  117. ) => {
  118.   const { error } = schema.validate(ctx.request.body);
  119.   if (error) {
  120.     throw new ApiError({
  121.       statusCode: 400,
  122.       message: error.message,
  123.       errorDetail: error,
  124.     });
  125.   }
  126.  
  127.   ctx.request.body = ctx.request.body as T;
  128.  
  129.   await next();
  130. };
  131.  
  132. export default validateBody;
  133. export { ContextWithBody };
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement