Skip to content

Instantly share code, notes, and snippets.

@nicobytes
Created December 5, 2021 20:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nicobytes/09b882a254e83aed5d4b0edef8769ebb to your computer and use it in GitHub Desktop.
Save nicobytes/09b882a254e83aed5d4b0edef8769ebb to your computer and use it in GitHub Desktop.
interface Task {
  id: number;
  title: string;
  completed: boolean;
  createdAt: Date;
}

interface CreateTaskDto extends Omit<Task, 'id' | 'createdAt'> {}
interface UpdateTaskDto extends Omit<Partial<Task>, 'id'> {}
interface FindTaskDto extends Readonly<Partial<Task>> {}


export class TodoService {
  private tasks: Task[] = [];

  getTasks(): Task[] {
    return this.tasks;
  }

  find(params: FindTaskDto) {
    return this.tasks;
  }

  addTask(dto: CreateTaskDto): Task  {
    const newTask = {
      id: Math.random(),
      createdAt: new Date(),
      ...dto,
    }
    this.tasks.push(newTask);
    return newTask;
  }

  updateTasks(id: number, changes: UpdateTaskDto): Task{
    const index = this.tasks.findIndex(item => item.id === id);
    this.tasks[index] = {
      ...this.tasks[index],
      ...changes
    };
    return this.tasks[index];
  }
} 

const service = new TodoService();

const myTasks = service.getTasks();

const myNewTask = {
  title: '',
  completed: false,
}
service.addTask(myNewTask);

const id = 9;
const changes = {
  title: 'newTitle',
  completed: true
}
service.updateTasks(9, changes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment