mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +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 { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { SpaceModelEntity } from './space-model.entity';
|
import { SpaceModelEntity } from './space-model.entity';
|
||||||
import { SubSpaceModelDto } from '../dtos';
|
import { SubSpaceModelDto } from '../dtos';
|
||||||
|
|
||||||
@Entity({ name: 'subspace-model' })
|
@Entity({ name: 'subspace-model' })
|
||||||
|
@Unique(['subspaceName', 'spaceModel'])
|
||||||
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
|
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
|
||||||
@Column({
|
@Column({
|
||||||
type: 'uuid',
|
type: 'uuid',
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
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 { Type } from 'class-transformer';
|
||||||
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
|
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
|
||||||
|
|
||||||
@ -17,6 +23,7 @@ export class CreateSpaceProductModelDto {
|
|||||||
example: 3,
|
example: 3,
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
@IsInt()
|
||||||
productCount: number;
|
productCount: number;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
|
@ -36,7 +36,7 @@ export class SpaceModelService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const spaceModel = await this.spaceModelRepository.create({
|
const spaceModel = this.spaceModelRepository.create({
|
||||||
modelName,
|
modelName,
|
||||||
project,
|
project,
|
||||||
});
|
});
|
||||||
|
@ -24,6 +24,10 @@ export class SpaceProductItemModelService {
|
|||||||
await this.create(itemModelDto, spaceProductModel, spaceModel);
|
await this.create(itemModelDto, spaceProductModel, spaceModel);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || `An unexpected error occurred`,
|
error.message || `An unexpected error occurred`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
@ -24,6 +24,10 @@ export class SpaceProductModelService {
|
|||||||
await this.create(spaceProductModelDto, spaceModel);
|
await this.create(spaceProductModelDto, spaceModel);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || `An unexpected error occurred`,
|
error.message || `An unexpected error occurred`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
@ -16,6 +16,7 @@ export class SubSpaceModelService {
|
|||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
this.validateInputDtos(subSpaceModelDtos);
|
||||||
for (const subspaceDto of subSpaceModelDtos) {
|
for (const subspaceDto of subSpaceModelDtos) {
|
||||||
const subspace = this.subspaceModelRepository.create({
|
const subspace = this.subspaceModelRepository.create({
|
||||||
subspaceName: subspaceDto.subspaceName,
|
subspaceName: subspaceDto.subspaceName,
|
||||||
@ -24,10 +25,46 @@ export class SubSpaceModelService {
|
|||||||
await this.subspaceModelRepository.save(subspace);
|
await this.subspaceModelRepository.save(subspace);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || `An unexpected error occurred`,
|
error.message || `An unexpected error occurred`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
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