From 73fed94b4e084b904059472a5772406eca2097ad Mon Sep 17 00:00:00 2001 From: AdonaiJehosua Date: Tue, 5 Nov 2024 00:00:36 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20cre?= =?UTF-8?q?ateOffer,=20=D0=BE=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B5=D0=B2=20=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/comment/comment.controller.ts | 17 ++++++++++++++--- .../modules/comment/dto/create-comment.dto.ts | 15 +++++++++++++++ .../comment/dto/create-comment.messages.ts | 18 ++++++++++++++++++ .../modules/offer/dto/create-offer.dto.ts | 6 +++--- .../offer/dto/create-offer.messages.ts | 6 +++--- src/shared/modules/offer/offer.http | 2 +- .../modules/user/dto/create-user.dto.ts | 16 +++++++++++++++- .../modules/user/dto/create-user.messages.ts | 19 +++++++++++++++++++ src/shared/modules/user/dto/login-user.dto.ts | 7 +++++++ .../modules/user/dto/login-user.messages.ts | 8 ++++++++ src/shared/modules/user/user.controller.ts | 18 +++++++++++++++--- 11 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 src/shared/modules/comment/dto/create-comment.messages.ts create mode 100644 src/shared/modules/user/dto/create-user.messages.ts create mode 100644 src/shared/modules/user/dto/login-user.messages.ts diff --git a/src/shared/modules/comment/comment.controller.ts b/src/shared/modules/comment/comment.controller.ts index 09bfcfe..2bf7ddc 100644 --- a/src/shared/modules/comment/comment.controller.ts +++ b/src/shared/modules/comment/comment.controller.ts @@ -1,12 +1,13 @@ import {inject, injectable} from 'inversify'; import {Request, Response} from 'express'; -import {BaseController, HttpMethod, ValidateObjectIdMiddleware} from '../../libs/rest/index.js'; +import {BaseController, HttpMethod, ValidateDtoMiddleware, ValidateObjectIdMiddleware} from '../../libs/rest/index.js'; import {Logger} from '../../libs/logger/index.js'; import {Component} from '../../types/index.js'; import {CommentService} from './comment-service.interface.js'; import {CommentRdo} from './rdo/comment.rdo.js'; import {fillDTO} from '../../helpers/index.js'; import {CreateCommentDto} from './dto/create-comment.dto.js'; +import {CreateOfferDto} from '../offer/index.js'; @injectable() export class CommentController extends BaseController { @@ -18,8 +19,18 @@ export class CommentController extends BaseController { this.logger.info('Register routes for CommentController…'); - this.addRoute({path: '/:offerId', method: HttpMethod.Get, handler: this.index, middlewares: [new ValidateObjectIdMiddleware('offerId')]}); - this.addRoute({path: '/:offerId', method: HttpMethod.Post, handler: this.create, middlewares: [new ValidateObjectIdMiddleware('offerId')]}); + this.addRoute({ + path: '/:offerId', + method: HttpMethod.Get, + handler: this.index, + middlewares: [new ValidateObjectIdMiddleware('offerId')] + }); + this.addRoute({ + path: '/:offerId', + method: HttpMethod.Post, + handler: this.create, + middlewares: [new ValidateObjectIdMiddleware('offerId'), new ValidateDtoMiddleware(CreateOfferDto)] + }); } public async index(req: Request, res: Response): Promise { diff --git a/src/shared/modules/comment/dto/create-comment.dto.ts b/src/shared/modules/comment/dto/create-comment.dto.ts index 7edbb4c..6f04760 100644 --- a/src/shared/modules/comment/dto/create-comment.dto.ts +++ b/src/shared/modules/comment/dto/create-comment.dto.ts @@ -1,6 +1,21 @@ +import {IsMongoId, IsNumber, IsString, Max, MaxLength, Min, MinLength} from 'class-validator'; + +import {CreateCommentValidationMessages} from './create-comment.messages.js'; + export class CreateCommentDto { + @IsString({message: CreateCommentValidationMessages.text.invalidFormat}) + @MinLength(10, {message: CreateCommentValidationMessages.text.minLength}) + @MaxLength(100, {message: CreateCommentValidationMessages.text.maxLength}) public text: string; + + @IsNumber({maxDecimalPlaces: 1}, {message: CreateCommentValidationMessages.rating.invalidFormat}) + @Min(1, {message: CreateCommentValidationMessages.rating.minValue}) + @Max(5, {message: CreateCommentValidationMessages.rating.maxValue}) public rating: number; + + @IsMongoId({message: CreateCommentValidationMessages.offerId.invalidId}) public authorId: string; + + @IsMongoId({message: CreateCommentValidationMessages.offerId.invalidId}) public offerId: string; } diff --git a/src/shared/modules/comment/dto/create-comment.messages.ts b/src/shared/modules/comment/dto/create-comment.messages.ts new file mode 100644 index 0000000..61eea58 --- /dev/null +++ b/src/shared/modules/comment/dto/create-comment.messages.ts @@ -0,0 +1,18 @@ +export const CreateCommentValidationMessages = { + text: { + invalidFormat: 'text must be a string', + minLength: 'minimum text length must be 5 characters', + maxLength: 'maximum text length must be 1024 characters', + }, + rating: { + invalidFormat: 'rating must be a number with maximum 1 decimal place', + minValue: 'minimum rating is 1', + maxValue: 'maximum rating is 5', + }, + authorId: { + invalidId: 'author id field must be a valid id', + }, + offerId: { + invalidId: 'offer id field must be a valid id', + } +} as const; diff --git a/src/shared/modules/offer/dto/create-offer.dto.ts b/src/shared/modules/offer/dto/create-offer.dto.ts index ff67bf2..0ce7cb6 100644 --- a/src/shared/modules/offer/dto/create-offer.dto.ts +++ b/src/shared/modules/offer/dto/create-offer.dto.ts @@ -20,7 +20,7 @@ import {CityAndLocationValidationMessages} from './city-and-location.messages.js import {CityValidate, LocationValidate} from './city-and-location.dto.js'; export class CreateOfferDto { - @IsString({message: CreateOfferValidationMessage.title.minLength}) + @IsString({message: CreateOfferValidationMessage.title.invalidFormat}) @MinLength(10, {message: CreateOfferValidationMessage.title.minLength}) @MaxLength(100, {message: CreateOfferValidationMessage.title.maxLength}) public title: string; @@ -48,8 +48,8 @@ export class CreateOfferDto { public isPremium: boolean; @IsNumber({maxDecimalPlaces: 1}, {message: CreateOfferValidationMessage.rating.invalidFormat}) - @Min(1, {message: CreateOfferValidationMessage.price.minValue}) - @Max(5, {message: CreateOfferValidationMessage.price.maxValue}) + @Min(1, {message: CreateOfferValidationMessage.rating.minValue}) + @Max(5, {message: CreateOfferValidationMessage.rating.maxValue}) public rating: number; @IsString({message: CreateOfferValidationMessage.description.invalidFormat}) diff --git a/src/shared/modules/offer/dto/create-offer.messages.ts b/src/shared/modules/offer/dto/create-offer.messages.ts index dd6e0b3..7c1b23f 100644 --- a/src/shared/modules/offer/dto/create-offer.messages.ts +++ b/src/shared/modules/offer/dto/create-offer.messages.ts @@ -20,8 +20,8 @@ export const CreateOfferValidationMessage = { }, rating: { invalidFormat: 'rating must be a number with maximum 1 decimal place', - minValue: 'minimum price is 100', - maxValue: 'maximum price is 200000', + minValue: 'minimum rating is 1', + maxValue: 'maximum rating is 5', }, description: { invalidFormat: 'description must be a string', @@ -34,7 +34,7 @@ export const CreateOfferValidationMessage = { invalidItem: 'items of images array must be a string type' }, image: { - invalidFormat: 'image must be a string type', + invalidFormat: 'image must be a string', }, offerGood: { invalid: 'offerGood must be an array', diff --git a/src/shared/modules/offer/offer.http b/src/shared/modules/offer/offer.http index 420d222..31ec5a0 100644 --- a/src/shared/modules/offer/offer.http +++ b/src/shared/modules/offer/offer.http @@ -59,7 +59,7 @@ PATCH http://localhost:4000/offers/6717e369c934030b401bf05e HTTP/1.1 Content-Type: application/json { - "title": "Vovovo" + "title": "Vovovfdfjhdfjo" } ### diff --git a/src/shared/modules/user/dto/create-user.dto.ts b/src/shared/modules/user/dto/create-user.dto.ts index 579cb58..e08a85a 100644 --- a/src/shared/modules/user/dto/create-user.dto.ts +++ b/src/shared/modules/user/dto/create-user.dto.ts @@ -1,8 +1,22 @@ +import {IsBoolean, IsEmail, IsString, MaxLength, MinLength} from 'class-validator'; + +import {CreateUserValidationMessages} from './create-user.messages.js'; + export class CreateUserDto { + @IsString({message: CreateUserValidationMessages.name.invalidFormat}) + @MinLength(10, {message: CreateUserValidationMessages.name.minLength}) + @MaxLength(100, {message: CreateUserValidationMessages.name.maxLength}) public name: string; + + @IsString({message: CreateUserValidationMessages.avatarUrl.invalidFormat}) public avatarUrl: string; + + @IsBoolean({message: CreateUserValidationMessages.isPro.invalidFormat}) public isPro: boolean; + + @IsEmail({}, {message: CreateUserValidationMessages.email.invalidFormat}) public email: string; - public token: string; + + @IsString({message: CreateUserValidationMessages.email.invalidFormat}) public password: string; } diff --git a/src/shared/modules/user/dto/create-user.messages.ts b/src/shared/modules/user/dto/create-user.messages.ts new file mode 100644 index 0000000..5a756ed --- /dev/null +++ b/src/shared/modules/user/dto/create-user.messages.ts @@ -0,0 +1,19 @@ +export const CreateUserValidationMessages = { + name: { + invalidFormat: 'name must be a string', + minLength: 'minimum name length must be 1', + maxLength: 'maximum name length must be 15', + }, + avatarUrl: { + invalidFormat: 'avatar url must be a string', + }, + isPro: { + invalidFormat: 'isPro flag must be a boolean', + }, + email: { + invalidFormat: 'email email must be in the correct format', + }, + password: { + invalidFormat: 'password must be a string', + } +} as const; diff --git a/src/shared/modules/user/dto/login-user.dto.ts b/src/shared/modules/user/dto/login-user.dto.ts index 338ed08..cf7ef41 100644 --- a/src/shared/modules/user/dto/login-user.dto.ts +++ b/src/shared/modules/user/dto/login-user.dto.ts @@ -1,4 +1,11 @@ +import {IsEmail, IsString} from 'class-validator'; + +import {LoginUserValidationMessages} from './login-user.messages.js'; + export class LoginUserDto { + @IsEmail({}, {message: LoginUserValidationMessages.email.invalidFormat}) public email: string; + + @IsString({message: LoginUserValidationMessages.password.invalidFormat}) public password: string; } diff --git a/src/shared/modules/user/dto/login-user.messages.ts b/src/shared/modules/user/dto/login-user.messages.ts new file mode 100644 index 0000000..99d734b --- /dev/null +++ b/src/shared/modules/user/dto/login-user.messages.ts @@ -0,0 +1,8 @@ +export const LoginUserValidationMessages = { + email: { + invalidFormat: 'email email must be in the correct format' + }, + password: { + invalidFormat: 'password must be a string' + } +} as const; diff --git a/src/shared/modules/user/user.controller.ts b/src/shared/modules/user/user.controller.ts index 4a29b71..d1887b8 100644 --- a/src/shared/modules/user/user.controller.ts +++ b/src/shared/modules/user/user.controller.ts @@ -1,6 +1,6 @@ import {inject, injectable} from 'inversify'; import {Response} from 'express'; -import {BaseController, HttpError, HttpMethod} from '../../libs/rest/index.js'; +import {BaseController, HttpError, HttpMethod, ValidateDtoMiddleware} from '../../libs/rest/index.js'; import {Logger} from '../../libs/logger/index.js'; import {Component} from '../../types/index.js'; import {CreateUserRequest} from './create-user-request.type.js'; @@ -10,6 +10,8 @@ import {StatusCodes} from 'http-status-codes'; import {fillDTO} from '../../helpers/index.js'; import {UserRdo} from './rdo/user.rdo.js'; import {LoginUserRequest} from './login-user-request.type.js'; +import {CreateUserDto} from './dto/create-user.dto.js'; +import {LoginUserDto} from './dto/login-user.dto.js'; @injectable() export class UserController extends BaseController { @@ -21,8 +23,18 @@ export class UserController extends BaseController { super(logger); this.logger.info('Register routes for UserController...'); - this.addRoute({path: '/register', method: HttpMethod.Post, handler: this.create}); - this.addRoute({path: '/login', method: HttpMethod.Post, handler: this.login}); + this.addRoute({ + path: '/register', + method: HttpMethod.Post, + handler: this.create, + middlewares: [new ValidateDtoMiddleware(CreateUserDto)] + }); + this.addRoute({ + path: '/login', + method: HttpMethod.Post, + handler: this.login, + middlewares: [new ValidateDtoMiddleware(LoginUserDto)] + }); } public async create(