Hozifa Elgherbawy commited on
Commit
f93d8f6
·
unverified ·
2 Parent(s): 8be0bd5 d491754

Merge pull request #27 from Modarb-Ai-Trainer:ExercisesAndWorkouts

Browse files
src/common/models/exercise.model.ts ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mongoose from "mongoose";
2
+ const { Schema } = mongoose;
3
+
4
+ export interface IExercise {
5
+ name: string;
6
+ }
7
+
8
+ const exerciseSchema = new Schema({
9
+ name: { type: String, required: true, unique: true, dropDups: true },
10
+
11
+ });
12
+
13
+ export type ExerciseDocument = IExercise & mongoose.Document;
14
+
15
+ export const Exercise = mongoose.model<ExerciseDocument>("exercises", exerciseSchema);
16
+
src/common/models/workout.model.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mongoose from "mongoose";
2
+ const { Schema } = mongoose;
3
+
4
+ export interface IWorkout {
5
+ name: string;
6
+ }
7
+
8
+ const workoutSchema = new Schema({
9
+ name: { type: String, required: true, unique: true, dropDups: true },
10
+ });
11
+
12
+
13
+ export type WorkoutDocument = IWorkout & mongoose.Document;
14
+
15
+ export const Workout = mongoose.model<WorkoutDocument>("workouts", workoutSchema);
src/common/serializers/exercise.serializtion.ts ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Expose, Transform } from "class-transformer";
2
+
3
+ export class ExerciseSerialization {
4
+ @Expose({ name: "_id" })
5
+ id: string;
6
+
7
+ @Expose()
8
+ name: string;
9
+
10
+ }
src/common/serializers/workout.serializtion.ts ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Expose, Transform } from "class-transformer";
2
+
3
+ export class WorkoutSerialization {
4
+ @Expose({ name: "_id" })
5
+ id: string;
6
+
7
+ @Expose()
8
+ name: string;
9
+
10
+ }
src/modules/users/exercises/controllers/exercises.controller.ts ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { ExerciseService } from "../services/exercises.service";
2
+ import { Request, Response } from "express";
3
+ import { JsonResponse } from "@lib/responses/json-response";
4
+ import { parsePaginationQuery } from "@helpers/pagination";
5
+ import { asyncHandler } from "@helpers/async-handler";
6
+ import { paramsValidator } from "@helpers/validation.helper";
7
+ import { BaseController } from "@lib/controllers/controller.base";
8
+ import { Prefix } from "@lib/decorators/prefix.decorator";
9
+ import { serialize } from "@helpers/serialize";
10
+ import { ExerciseSerialization } from "@common/serializers/exercise.serializtion";
11
+
12
+
13
+ @Prefix("/users/exercises")
14
+ export class ExerciseController extends BaseController {
15
+ private exercisesService = new ExerciseService();
16
+
17
+ setRoutes(): void {
18
+ this.router.get("/", asyncHandler(this.list));
19
+ this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
20
+ }
21
+
22
+ list = async (req: Request, res: Response) => {
23
+ const paginationQuery = parsePaginationQuery(req.query);
24
+ const { docs, paginationData } = await this.exercisesService.list(
25
+ {},
26
+ paginationQuery
27
+ );
28
+
29
+ const response = new JsonResponse({
30
+ data: serialize(docs, ExerciseSerialization),
31
+ meta: paginationData,
32
+ });
33
+ return res.json(response);
34
+ };
35
+
36
+ get = async (req: Request, res: Response) => {
37
+ const data = await this.exercisesService.findOneOrFail({
38
+ _id: req.params.id,
39
+ });
40
+ const response = new JsonResponse({
41
+ data: serialize(data.toJSON(), ExerciseSerialization),
42
+ });
43
+ res.json(response);
44
+ };
45
+ }
src/modules/users/exercises/services/exercises.service.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import { Exercise } from "@common/models/exercise.model";
2
+ import { CrudService } from "@lib/services/crud.service";
3
+
4
+ export class ExerciseService extends CrudService(Exercise) {}
src/modules/users/workouts/controllers/workouts.controller.ts ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { WorkoutService } from "../services/workouts.service";
2
+ import { Request, Response } from "express";
3
+ import { JsonResponse } from "@lib/responses/json-response";
4
+ import { parsePaginationQuery } from "@helpers/pagination";
5
+ import { asyncHandler } from "@helpers/async-handler";
6
+ import { paramsValidator } from "@helpers/validation.helper";
7
+ import { BaseController } from "@lib/controllers/controller.base";
8
+ import { Prefix } from "@lib/decorators/prefix.decorator";
9
+ import { serialize } from "@helpers/serialize";
10
+ import { WorkoutSerialization } from "@common/serializers/workout.serializtion";
11
+
12
+ @Prefix("/users/workouts")
13
+ export class WorkoutController extends BaseController {
14
+ private workoutsService = new WorkoutService();
15
+
16
+ setRoutes(): void {
17
+ this.router.get("/", asyncHandler(this.list));
18
+ this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
19
+ }
20
+
21
+ list = async (req: Request, res: Response) => {
22
+ const paginationQuery = parsePaginationQuery(req.query);
23
+ const { docs, paginationData } = await this.workoutsService.list(
24
+ {},
25
+ paginationQuery
26
+ );
27
+
28
+ const response = new JsonResponse({
29
+ data: serialize(docs, WorkoutSerialization),
30
+ meta: paginationData,
31
+ });
32
+ return res.json(response);
33
+ };
34
+
35
+ get = async (req: Request, res: Response) => {
36
+ const data = await this.workoutsService.findOneOrFail({
37
+ _id: req.params.id,
38
+ });
39
+ const response = new JsonResponse({
40
+ data: serialize(data.toJSON(), WorkoutSerialization),
41
+ });
42
+ res.json(response);
43
+ };
44
+ }
src/modules/users/workouts/services/workouts.service.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import { Workout } from "@common/models/workout.model";
2
+ import { CrudService } from "@lib/services/crud.service";
3
+
4
+ export class WorkoutService extends CrudService(Workout) {}