Skip to content

Instantly share code, notes, and snippets.

@Qarun-Qadir-Bissoondial
Last active October 1, 2020 03:23
Show Gist options
  • Save Qarun-Qadir-Bissoondial/7634fcb13ad6441131b0485d8f8eb27e to your computer and use it in GitHub Desktop.
Save Qarun-Qadir-Bissoondial/7634fcb13ad6441131b0485d8f8eb27e to your computer and use it in GitHub Desktop.
Basic Todo service
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
export interface Todo {
userId: number,
id: number,
title: string,
completed: boolean;
}
@Injectable({
providedIn: 'root'
})
export class TodoService {
url: string = 'https://jsonplaceholder.typicode.com/todos';
constructor(private http: HttpClient) {}
getAllTodos() {
return this.http.get(this.url)
}
getSingleTodo(id: number) {
return this.http.get(`${this.url}/${id}`);
}
createTodo(item: Todo) {
return this.http.post(this.url, item);
}
updateTodo(id: number, updatedItem: Todo) {
return this.http.put(`${this.url}/${id}`, updatedItem);
}
deleteTodo(id: number) {
return this.http.delete(`${this.url}/${id}`);
}
displayError(message: string) {
console.log(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment