|
| 1 | +import { |
| 2 | + ConflictException, |
| 3 | + HttpException, |
| 4 | + Injectable, |
| 5 | + NotFoundException, |
| 6 | +} from '@nestjs/common'; |
| 7 | +import { Prisma, ProjectPostCategory } from '@prisma/client'; |
| 8 | +import { PrismaErrorService } from 'src/shared/modules/global/prisma-error.service'; |
| 9 | +import { PrismaService } from 'src/shared/modules/global/prisma.service'; |
| 10 | +import { EventBusService } from 'src/shared/modules/global/eventBus.service'; |
| 11 | +import { |
| 12 | + PROJECT_METADATA_RESOURCE, |
| 13 | + publishMetadataEvent, |
| 14 | +} from '../utils/metadata-event.utils'; |
| 15 | +import { toSerializable } from '../utils/metadata-utils'; |
| 16 | +import { CreateProjectPostCategoryDto } from './dto/create-project-post-category.dto'; |
| 17 | +import { ProjectPostCategoryResponseDto } from './dto/project-post-category-response.dto'; |
| 18 | +import { UpdateProjectPostCategoryDto } from './dto/update-project-post-category.dto'; |
| 19 | + |
| 20 | +@Injectable() |
| 21 | +export class ProjectPostCategoryService { |
| 22 | + constructor( |
| 23 | + private readonly prisma: PrismaService, |
| 24 | + private readonly prismaErrorService: PrismaErrorService, |
| 25 | + private readonly eventBusService: EventBusService, |
| 26 | + ) {} |
| 27 | + |
| 28 | + async findAll(): Promise<ProjectPostCategoryResponseDto[]> { |
| 29 | + const records = await this.prisma.projectPostCategory.findMany({ |
| 30 | + orderBy: [{ id: 'asc' }], |
| 31 | + }); |
| 32 | + |
| 33 | + return records.map((record) => this.toDto(record)); |
| 34 | + } |
| 35 | + |
| 36 | + async findById(id: string): Promise<ProjectPostCategoryResponseDto> { |
| 37 | + const parsedId = parseInt(id, 10); |
| 38 | + if (Number.isNaN(parsedId)) { |
| 39 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 40 | + } |
| 41 | + |
| 42 | + const record = await this.prisma.projectPostCategory.findFirst({ |
| 43 | + where: { |
| 44 | + id: BigInt(parsedId), |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + if (!record) { |
| 49 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 50 | + } |
| 51 | + |
| 52 | + return this.toDto(record); |
| 53 | + } |
| 54 | + |
| 55 | + async create( |
| 56 | + dto: CreateProjectPostCategoryDto, |
| 57 | + userId: number, |
| 58 | + ): Promise<ProjectPostCategoryResponseDto> { |
| 59 | + try { |
| 60 | + const existing = await this.prisma.projectPostCategory.findFirst({ |
| 61 | + where: { |
| 62 | + name: dto.name, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + if (existing) { |
| 67 | + throw new ConflictException( |
| 68 | + `Project showcase post category already exists for name ${dto.name}.`, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const created = await this.prisma.projectPostCategory.create({ |
| 73 | + data: { |
| 74 | + name: dto.name, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + await publishMetadataEvent( |
| 79 | + this.eventBusService, |
| 80 | + 'PROJECT_METADATA_CREATE', |
| 81 | + PROJECT_METADATA_RESOURCE.PROJECT_POST_CATEGORY, |
| 82 | + String(created.id), |
| 83 | + created, |
| 84 | + userId, |
| 85 | + ); |
| 86 | + |
| 87 | + return this.toDto(created); |
| 88 | + } catch (error) { |
| 89 | + this.handleError(error, `create project showcase post category ${dto.name}`); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + async update( |
| 94 | + id: string, |
| 95 | + dto: UpdateProjectPostCategoryDto, |
| 96 | + userId: number, |
| 97 | + ): Promise<ProjectPostCategoryResponseDto> { |
| 98 | + const parsedId = parseInt(id, 10); |
| 99 | + if (Number.isNaN(parsedId)) { |
| 100 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 101 | + } |
| 102 | + |
| 103 | + const existing = await this.prisma.projectPostCategory.findFirst({ |
| 104 | + where: { |
| 105 | + id: BigInt(parsedId), |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + if (!existing) { |
| 110 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 111 | + } |
| 112 | + |
| 113 | + const updated = await this.prisma.projectPostCategory.update({ |
| 114 | + where: { id: BigInt(parsedId) }, |
| 115 | + data: { |
| 116 | + ...(typeof dto.name === 'undefined' ? {} : { name: dto.name }), |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + await publishMetadataEvent( |
| 121 | + this.eventBusService, |
| 122 | + 'PROJECT_METADATA_UPDATE', |
| 123 | + PROJECT_METADATA_RESOURCE.PROJECT_POST_CATEGORY, |
| 124 | + String(updated.id), |
| 125 | + updated, |
| 126 | + userId, |
| 127 | + ); |
| 128 | + |
| 129 | + return this.toDto(updated); |
| 130 | + } |
| 131 | + |
| 132 | + async delete(id: string, userId: number): Promise<void> { |
| 133 | + const parsedId = parseInt(id, 10); |
| 134 | + if (Number.isNaN(parsedId)) { |
| 135 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 136 | + } |
| 137 | + |
| 138 | + const existing = await this.prisma.projectPostCategory.findFirst({ |
| 139 | + where: { |
| 140 | + id: BigInt(parsedId), |
| 141 | + }, |
| 142 | + select: { id: true }, |
| 143 | + }); |
| 144 | + |
| 145 | + if (!existing) { |
| 146 | + throw new NotFoundException(`Category not found for id ${id}.`); |
| 147 | + } |
| 148 | + |
| 149 | + await this.prisma.projectPostCategory.delete({ |
| 150 | + where: { id: BigInt(parsedId) }, |
| 151 | + }); |
| 152 | + |
| 153 | + await publishMetadataEvent( |
| 154 | + this.eventBusService, |
| 155 | + 'PROJECT_METADATA_DELETE', |
| 156 | + PROJECT_METADATA_RESOURCE.PROJECT_POST_CATEGORY, |
| 157 | + String(parsedId), |
| 158 | + { id: parsedId }, |
| 159 | + userId, |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + private toDto(record: ProjectPostCategory): ProjectPostCategoryResponseDto { |
| 164 | + return { |
| 165 | + id: String(record.id), |
| 166 | + name: record.name, |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + private handleError(error: unknown, operation: string): never { |
| 171 | + if (error instanceof HttpException) { |
| 172 | + throw error; |
| 173 | + } |
| 174 | + |
| 175 | + this.prismaErrorService.handleError(error, operation); |
| 176 | + } |
| 177 | +} |
0 commit comments