From 768ea7cad8b709cab5979d2ae0e68f9791b333d8 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 9 Dec 2024 12:26:53 +0400 Subject: [PATCH] add update project --- src/project/services/project.service.ts | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/project/services/project.service.ts b/src/project/services/project.service.ts index 62070a2..6ba7e0c 100644 --- a/src/project/services/project.service.ts +++ b/src/project/services/project.service.ts @@ -128,6 +128,52 @@ export class ProjectService { } } + async updateProject( + uuid: string, + updateProjectDto: CreateProjectDto, + ): Promise { + try { + // Find the project by UUID + const project = await this.findOne(uuid); + + if (!project) { + throw new HttpException( + `Project with ID ${uuid} not found`, + HttpStatus.NOT_FOUND, + ); + } + + if (updateProjectDto.name && updateProjectDto.name !== project.name) { + const isNameExist = await this.validate(updateProjectDto.name); + if (isNameExist) { + throw new HttpException( + `Project with name ${updateProjectDto.name} already exists`, + HttpStatus.CONFLICT, + ); + } + } + + // Update the project details + Object.assign(project, updateProjectDto); + const updatedProject = await this.projectRepository.save(project); + + return new SuccessResponseDto({ + message: `Project with ID ${uuid} successfully updated`, + data: updatedProject, + statusCode: HttpStatus.OK, + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } + + throw new HttpException( + `An error occurred while updating the project with ID ${uuid}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + async findOne(uuid: string): Promise { const project = await this.projectRepository.findOne({ where: { uuid } }); return project;