Hozifa Elgherbawy commited on
Commit
b798149
·
unverified ·
2 Parent(s): fe47918 b7f0477

Merge pull request #40 from Modarb-Ai-Trainer:Workout

Browse files
src/common/models/workout.model.ts CHANGED
@@ -24,16 +24,14 @@ export interface IWorkout {
24
  const workoutSchema = new Schema({
25
  name: { type: String, required: true, unique: true, dropDups: true },
26
  type: { type: String, required: true },
27
- created_by: { type: mongoose.Types.ObjectId, ref: "users" },
28
  templateWeeks: [
29
  {
30
  days: [
31
  {
32
  day: Number,
33
  exercises: [
34
- {
35
- exercise: { type: mongoose.Types.ObjectId, ref: "exercises" },
36
- },
37
  ],
38
  },
39
  ],
 
24
  const workoutSchema = new Schema({
25
  name: { type: String, required: true, unique: true, dropDups: true },
26
  type: { type: String, required: true },
27
+ created_by: { type: mongoose.Types.ObjectId, ref: "admins" },
28
  templateWeeks: [
29
  {
30
  days: [
31
  {
32
  day: Number,
33
  exercises: [
34
+ { type: mongoose.Types.ObjectId, ref: "exercises" },
 
 
35
  ],
36
  },
37
  ],
src/common/serializers/workout.serializtion.ts CHANGED
@@ -1,4 +1,24 @@
1
  import { Expose, Transform } from "class-transformer";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  export class WorkoutSerialization {
4
  @Expose({ name: "_id" })
@@ -7,4 +27,16 @@ export class WorkoutSerialization {
7
  @Expose()
8
  name: string;
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  }
 
1
  import { Expose, Transform } from "class-transformer";
2
+ import { serialize } from "@helpers/serialize";
3
+
4
+
5
+
6
+ class Days {
7
+ @Expose()
8
+ day: number;
9
+
10
+ @Expose({ name: "exercises" })
11
+ exercises: any;
12
+ }
13
+
14
+
15
+ class TemplateWeeks {
16
+ @Expose({ name: "days" })
17
+ @Transform(
18
+ ({ value }) => serialize(value, Days)
19
+ )
20
+ days: any;
21
+ }
22
 
23
  export class WorkoutSerialization {
24
  @Expose({ name: "_id" })
 
27
  @Expose()
28
  name: string;
29
 
30
+ @Expose()
31
+ type: string;
32
+
33
+ @Expose()
34
+ created_by: string;
35
+
36
+ @Expose({ name: "templateWeeks" })
37
+ @Transform(
38
+ ({ value }) => serialize(value, TemplateWeeks)
39
+ )
40
+ templateWeeks: any;
41
+
42
  }
src/modules/console/modules/admins/controllers/admins.controller.ts CHANGED
@@ -14,7 +14,7 @@ import { serialize } from "@helpers/serialize";
14
  import { AdminSerialization } from "modules/console/common/serializers/admin.serialization";
15
 
16
  @Prefix("/console/admins")
17
- @ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
18
  export class AdminsController extends BaseController {
19
  private adminsService = new AdminsService();
20
 
 
14
  import { AdminSerialization } from "modules/console/common/serializers/admin.serialization";
15
 
16
  @Prefix("/console/admins")
17
+ // @ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
18
  export class AdminsController extends BaseController {
19
  private adminsService = new AdminsService();
20
 
src/modules/console/modules/{admins → workouts}/controllers/workouts.controller.ts RENAMED
@@ -4,6 +4,8 @@ import { JsonResponse } from "@lib/responses/json-response";
4
  import { parsePaginationQuery } from "@helpers/pagination";
5
  import { asyncHandler } from "@helpers/async-handler";
6
  import { paramsValidator, bodyValidator } 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";
@@ -12,7 +14,7 @@ import { ControllerMiddleware } from "@lib/decorators/controller-middleware.deco
12
  import { AdminGuardMiddleware } from "modules/console/common/guards/admins.guard";
13
 
14
  @Prefix("/console/workouts")
15
- @ControllerMiddleware(AdminGuardMiddleware({}))
16
 
17
  export class WorkoutController extends BaseController {
18
  private workoutsService = new WorkoutService();
@@ -20,6 +22,20 @@ export class WorkoutController extends BaseController {
20
  setRoutes() {
21
  this.router.get("/", asyncHandler(this.list));
22
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  }
24
 
25
  list = async (req: Request, res: Response) => {
@@ -29,11 +45,6 @@ export class WorkoutController extends BaseController {
29
  paginationQuery
30
  );
31
 
32
- // const response = new JsonResponse({
33
- // data: serialize(docs, WorkoutSerialization),
34
- // meta: paginationData,
35
- // });
36
- // return res.json(response);
37
  return JsonResponse.success(
38
  {
39
  data: serialize(docs, WorkoutSerialization),
 
4
  import { parsePaginationQuery } from "@helpers/pagination";
5
  import { asyncHandler } from "@helpers/async-handler";
6
  import { paramsValidator, bodyValidator } from "@helpers/validation.helper";
7
+ import { createWorkoutSchema } from "../validations/create-workout.validation";
8
+ import { updateWorkoutSchema } from "../validations/update-workout.validation";
9
  import { BaseController } from "@lib/controllers/controller.base";
10
  import { Prefix } from "@lib/decorators/prefix.decorator";
11
  import { serialize } from "@helpers/serialize";
 
14
  import { AdminGuardMiddleware } from "modules/console/common/guards/admins.guard";
15
 
16
  @Prefix("/console/workouts")
17
+ // @ControllerMiddleware(AdminGuardMiddleware())
18
 
19
  export class WorkoutController extends BaseController {
20
  private workoutsService = new WorkoutService();
 
22
  setRoutes() {
23
  this.router.get("/", asyncHandler(this.list));
24
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
25
+ this.router.post("/",
26
+ bodyValidator(createWorkoutSchema),
27
+ asyncHandler(this.create));
28
+ this.router.patch(
29
+ "/:id",
30
+ paramsValidator("id"),
31
+ bodyValidator(updateWorkoutSchema),
32
+ asyncHandler(this.update)
33
+ );
34
+ this.router.delete(
35
+ "/:id",
36
+ paramsValidator("id"),
37
+ asyncHandler(this.delete)
38
+ );
39
  }
40
 
41
  list = async (req: Request, res: Response) => {
 
45
  paginationQuery
46
  );
47
 
 
 
 
 
 
48
  return JsonResponse.success(
49
  {
50
  data: serialize(docs, WorkoutSerialization),
src/modules/console/modules/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) {};
src/modules/console/modules/workouts/validations/create-workout.validation.ts ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as joi from "joi";
2
+ import { createSchema } from "@helpers/create-schema";
3
+
4
+ export interface ICreateWorkout {
5
+ name: string;
6
+ type: string;
7
+ created_by: string;
8
+ templateWeeks: string[];
9
+ };
10
+
11
+
12
+ export const createWorkoutSchema = createSchema<ICreateWorkout>({
13
+ name: joi.string().empty().required().messages({
14
+ "string.base": "please enter a valid name",
15
+ "any.required": "name is required",
16
+ "string.empty": "name can not be empty",
17
+ }),
18
+ type: joi.string().empty().required().messages({
19
+ "string.base": "please enter a valid type",
20
+ "any.required": "type is required",
21
+ "string.empty": "type can not be empty",
22
+ }),
23
+ created_by: joi.string().empty().required().messages({
24
+ "string.base": "please enter a valid created_by",
25
+ "any.required": "created_by is required",
26
+ "string.empty": "created_by can not be empty",
27
+ }),
28
+ templateWeeks: joi.array().empty().required().messages({
29
+ "string.base": "please enter a valid templateWeeks",
30
+ "any.required": "templateWeeks is required",
31
+ "string.empty": "templateWeeks can not be empty",
32
+ }),
33
+ });
src/modules/console/modules/workouts/validations/update-workout.validation.ts ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as joi from "joi";
2
+ import { createSchema } from "@helpers/create-schema";
3
+
4
+ export interface IUpdateWorkout {
5
+ name?: string;
6
+ type?: string;
7
+ created_by?: string;
8
+ templateWeeks?: string[];
9
+ };
10
+
11
+
12
+ export const updateWorkoutSchema = createSchema<IUpdateWorkout>({
13
+ name: joi.string().empty().optional().messages({
14
+ "string.base": "please enter a valid name",
15
+ "string.empty": "name can not be empty",
16
+ }),
17
+ type: joi.string().empty().optional().messages({
18
+ "string.base": "please enter a valid type",
19
+ "string.empty": "type can not be empty",
20
+ }),
21
+ created_by: joi.string().empty().optional().messages({
22
+ "string.base": "please enter a valid created_by",
23
+ "string.empty": "created_by can not be empty",
24
+ }),
25
+ templateWeeks: joi.array().empty().optional().messages({
26
+ "string.base": "please enter a valid templateWeeks",
27
+ "string.empty": "templateWeeks can not be empty",
28
+ }),
29
+
30
+ });