Skip to main content
Glama
todo.service.ts1.38 kB
import { Injectable, NotFoundException } from '@nestjs/common'; import { CreateTodoDto } from './dto/create-todo.dto'; import { UpdateTodoDto } from './dto/update-todo.dto'; import { Todo } from './entities/todo.entity'; @Injectable() export class TodoService { private todos: Todo[] = []; private nextId = 1; create(createTodoDto: CreateTodoDto): Todo { const todo: Todo = { id: this.nextId++, ...createTodoDto, completed: false, }; this.todos.push(todo); return todo; } findAll(): Todo[] { return this.todos; } findOne(id: number): Todo { const todo = this.todos.find(t => t.id === id); if (!todo) { throw new NotFoundException(`Todo with ID ${id} not found`); } return todo; } update(id: number, updateTodoDto: UpdateTodoDto): Todo { const todoIndex = this.todos.findIndex(t => t.id === id); if (todoIndex === -1) { throw new NotFoundException(`Todo with ID ${id} not found`); } const updatedTodo = { ...this.todos[todoIndex], ...updateTodoDto, }; this.todos[todoIndex] = updatedTodo; return updatedTodo; } remove(id: number): void { const todoIndex = this.todos.findIndex(t => t.id === id); if (todoIndex === -1) { throw new NotFoundException(`Todo with ID ${id} not found`); } this.todos.splice(todoIndex, 1); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/javascripto/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server