File size: 1,771 Bytes
d491754
 
 
 
 
 
 
 
 
 
4e3908e
 
d491754
 
4e3908e
d491754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { WorkoutService } from "../services/workouts.service";
import { Request, Response } from "express";
import { JsonResponse } from "@lib/responses/json-response";
import { parsePaginationQuery } from "@helpers/pagination";
import { asyncHandler } from "@helpers/async-handler";
import { paramsValidator } from "@helpers/validation.helper";
import { BaseController } from "@lib/controllers/controller.base";
import { Prefix } from "@lib/decorators/prefix.decorator";
import { serialize } from "@helpers/serialize";
import { WorkoutSerialization } from "@common/serializers/workout.serializtion";
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";

@Prefix("/users/workouts")
@ControllerMiddleware(UsersGuardMiddleware())
export class WorkoutController extends BaseController {
  private workoutsService = new WorkoutService();

  setRoutes(): void {
    this.router.get("/", asyncHandler(this.list));
    this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
  }

  list = async (req: Request, res: Response) => {
    const paginationQuery = parsePaginationQuery(req.query);
    const { docs, paginationData } = await this.workoutsService.list(
      {},
      paginationQuery
    );

    const response = new JsonResponse({
      data: serialize(docs, WorkoutSerialization),
      meta: paginationData,
    });
    return res.json(response);
  };

  get = async (req: Request, res: Response) => {
    const data = await this.workoutsService.findOneOrFail({
      _id: req.params.id,
    });
    const response = new JsonResponse({
      data: serialize(data.toJSON(), WorkoutSerialization),
    });
    res.json(response);
  };
}