import { Injectable } from '@nestjs/common' ; import { CreateTodoDto } from './dto/create-todo.dto' ; import { UpdateTodoDto } from './dto/update-todo.dto' ; import { Todo , TodoStatus } from './entities/todo.entity' ; import { TodoRepository } from '../db/repositories/TodoRepository' ; import { UserRepository } from '../db/repositories/UserRepository' ; @ Injectable ( ) export class TodoService { constructor ( private todoRepository , private userRepository ) { } async create ( userId , createTodoDto ) { const user = await this . userRepository . findOne ( userId ) ; const { title , description , media } = createTodoDto ; const todo = new Todo ( ) ; todo . title = title ; todo . description = description ; todo . status = createTodoDto . status || TodoStatus . TODO ; todo . media = media ; todo . author = user ; return this . todoRepository . save ( todo ) ; } async findAll ( ) { return this . todoRepository . find ( ) ; } async findAllByUserId ( userId ) { const user = await this . userRepository . findOne ( { relations : [ 'todos' ] , where : { id : userId } , } ) ; return user ? user . todos : [ ] ; } async findOne ( id ) { return this . todoRepository . findOne ( id ) ; } async update ( id , updateTodoDto ) { const { title , description , status , media } = updateTodoDto ; return this . todoRepository . update ( id , { title , description , status : status || TodoStatus . TODO , media : media || '' , } ) ; } async remove ( id ) { return this . todoRepository . delete ( { id , } ) ; } }