added edit space module endpoint

This commit is contained in:
hannathkadher
2024-12-18 15:00:34 +04:00
parent 7833151b70
commit 85aa64ac36
8 changed files with 103 additions and 17 deletions
+49 -15
View File
@@ -1,7 +1,9 @@
import { SpaceModelRepository } from '@app/common/modules/space-model';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSpaceModelDto } from '../dtos';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateSpaceModelDto, UpdateSpaceModelDto } from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { SubSpaceModelService } from './subspace/subspace-model.service';
@@ -13,13 +15,16 @@ import {
} from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
import { SpaceModelParam } from '../dtos/space-model-param';
import { ProjectService } from 'src/project/services';
import { ProjectEntity } from '@app/common/modules/project/entities';
@Injectable()
export class SpaceModelService {
constructor(
private readonly dataSource: DataSource,
private readonly spaceModelRepository: SpaceModelRepository,
private readonly projectRepository: ProjectRepository,
private readonly projectService: ProjectService,
private readonly subSpaceModelService: SubSpaceModelService,
private readonly spaceProductModelService: SpaceProductModelService,
) {}
@@ -44,7 +49,7 @@ export class SpaceModelService {
);
if (isModelExist) {
throw new HttpException(
`Model name "${modelName}" already exists in this project ${project.name}.`,
`Model name "${modelName}" already exists in this project ${params.projectUuid}.`,
HttpStatus.CONFLICT,
);
}
@@ -125,20 +130,37 @@ export class SpaceModelService {
}
}
async validateProject(projectUuid: string) {
const project = await this.projectRepository.findOne({
where: {
uuid: projectUuid,
},
});
async validateProject(projectUuid: string): Promise<ProjectEntity> {
return await this.projectService.findOne(projectUuid);
}
if (!project) {
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
await this.validateProject(param.projectUuid);
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const { modelName } = dto;
if (modelName) spaceModel.modelName = modelName;
await queryRunner.manager.save(spaceModel);
await queryRunner.commitTransaction();
return new SuccessResponseDto({
message: 'SpaceModel updated successfully',
data: spaceModel,
});
} catch (error) {
await queryRunner.rollbackTransaction();
throw new HttpException(
`Project with uuid ${projectUuid} not found`,
HttpStatus.NOT_FOUND,
error.message || 'Failed to update SpaceModel',
HttpStatus.INTERNAL_SERVER_ERROR,
);
} finally {
await queryRunner.release();
}
return project;
}
async validateName(modelName: string, projectUuid: string): Promise<boolean> {
@@ -147,4 +169,16 @@ export class SpaceModelService {
});
return isModelExist;
}
async validateSpaceModel(uuid: string): Promise<SpaceModelEntity> {
const spaceModel = await this.spaceModelRepository.findOne({
where: {
uuid,
},
});
if (!spaceModel) {
throw new HttpException('space model not found', HttpStatus.NOT_FOUND);
}
return spaceModel;
}
}