mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Project service to CRUD project entity
This commit is contained in:
@ -16,6 +16,9 @@ const mappingInclude: { [key: string]: any } = {
|
||||
subspace: {
|
||||
subspace: true,
|
||||
},
|
||||
project: {
|
||||
project: true,
|
||||
},
|
||||
};
|
||||
|
||||
export function buildTypeORMIncludeQuery(
|
||||
|
1
src/project/services/index.ts
Normal file
1
src/project/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './project.service';
|
140
src/project/services/project.service.ts
Normal file
140
src/project/services/project.service.ts
Normal file
@ -0,0 +1,140 @@
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateProjectDto } from '../dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
TypeORMCustomModelFindAllQuery,
|
||||
} from '@app/common/models/typeOrmCustom.model';
|
||||
import { ProjectDto } from '@app/common/modules/project/dtos';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
constructor(private readonly projectRepository: ProjectRepository) {}
|
||||
|
||||
async createProject(
|
||||
createProjectDto: CreateProjectDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { name } = createProjectDto;
|
||||
|
||||
try {
|
||||
// Check if the project name already exists
|
||||
const isNameExist = await this.validate(name);
|
||||
if (isNameExist) {
|
||||
throw new HttpException(
|
||||
`Project with name ${name} already exists`,
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
|
||||
const newProject = this.projectRepository.create(createProjectDto);
|
||||
const savedProject = await this.projectRepository.save(newProject);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Project with ID ${savedProject.uuid} successfully created`,
|
||||
data: savedProject,
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
'An error occurred while creating the project. Please try again later.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteProject(uuid: string): Promise<BaseResponseDto> {
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
// Delete the project
|
||||
await this.projectRepository.delete({ uuid });
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Project with ID ${uuid} successfully deleted`,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
`An error occurred while deleting the project ${uuid}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async listProjects(
|
||||
pageable: Partial<TypeORMCustomModelFindAllQuery>,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
pageable.modelName = 'project';
|
||||
const customModel = TypeORMCustomModel(this.projectRepository);
|
||||
|
||||
const { baseResponseDto, paginationResponseDto } =
|
||||
await customModel.findAll(pageable);
|
||||
return new PageResponse<ProjectDto>(
|
||||
baseResponseDto,
|
||||
paginationResponseDto,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
async getProject(uuid: string): Promise<BaseResponseDto> {
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Project with ID ${uuid} retrieved successfully`,
|
||||
data: project,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
`An error occurred while retrieving the project with id ${uuid}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(uuid: string): Promise<ProjectEntity> {
|
||||
const project = await this.projectRepository.findOne({ where: { uuid } });
|
||||
return project;
|
||||
}
|
||||
|
||||
async validate(name: string): Promise<boolean> {
|
||||
const project = await this.projectRepository.findOne({ where: { name } });
|
||||
return !project;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user