Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //src/routes/group.router.ts
- import Router from 'koa-router';
- import GroupController from '../controllers/group.controller';
- import { groupPaths } from '../consts';
- import { validateBody } from '../middlewares';
- import {
- addStudentToGroupSchema,
- groupSchema,
- updateGroupSchema,
- } from '../schemas';
- import { ContextWithBody } from '../middlewares/validateBody';
- import { CreateGroupDto } from '../data/dto/GroupDto';
- const controller = new GroupController();
- const router = new Router<object, ContextWithBody<unknown>>();
- router
- .get(groupPaths.BASE, controller.getAllGroups)
- .post(
- groupPaths.BASE,
- validateBody<CreateGroupDto>(groupSchema),
- controller.createGroup,
- )
- .get(groupPaths.ID, controller.getGroupById)
- .patch(
- groupPaths.ID,
- validateBody(updateGroupSchema),
- controller.updateGroupByID,
- )
- .delete(groupPaths.ID, controller.deleteGroupById)
- .get(groupPaths.STUDENTS, controller.getAllGroupStudents)
- .post(
- groupPaths.STUDENTS,
- validateBody(addStudentToGroupSchema),
- controller.addStudentToGroup,
- );
- export const groupRouter = router;
- //src/controllers/group.controller.ts
- import { groupService } from '../services/group.service';
- import { groupRes } from '../consts/index';
- import { ContextWithBody } from '../middlewares/validateBody';
- import { CreateGroupDto, UpdateGroupDto } from '../data/dto/GroupDto';
- import { ApiResponse } from '../helpers';
- export default class GroupController {
- async getAllGroups(ctx: ContextWithBody<unknown>) {
- const data = await groupService.getAllGroups();
- ApiResponse.success(ctx, { ...groupRes.OK, data });
- }
- async getGroupById(ctx: ContextWithBody<unknown>) {
- const data = await groupService.getGroupById(+ctx.params.id);
- ApiResponse.success(ctx, { ...groupRes.OK, data });
- }
- async createGroup(ctx: ContextWithBody<CreateGroupDto>) {
- const groupObj = ctx.request.body;
- const data = await groupService.createGroup(groupObj);
- ApiResponse.success(ctx, { ...groupRes.CREATED, data });
- }
- async updateGroupByID(ctx: ContextWithBody<UpdateGroupDto>) {
- const groupObj = ctx.request.body;
- const id = +ctx.params.id;
- const data = await groupService.updateGroupById(groupObj, id);
- ApiResponse.success(ctx, { ...groupRes.UPDATED, data });
- }
- async deleteGroupById(ctx: ContextWithBody<unknown>) {
- const id = +ctx.params.id;
- const data = await groupService.deleteGroupById(id);
- ApiResponse.success(ctx, { ...groupRes.DELETED, data });
- }
- async addStudentToGroup(ctx: ContextWithBody<number>) {
- const studentId = ctx.request.body;
- const id = +ctx.params.id;
- const data = await groupService.addStudentToGroup(studentId, id);
- ApiResponse.success(ctx, { ...groupRes.OK, data });
- }
- async getAllGroupStudents(ctx: ContextWithBody<unknown>) {
- const id = +ctx.params.id;
- const data = await groupService.getAllGroupStudents(id);
- ApiResponse.success(ctx, { ...groupRes.OK, data });
- }
- async removeStudentFromGroup(ctx: ContextWithBody<number>) {
- const studentId = ctx.request.body;
- const id = +ctx.params.id;
- const data = await groupService.addStudentToGroup(studentId, id);
- ApiResponse.success(ctx, { ...groupRes.OK, data });
- }
- }
- //src/middlewares/validateBody.ts
- import { Context, Next, Request } from 'koa';
- import { Schema } from 'joi';
- import ApiError from '../helpers/ApiError';
- interface RequestWithBody<T> extends Request {
- body: T;
- }
- interface ContextWithBody<T> extends Context {
- request: RequestWithBody<T>;
- }
- const validateBody = <T>(schema: Schema) => async (
- ctx: ContextWithBody<T>,
- next: Next,
- ) => {
- const { error } = schema.validate(ctx.request.body);
- if (error) {
- throw new ApiError({
- statusCode: 400,
- message: error.message,
- errorDetail: error,
- });
- }
- ctx.request.body = ctx.request.body as T;
- await next();
- };
- export default validateBody;
- export { ContextWithBody };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement