added unique subspace name

This commit is contained in:
hannathkadher
2024-12-11 11:46:30 +04:00
parent 02faca3397
commit 69d0065ee6
6 changed files with 56 additions and 3 deletions

View File

@ -1,9 +1,10 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceModelEntity } from './space-model.entity';
import { SubSpaceModelDto } from '../dtos';
@Entity({ name: 'subspace-model' })
@Unique(['subspaceName', 'spaceModel'])
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
@Column({
type: 'uuid',

View File

@ -1,5 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
import {
IsNotEmpty,
IsString,
IsArray,
ValidateNested,
IsInt,
} from 'class-validator';
import { Type } from 'class-transformer';
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
@ -17,6 +23,7 @@ export class CreateSpaceProductModelDto {
example: 3,
})
@IsNotEmpty()
@IsInt()
productCount: number;
@ApiProperty({

View File

@ -36,7 +36,7 @@ export class SpaceModelService {
);
}
const spaceModel = await this.spaceModelRepository.create({
const spaceModel = this.spaceModelRepository.create({
modelName,
project,
});

View File

@ -24,6 +24,10 @@ export class SpaceProductItemModelService {
await this.create(itemModelDto, spaceProductModel, spaceModel);
}
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,

View File

@ -24,6 +24,10 @@ export class SpaceProductModelService {
await this.create(spaceProductModelDto, spaceModel);
}
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,

View File

@ -16,6 +16,7 @@ export class SubSpaceModelService {
spaceModel: SpaceModelEntity,
) {
try {
this.validateInputDtos(subSpaceModelDtos);
for (const subspaceDto of subSpaceModelDtos) {
const subspace = this.subspaceModelRepository.create({
subspaceName: subspaceDto.subspaceName,
@ -24,10 +25,46 @@ export class SubSpaceModelService {
await this.subspaceModelRepository.save(subspace);
}
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private validateInputDtos(subSpaceModelDtos: CreateSubspaceModelDto[]) {
if (subSpaceModelDtos.length === 0) {
throw new HttpException(
'Subspace models cannot be empty.',
HttpStatus.BAD_REQUEST,
);
}
const incomingNames = subSpaceModelDtos.map((dto) => dto.subspaceName);
this.validateName(incomingNames);
}
private validateName(names: string[]) {
const seenNames = new Set<string>();
const duplicateNames = new Set<string>();
for (const name of names) {
if (seenNames.has(name)) {
duplicateNames.add(name);
} else {
seenNames.add(name);
}
}
if (duplicateNames.size > 0) {
throw new HttpException(
`Duplicate subspace names found in request: ${[...duplicateNames].join(', ')}`,
HttpStatus.CONFLICT,
);
}
}
}