mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 16:02:40 +00:00
refactor; create ad request validation
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
import { AutoMap } from '@automapper/classes';
|
||||
import { IsBoolean, IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { PointType } from '../../../geography/domain/types/point-type.enum';
|
||||
import { Frequency } from '../types/frequency.enum';
|
||||
import { Point } from '../../../geography/domain/types/point.type';
|
||||
|
||||
export class CreateAdRequest {
|
||||
@IsString()
|
||||
@@ -15,9 +28,10 @@ export class CreateAdRequest {
|
||||
@AutoMap()
|
||||
passenger: boolean;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
@IsEnum(Frequency)
|
||||
@AutoMap()
|
||||
frequency: number;
|
||||
frequency: Frequency;
|
||||
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
@@ -27,33 +41,40 @@ export class CreateAdRequest {
|
||||
@AutoMap()
|
||||
toDate: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
monTime: string;
|
||||
monTime: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
tueTime: string;
|
||||
tueTime: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
wedTime: string;
|
||||
wedTime: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
thuTime: string;
|
||||
thuTime!: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
friTime: string;
|
||||
friTime: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
satTime: string;
|
||||
satTime: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@AutoMap()
|
||||
sunTime: string;
|
||||
sunTime: string | null;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
@@ -83,16 +104,19 @@ export class CreateAdRequest {
|
||||
@AutoMap()
|
||||
sunMargin: number;
|
||||
|
||||
@IsNumber()
|
||||
@IsEnum(PointType)
|
||||
@AutoMap()
|
||||
originType: number;
|
||||
originType: PointType;
|
||||
|
||||
@IsNumber()
|
||||
@IsEnum(PointType)
|
||||
@AutoMap()
|
||||
destinationType: number;
|
||||
destinationType: PointType;
|
||||
|
||||
@IsArray()
|
||||
@ArrayMinSize(2)
|
||||
@ValidateNested({ each: true })
|
||||
@AutoMap()
|
||||
waypoints: [];
|
||||
waypoints: Array<Point>;
|
||||
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
@@ -102,6 +126,7 @@ export class CreateAdRequest {
|
||||
@AutoMap()
|
||||
seatsPassenger: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@AutoMap()
|
||||
seatsUsed: number;
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { AutoMap } from '@automapper/classes';
|
||||
import { ArrayMinSize, IsArray, IsEnum, ValidateNested } from 'class-validator';
|
||||
import { PointType } from '../../../geography/domain/types/point-type.enum';
|
||||
import { Point } from '../../../geography/domain/types/point.type';
|
||||
|
||||
export class Ad {
|
||||
@AutoMap()
|
||||
@@ -14,10 +17,10 @@ export class Ad {
|
||||
frequency: number;
|
||||
|
||||
@AutoMap()
|
||||
fromDate: string;
|
||||
fromDate: Date;
|
||||
|
||||
@AutoMap()
|
||||
toDate: string;
|
||||
toDate: Date;
|
||||
|
||||
@AutoMap()
|
||||
monTime: string;
|
||||
@@ -73,14 +76,19 @@ export class Ad {
|
||||
@AutoMap()
|
||||
passengerDistance: number;
|
||||
|
||||
@IsEnum(PointType)
|
||||
@AutoMap()
|
||||
originType: number;
|
||||
originType: PointType;
|
||||
|
||||
@IsEnum(PointType)
|
||||
@AutoMap()
|
||||
destinationType: number;
|
||||
destinationType: PointType;
|
||||
|
||||
@IsArray()
|
||||
@ArrayMinSize(2)
|
||||
@ValidateNested({ each: true })
|
||||
@AutoMap()
|
||||
waypoints: [];
|
||||
waypoints: Array<Point>;
|
||||
|
||||
@AutoMap()
|
||||
direction: string;
|
||||
@@ -101,8 +109,8 @@ export class Ad {
|
||||
seatsUsed: number;
|
||||
|
||||
@AutoMap()
|
||||
createdAt: string;
|
||||
createdAt: Date;
|
||||
|
||||
@AutoMap()
|
||||
updatedAt: string;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
4
src/modules/ad/domain/types/frequency.enum.ts
Normal file
4
src/modules/ad/domain/types/frequency.enum.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum Frequency {
|
||||
PUNCTUAL = 1,
|
||||
RECURRENT = 2,
|
||||
}
|
||||
@@ -2,14 +2,26 @@ import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { CreateAdCommand } from '../../commands/create-ad.command';
|
||||
import { Ad } from '../entities/ad';
|
||||
import { AdRepository } from '../../adapters/secondaries/ad.repository';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { CreateAdRequest } from '../dtos/create-ad.request';
|
||||
|
||||
@CommandHandler(CreateAdCommand)
|
||||
export class CreateAdUseCase {
|
||||
constructor(private readonly adRepository: AdRepository) {}
|
||||
constructor(
|
||||
@InjectMapper() private readonly mapper: Mapper,
|
||||
private readonly adRepository: AdRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: CreateAdCommand): Promise<Ad> {
|
||||
try {
|
||||
return await this.adRepository.createAd(command.createAdRequest);
|
||||
const adToCreate: Ad = this.mapper.map(
|
||||
command.createAdRequest,
|
||||
CreateAdRequest,
|
||||
Ad,
|
||||
);
|
||||
return adToCreate;
|
||||
// return await this.adRepository.createAd(adToCreate);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user