import { Model } from 'mongoose' ; import { Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import { ITodo , ITodosService } from './interfaces/index' ; import { CreateTodoDto } from './dto/createTodo.dto' ; import { debug } from 'console' ; @ Injectable ( ) export class TodosService implements ITodosService { constructor ( @ InjectModel ( 'Todo' ) private readonly todoModel ) { } async findAll ( ) { return await this . todoModel . find ( ) . exec ( ) ; } async findOne ( options ) { return await this . todoModel . findOne ( options ) . exec ( ) ; } async findById ( ID ) { return await this . todoModel . findById ( ID ) . exec ( ) ; } async create ( createTodoDto ) { const createdTodo = new this . todoModel ( createTodoDto ) ; return await createdTodo . save ( ) ; } async update ( ID , newValue ) { const todo = await this . todoModel . findById ( ID ) . exec ( ) ; if ( ! todo . _id ) { debug ( 'todo not found' ) ; } await this . todoModel . findByIdAndUpdate ( ID , newValue ) . exec ( ) ; return await this . todoModel . findById ( ID ) . exec ( ) ; } async delete ( ID ) { try { await this . todoModel . findByIdAndRemove ( ID ) . exec ( ) ; return 'The todo has been deleted' ; } catch ( err ) { debug ( err ) ; return 'The todo could not be deleted' ; } } }