import { Injectable } from '@angular/core' ; import { Todo } from './todo' ; import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Observable , throwError } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; @ Injectable ( ) export class TodoService { private baseUrl = 'http://localhost:8080' ; constructor ( private http ) { } getTodos ( ) { return this . http . get < Todo [ ] > ( this . baseUrl + '/api/todos/' ) . pipe ( catchError ( this . handleError ) ) ; } createTodo ( todoData ) { return this . http . post < Todo > ( this . baseUrl + '/api/todos/' , todoData ) . pipe ( catchError ( this . handleError ) ) ; } updateTodo ( todoData ) { return this . http . put < Todo > ( this . baseUrl + '/api/todos/' + todoData . id , todoData ) . pipe ( catchError ( this . handleError ) ) ; } deleteTodo ( id ) { return this . http . delete ( this . baseUrl + '/api/todos/' + id ) . pipe ( catchError ( this . handleError ) ) ; } private handleError ( error ) { if ( error . status === 0 ) { console . error ( 'An error occurred:' , error . error ) ; } else { console . error ( `Backend returned code ${ error . status }, body was: ` , error . error ) ; } return throwError ( 'Something bad happened; please try again later.' ) ; } }