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