mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
added unique subspace name
This commit is contained in:
@ -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',
|
||||
|
@ -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({
|
||||
|
@ -36,7 +36,7 @@ export class SpaceModelService {
|
||||
);
|
||||
}
|
||||
|
||||
const spaceModel = await this.spaceModelRepository.create({
|
||||
const spaceModel = this.spaceModelRepository.create({
|
||||
modelName,
|
||||
project,
|
||||
});
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user