import { Injectable, NotFoundException } from "@nestjs/common"; import { CheckListItemRepository } from "../repositories/check-list-item.repository"; import { TaskRepository } from "../repositories/task.repository"; import { TMCheckListItem } from "../entities/check-list-item.entity"; import { CreateCheckListItemDto } from "../dto/check-list-item/create-check-list-item.dto"; import { UpdateCheckListItemDto } from "../dto/check-list-item/update-check-list-item.dto"; @Injectable() export class CheckListItemService { constructor( private readonly checkListItemRepository: CheckListItemRepository, private readonly taskRepository: TaskRepository, ) {} findAll(): Promise { return this.checkListItemRepository.findAll(); } findByTask(taskId: string): Promise { return this.checkListItemRepository.findByTask(taskId); } async findOne(id: string): Promise { const checkListItem = await this.checkListItemRepository.findOneById(id); if (!checkListItem) throw new NotFoundException(`CheckListItem #${id} not found`); return checkListItem; } async create(dto: CreateCheckListItemDto): Promise { const task = await this.taskRepository.findOneById(dto.taskId); if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`); const checkListItem = this.checkListItemRepository.create(dto); return this.checkListItemRepository.save(checkListItem); } async update(id: string, dto: UpdateCheckListItemDto): Promise { const checkListItem = await this.findOne(id); if (dto.taskId) { const task = await this.taskRepository.findOneById(dto.taskId); if (!task) throw new NotFoundException(`Task #${dto.taskId} not found`); } Object.assign(checkListItem, dto); return this.checkListItemRepository.save(checkListItem); } async remove(id: string): Promise { await this.findOne(id); await this.checkListItemRepository.delete(id); } }