Spaces:
Running
Running
Merge pull request #53 from Modarb-Ai-Trainer:template
Browse files- src/common/models/template.model.ts +11 -1
- src/common/serializers/template.serialization.ts +15 -0
- src/modules/users/modules/templates/controllers/templates.controller.ts +90 -0
- src/modules/users/modules/templates/services/templates.service.ts +4 -0
- src/modules/users/modules/templates/validations/create-templates.validation.ts +32 -0
- src/modules/users/modules/templates/validations/update-templates.validation.ts +30 -0
src/common/models/template.model.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
| 1 |
import mongoose from "mongoose";
|
| 2 |
const { Schema } = mongoose;
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
const templateSchema = new Schema({
|
| 5 |
name: { type: String, required: true, unique: true, dropDups: true },
|
| 6 |
user: { type: mongoose.Types.ObjectId, ref: "users" },
|
|
@@ -8,5 +14,9 @@ const templateSchema = new Schema({
|
|
| 8 |
});
|
| 9 |
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
export const
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import mongoose from "mongoose";
|
| 2 |
const { Schema } = mongoose;
|
| 3 |
|
| 4 |
+
export interface ITemplate {
|
| 5 |
+
name: string;
|
| 6 |
+
user: string;
|
| 7 |
+
exercises: string[];
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
const templateSchema = new Schema({
|
| 11 |
name: { type: String, required: true, unique: true, dropDups: true },
|
| 12 |
user: { type: mongoose.Types.ObjectId, ref: "users" },
|
|
|
|
| 14 |
});
|
| 15 |
|
| 16 |
|
| 17 |
+
export type TemplateDocument = ITemplate & mongoose.Document;
|
| 18 |
|
| 19 |
+
export const Template = mongoose.model<TemplateDocument>(
|
| 20 |
+
"templates",
|
| 21 |
+
templateSchema
|
| 22 |
+
);
|
src/common/serializers/template.serialization.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Expose } from "class-transformer";
|
| 2 |
+
|
| 3 |
+
export class TemplateSerialization {
|
| 4 |
+
@Expose({ name: "_id" })
|
| 5 |
+
id: string;
|
| 6 |
+
|
| 7 |
+
@Expose()
|
| 8 |
+
name: string;
|
| 9 |
+
|
| 10 |
+
@Expose()
|
| 11 |
+
user: string;
|
| 12 |
+
|
| 13 |
+
@Expose()
|
| 14 |
+
exercises: string[];
|
| 15 |
+
}
|
src/modules/users/modules/templates/controllers/templates.controller.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { TemplateService } from "../services/templates.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, bodyValidator } from "@helpers/validation.helper";
|
| 7 |
+
import { BaseController } from "@lib/controllers/controller.base";
|
| 8 |
+
import { Controller } from "@lib/decorators/prefix.decorator";
|
| 9 |
+
import { serialize } from "@helpers/serialize";
|
| 10 |
+
import { TemplateSerialization } from "@common/serializers/template.serialization";
|
| 11 |
+
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
| 12 |
+
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";
|
| 13 |
+
import {
|
| 14 |
+
SwaggerGet,
|
| 15 |
+
SwaggerPost,
|
| 16 |
+
} from "@lib/decorators/swagger-routes.decorator";
|
| 17 |
+
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
| 18 |
+
import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";
|
| 19 |
+
import { createTemplatesSchema } from "../validations/create-templates.validation";
|
| 20 |
+
import { updateTemplatesSchema } from "../validations/update-templates.validation";
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
interface userRequest extends Request {
|
| 24 |
+
jwtPayload?: any;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
@Controller("/user/templates")
|
| 28 |
+
@ControllerMiddleware(UsersGuardMiddleware())
|
| 29 |
+
export class templateController extends BaseController {
|
| 30 |
+
private templatesService = new TemplateService();
|
| 31 |
+
|
| 32 |
+
setRoutes(): void {
|
| 33 |
+
this.router.get("/", asyncHandler(this.list));
|
| 34 |
+
this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
|
| 35 |
+
this.router.post(
|
| 36 |
+
"/",
|
| 37 |
+
bodyValidator(createTemplatesSchema),
|
| 38 |
+
asyncHandler(this.create)
|
| 39 |
+
);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
@SwaggerGet()
|
| 43 |
+
@SwaggerResponse([TemplateSerialization])
|
| 44 |
+
list = async (req: userRequest, res: Response): Promise<Response> => {
|
| 45 |
+
const paginationQuery = parsePaginationQuery(req.query);
|
| 46 |
+
const { docs, paginationData } = await this.templatesService.list(
|
| 47 |
+
{ user: req.jwtPayload.id },
|
| 48 |
+
paginationQuery
|
| 49 |
+
);
|
| 50 |
+
|
| 51 |
+
return JsonResponse.success(
|
| 52 |
+
{
|
| 53 |
+
data: serialize(docs, TemplateSerialization),
|
| 54 |
+
meta: paginationData,
|
| 55 |
+
},
|
| 56 |
+
res
|
| 57 |
+
);
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
@SwaggerGet("/:id")
|
| 61 |
+
@SwaggerResponse(TemplateSerialization)
|
| 62 |
+
get = async (req: userRequest, res: Response): Promise<Response> => {
|
| 63 |
+
const data = await this.templatesService.findOneOrFail({
|
| 64 |
+
_id: req.params.id,
|
| 65 |
+
user: req.jwtPayload.id
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
return JsonResponse.success(
|
| 69 |
+
{
|
| 70 |
+
data: serialize(data, TemplateSerialization),
|
| 71 |
+
},
|
| 72 |
+
res
|
| 73 |
+
);
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
@SwaggerPost()
|
| 77 |
+
@SwaggerResponse(TemplateSerialization)
|
| 78 |
+
@SwaggerRequest(createTemplatesSchema)
|
| 79 |
+
create = async (req: userRequest, res: Response) => {
|
| 80 |
+
const data = await this.templatesService.create(req.body);
|
| 81 |
+
return JsonResponse.success(
|
| 82 |
+
{
|
| 83 |
+
status: 201,
|
| 84 |
+
data: serialize(data, TemplateSerialization),
|
| 85 |
+
},
|
| 86 |
+
res
|
| 87 |
+
);
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
}
|
src/modules/users/modules/templates/services/templates.service.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Template } from "@common/models/template.model";
|
| 2 |
+
import { CrudService } from "@lib/services/crud.service";
|
| 3 |
+
|
| 4 |
+
export class TemplateService extends CrudService(Template) {}
|
src/modules/users/modules/templates/validations/create-templates.validation.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as joi from "joi";
|
| 2 |
+
import { createSchema } from "@helpers/create-schema";
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
export interface ICreateTemplates {
|
| 6 |
+
name: string;
|
| 7 |
+
user: string;
|
| 8 |
+
exercises: string[];
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
export const createTemplatesSchema = createSchema<ICreateTemplates>({
|
| 12 |
+
name: joi.string().empty().required().messages({
|
| 13 |
+
"string.base": "please enter a valid name",
|
| 14 |
+
"any.required": "name is required",
|
| 15 |
+
"string.empty": "name can not be empty",
|
| 16 |
+
}),
|
| 17 |
+
user: joi.string().empty().required().messages({
|
| 18 |
+
"string.base": "please enter a valid user id",
|
| 19 |
+
"any.required": "user id is required",
|
| 20 |
+
"string.empty": "user id can not be empty",
|
| 21 |
+
}),
|
| 22 |
+
exercises: joi.array().empty().required().items(
|
| 23 |
+
joi.string().empty().required().messages({
|
| 24 |
+
"string.base": "please enter a valid exercise id",
|
| 25 |
+
"any.required": "exercise id is required",
|
| 26 |
+
"string.empty": "exercise id can not be empty",
|
| 27 |
+
}),).messages({
|
| 28 |
+
"array.base": "please enter a valid exercises array",
|
| 29 |
+
"any.required": "exercises array is required",
|
| 30 |
+
"array.empty": "exercises array can not be empty",
|
| 31 |
+
}),
|
| 32 |
+
});
|
src/modules/users/modules/templates/validations/update-templates.validation.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as joi from "joi";
|
| 2 |
+
import { createSchema } from "@helpers/create-schema";
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
export interface IUpdateTemplates {
|
| 7 |
+
name?: string;
|
| 8 |
+
user?: string;
|
| 9 |
+
exercises?: string[];
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
export const updateTemplatesSchema = createSchema<IUpdateTemplates>({
|
| 14 |
+
name: joi.string().empty().optional().messages({
|
| 15 |
+
"string.base": "please enter a valid name",
|
| 16 |
+
"string.empty": "name can not be empty",
|
| 17 |
+
}),
|
| 18 |
+
user: joi.string().empty().optional().messages({
|
| 19 |
+
"string.base": "please enter a valid user id",
|
| 20 |
+
"string.empty": "user id can not be empty",
|
| 21 |
+
}),
|
| 22 |
+
exercises: joi.array().empty().optional().items(
|
| 23 |
+
joi.string().empty().optional().messages({
|
| 24 |
+
"string.base": "please enter a valid exercise id",
|
| 25 |
+
"string.empty": "exercise id can not be empty",
|
| 26 |
+
}),).messages({
|
| 27 |
+
"array.base": "please enter a valid exercises array",
|
| 28 |
+
"array.empty": "exercises array can not be empty",
|
| 29 |
+
}),
|
| 30 |
+
});
|