import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from "@angular/common/http"; import {Grade} from "./grade.model"; import {catchError, Observable, of} from "rxjs"; const httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) } @Injectable({ providedIn: 'root' }) export class GradeService { private gradesUrl = 'http://localhost:8080/grades'; constructor(private http: HttpClient) { } addGrade(grade: Grade): Observable { return this.http.post(this.gradesUrl, grade, httpOptions).pipe( catchError(this.handleError('addGrade', 'Error while adding the grade!')) ); } getGradesFromSubject(subjectId: number): Observable { return this.http.get(`${this.gradesUrl}/subject/${subjectId}`).pipe( catchError(this.handleError('getGradesFromSubject', 'Error while getting grades', [])) ); } getStudentGrades(): Observable { return this.http.get(`${this.gradesUrl}/student`).pipe( catchError(this.handleError('getStudentGrades', 'Error while getting grades!',[])) ); } editGrade(grade: Grade): Observable { return this.http.patch(`${this.gradesUrl}/edit/${grade.id}`, grade.grade, httpOptions).pipe( catchError(this.handleError('editGrade', 'Editing grade failed!')) ); } deleteGrade(id: number): Observable {const url = `${this.gradesUrl}/${id}`; return this.http.delete(url, httpOptions).pipe( catchError(this.handleError('deleteGrade', 'Error while deleting the grade!')) ); } private handleError(operation = 'operation', communicate: string, result?: T) { return (error: any): Observable => { this.log(`${operation} failed: ${error.message}`); alert(`${communicate}`); return of(result as T); }; } private log(message: string) { console.log('GradeService: ' + message); } }