Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rcole1919 committed Oct 29, 2024
1 parent ae1d34c commit 5edb80c
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 204 deletions.
8 changes: 8 additions & 0 deletions src/rest/middlewares/document-exists.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export class DocumentExistsMiddleware implements IMiddleware {

public async execute({ params }: Request, _res: Response, next: NextFunction): Promise<void> {
const documentId = params[this.paramName];
if (!documentId) {
throw new HttpError(
StatusCodes.BAD_REQUEST,
`${documentId} is not defined`,
'DocumentExistsMiddleware'
);
}

if (! await this.service.exists(documentId)) {
throw new HttpError(
StatusCodes.NOT_FOUND,
Expand Down
2 changes: 1 addition & 1 deletion src/rest/middlewares/validate-dto.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ValidateDTOMiddleware implements IMiddleware {
const dtoInstance = plainToInstance(this.dto, body);
const errors = await validate(dtoInstance);

if (errors.length > 0) {
if (errors.length) {
res.status(StatusCodes.BAD_REQUEST).send(errors);
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/shared/modules/comment/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { inject, injectable } from 'inversify';
import { Request, Response } from 'express';
// import { StatusCodes } from 'http-status-codes';

import {
BaseController,
Expand Down
3 changes: 2 additions & 1 deletion src/shared/modules/comment/comment.http
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Content-Type: application/json

{
"text": "asddfsdasc 23dfsdfsdf sdfs",
"authorId": "66f947e7e706754fb39b93a7"
"authorId": "66f947e7e706754fb39b93a7",
"rating": 4
}

###
4 changes: 1 addition & 3 deletions src/shared/modules/comment/default-comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { Types } from 'mongoose';

import { ICommentService } from './types/index.js';
import { CommentEntity, CreateCommentDTO } from './index.js';
import { COMMENT_RATING, COMPONENT, DEFAULT_COMMENTS_COUNT } from '../../constants/index.js';
import { COMPONENT, DEFAULT_COMMENTS_COUNT } from '../../constants/index.js';
import { ILogger } from '../../libs/logger/types/index.js';
import { ESortType } from '../../types/sort-type.enum.js';
import { getRandomNumber } from '../../helpers/index.js';

@injectable()
export class DefaultCommentService implements ICommentService {
Expand All @@ -20,7 +19,6 @@ export class DefaultCommentService implements ICommentService {
const result = await this.commentModel.create({
...dto,
offerId,
rating: getRandomNumber(COMMENT_RATING.MIN, COMMENT_RATING.MAX),
});
this.logger.info(`New comment created: ${dto.text}`);

Expand Down
17 changes: 10 additions & 7 deletions src/shared/modules/comment/dto/create-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { IsMongoId, MinLength, MaxLength, IsString } from 'class-validator';
import { IsMongoId, IsString, IsInt, Min, Max, Length } from 'class-validator';

import { COMMENT_TEXT_LENGTH } from '../../../constants/index.js';
import { CreateCommentValidationMessage } from './create-comment.message.js';
import { COMMENT_RATING, COMMENT_TEXT_LENGTH } from '../../../constants/index.js';

export class CreateCommentDTO {
@IsString({ message: CreateCommentValidationMessage.text.invalidFormat })
@MinLength(COMMENT_TEXT_LENGTH.MIN, { message: CreateCommentValidationMessage.text.minLength })
@MaxLength(COMMENT_TEXT_LENGTH.MAX, { message: CreateCommentValidationMessage.text.maxLength })
@IsString()
@Length(COMMENT_TEXT_LENGTH.MIN, COMMENT_TEXT_LENGTH.MAX)
public text!: string;

@IsMongoId({ message: CreateCommentValidationMessage.authorId.invalid })
@IsMongoId()
public authorId!: string;

@IsInt()
@Min(COMMENT_RATING.MIN)
@Max(COMMENT_RATING.MAX)
public rating!: number;
}
12 changes: 0 additions & 12 deletions src/shared/modules/comment/dto/create-comment.message.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/shared/modules/offer/default-offer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class DefaultOfferService implements IOfferService {
) {}

public async exists(documentId: string): Promise<boolean> {
return await this.offerModel
.exists({_id: documentId}) !== null;
return this.offerModel
.exists({_id: documentId}).then(((resolve) => !!resolve));
}

public async create(dto: CreateOfferDTO): Promise<DocumentType<OfferEntity>> {
Expand Down
7 changes: 3 additions & 4 deletions src/shared/modules/offer/dto/coords.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { IsInt } from 'class-validator';
import { OfferValidationMessage } from './offer.message.js';
import { IsNumber } from 'class-validator';

export class CoordsDTO {
@IsInt({ message: OfferValidationMessage.coords.latitude.invalidFormat })
@IsNumber()
public latitude!: number;

@IsInt({ message: OfferValidationMessage.coords.longitude.invalidFormat })
@IsNumber()
public longitude!: number;
}
54 changes: 25 additions & 29 deletions src/shared/modules/offer/dto/create-offer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
ArrayUnique,
ArrayMinSize,
ArrayMaxSize,
MinLength,
MaxLength,
Min,
Max,
ValidateNested,
Length,
} from 'class-validator';
import { Type } from 'class-transformer';

Expand All @@ -26,61 +25,58 @@ import {
VISITORS_NUMBER,
PRICE,
} from '../../../constants/index.js';
import { OfferValidationMessage } from './offer.message.js';
import { CoordsDTO } from './coords.dto.js';

export class CreateOfferDTO {
@IsString({ message: OfferValidationMessage.title.invalidFormat })
@MinLength(OFFER_TITLE_LENGTH.MIN, { message: OfferValidationMessage.title.minLength })
@MaxLength(OFFER_TITLE_LENGTH.MAX, { message: OfferValidationMessage.title.maxLength })
@IsString()
@Length(OFFER_TITLE_LENGTH.MIN, OFFER_TITLE_LENGTH.MAX)
public title!: string;

@IsString({ message: OfferValidationMessage.description.invalidFormat })
@MinLength(OFFER_DESCRIPTION_LENGTH.MIN, { message: OfferValidationMessage.description.minLength })
@MaxLength(OFFER_DESCRIPTION_LENGTH.MAX, { message: OfferValidationMessage.description.maxLength })
@IsString()
@Length(OFFER_DESCRIPTION_LENGTH.MIN, OFFER_DESCRIPTION_LENGTH.MAX)
public description!: string;

@IsEnum(ECity, { message: OfferValidationMessage.city.invalid })
@IsEnum(ECity)
public city!: string;

@IsString({ message: OfferValidationMessage.previewImagePath.invalidFormat })
@IsString()
public previewImagePath!: string;

@IsArray({ message: OfferValidationMessage.photos.invalidFormat })
@ArrayMinSize(PHOTOS_LENGTH, { message: OfferValidationMessage.photos.invalidLength })
@ArrayMaxSize(PHOTOS_LENGTH, { message: OfferValidationMessage.photos.invalidLength })
@IsArray()
@ArrayMinSize(PHOTOS_LENGTH)
@ArrayMaxSize(PHOTOS_LENGTH)
public photos!: string[];

@IsBoolean({ message: OfferValidationMessage.isPremium.invalidFormat })
@IsBoolean()
public isPremium!: boolean;

@IsEnum(EHousing, { message: OfferValidationMessage.housingType.invalid })
@IsEnum(EHousing)
public housingType!: EHousing;

@IsInt({ message: OfferValidationMessage.roomsNumber.invalidFormat })
@Min(ROOMS_NUMBER.MIN, { message: OfferValidationMessage.roomsNumber.min })
@Max(ROOMS_NUMBER.MAX, { message: OfferValidationMessage.roomsNumber.max })
@IsInt()
@Min(ROOMS_NUMBER.MIN)
@Max(ROOMS_NUMBER.MAX)
public roomsNumber!: number;

@IsInt({ message: OfferValidationMessage.visitorsNumber.invalidFormat })
@Min(VISITORS_NUMBER.MIN, { message: OfferValidationMessage.visitorsNumber.min })
@Max(VISITORS_NUMBER.MAX, { message: OfferValidationMessage.visitorsNumber.max })
@IsInt()
@Min(VISITORS_NUMBER.MIN)
@Max(VISITORS_NUMBER.MAX)
public visitorsNumber!: number;

@IsInt({ message: OfferValidationMessage.price.invalidFormat })
@Min(PRICE.MIN, { message: OfferValidationMessage.price.min })
@Max(PRICE.MAX, { message: OfferValidationMessage.price.max })
@IsInt()
@Min(PRICE.MIN)
@Max(PRICE.MAX)
public price!: number;

@IsArray({ message: OfferValidationMessage.facilities.invalidFormat })
@ArrayUnique<EFacilities>({message: OfferValidationMessage.facilities.invalid})
@IsArray()
@ArrayUnique<EFacilities>()
public facilities!: EFacilities[];

@IsMongoId({ message: OfferValidationMessage.authorId.invalid })
@IsMongoId()
public authorId!: string;

@ValidateNested()
@IsObject({ message: OfferValidationMessage.coords.invalidFormat })
@IsObject()
@Type(() => CoordsDTO)
public coords!: CoordsDTO;
}
67 changes: 0 additions & 67 deletions src/shared/modules/offer/dto/offer.message.ts

This file was deleted.

52 changes: 24 additions & 28 deletions src/shared/modules/offer/dto/update-offer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
ArrayUnique,
ArrayMinSize,
ArrayMaxSize,
MinLength,
MaxLength,
Min,
Max,
Length,
ValidateNested,
IsOptional,
} from 'class-validator';
Expand All @@ -26,71 +25,68 @@ import {
VISITORS_NUMBER,
PRICE,
} from '../../../constants/index.js';
import { OfferValidationMessage } from './offer.message.js';
import { CoordsDTO } from './coords.dto.js';


export class UpdateOfferDTO {
@IsOptional()
@IsString({ message: OfferValidationMessage.title.invalidFormat })
@MinLength(OFFER_TITLE_LENGTH.MIN, { message: OfferValidationMessage.title.minLength })
@MaxLength(OFFER_TITLE_LENGTH.MAX, { message: OfferValidationMessage.title.maxLength })
@IsString()
@Length(OFFER_TITLE_LENGTH.MIN, OFFER_TITLE_LENGTH.MAX)
public title?: string;

@IsOptional()
@IsString({ message: OfferValidationMessage.description.invalidFormat })
@MinLength(OFFER_DESCRIPTION_LENGTH.MIN, { message: OfferValidationMessage.description.minLength })
@MaxLength(OFFER_DESCRIPTION_LENGTH.MAX, { message: OfferValidationMessage.description.maxLength })
@IsString()
@Length(OFFER_DESCRIPTION_LENGTH.MIN, OFFER_DESCRIPTION_LENGTH.MAX)
public description?: string;

@IsOptional()
@IsEnum(ECity, { message: OfferValidationMessage.city.invalid })
@IsEnum(ECity)
public city?: string;

@IsOptional()
@IsString({ message: OfferValidationMessage.previewImagePath.invalidFormat })
@IsString()
public previewImagePath?: string;

@IsOptional()
@IsArray({ message: OfferValidationMessage.photos.invalidFormat })
@ArrayMinSize(PHOTOS_LENGTH, { message: OfferValidationMessage.photos.invalidLength })
@ArrayMaxSize(PHOTOS_LENGTH, { message: OfferValidationMessage.photos.invalidLength })
@IsArray()
@ArrayMinSize(PHOTOS_LENGTH)
@ArrayMaxSize(PHOTOS_LENGTH)
public photos?: string[];

@IsOptional()
@IsBoolean({ message: OfferValidationMessage.isPremium.invalidFormat })
@IsBoolean()
public isPremium?: boolean;

@IsOptional()
@IsEnum(EHousing, { message: OfferValidationMessage.housingType.invalid })
@IsEnum(EHousing)
public housingType?: EHousing;

@IsOptional()
@IsInt({ message: OfferValidationMessage.roomsNumber.invalidFormat })
@Min(ROOMS_NUMBER.MIN, { message: OfferValidationMessage.roomsNumber.min })
@Max(ROOMS_NUMBER.MAX, { message: OfferValidationMessage.roomsNumber.max })
@IsInt()
@Min(ROOMS_NUMBER.MIN)
@Max(ROOMS_NUMBER.MAX)
public roomsNumber?: number;

@IsOptional()
@IsInt({ message: OfferValidationMessage.visitorsNumber.invalidFormat })
@Min(VISITORS_NUMBER.MIN, { message: OfferValidationMessage.visitorsNumber.min })
@Max(VISITORS_NUMBER.MAX, { message: OfferValidationMessage.visitorsNumber.max })
@IsInt()
@Min(VISITORS_NUMBER.MIN)
@Max(VISITORS_NUMBER.MAX)
public visitorsNumber?: number;

@IsOptional()
@IsInt({ message: OfferValidationMessage.price.invalidFormat })
@Min(PRICE.MIN, { message: OfferValidationMessage.price.min })
@Max(PRICE.MAX, { message: OfferValidationMessage.price.max })
@IsInt()
@Min(PRICE.MIN)
@Max(PRICE.MAX)
public price?: number;

@IsOptional()
@IsArray({ message: OfferValidationMessage.facilities.invalidFormat })
@ArrayUnique<EFacilities>({message: OfferValidationMessage.facilities.invalid})
@IsArray()
@ArrayUnique<EFacilities>()
public facilities?: EFacilities[];

@IsOptional()
@ValidateNested()
@IsObject({ message: OfferValidationMessage.coords.invalidFormat })
@IsObject()
@Type(() => CoordsDTO)
public coords?: CoordsDTO;
}
Loading

0 comments on commit 5edb80c

Please sign in to comment.