Browse Source

Implement Angular Services

Eldar Mukhtarov 9 tháng trước cách đây
mục cha
commit
a19fa68382

+ 16 - 0
project/frontend-angular/src/app/services/grade.spec.ts

@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { GradeService } from './grade';
+
+describe('Grade', () => {
+  let service: GradeService;
+
+  beforeEach(() => {
+    TestBed.configureTestingModule({});
+    service = TestBed.inject(GradeService);
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+});

+ 56 - 0
project/frontend-angular/src/app/services/grade.ts

@@ -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);
+    };
+  }
+}

+ 16 - 0
project/frontend-angular/src/app/services/student.spec.ts

@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { StudentService } from './student';
+
+describe('Student', () => {
+  let service: StudentService;
+
+  beforeEach(() => {
+    TestBed.configureTestingModule({});
+    service = TestBed.inject(StudentService);
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+});

+ 40 - 0
project/frontend-angular/src/app/services/student.ts

@@ -0,0 +1,40 @@
+import {inject, Injectable} from '@angular/core';
+import {HttpClient, HttpHeaders} from "@angular/common/http";
+import {catchError, Observable, of, tap} from "rxjs";
+import {Student} from "../models/student";
+
+const httpOptions = {
+  headers: new HttpHeaders({'Content-Type': 'application/json'})
+};
+
+@Injectable({
+  providedIn: 'root'
+})
+export class StudentService {
+  private studentsUrl = 'http://localhost:8080/students';
+
+  private http = inject(HttpClient);
+
+  getStudents(): Observable<Student[]> {
+    return this.http.get<Student[]>(this.studentsUrl).pipe(
+      catchError(this.handleError<Student[]>('getStudents', 'Error while getting students'))
+    );
+  }
+
+  deleteStudent(student: Student | number): Observable<Student> {
+    const id = typeof student === 'number' ? student : student.id;
+    const url = `${this.studentsUrl}/${id}`;
+    return this.http.delete<Student>(url, httpOptions).pipe(
+      tap(_ => console.log(`StudentService: deleted student id=${id}`)),
+      catchError(this.handleError<Student>('deleteStudent', 'Error while deleting student'))
+    );
+  }
+
+  private handleError<T>(operation = 'operation', communicate: string, result?: T) {
+    return (error: any): Observable<T> => {
+      console.log(`StudentService: ${operation} failed: ${error.message}`);
+      alert(`${communicate}`);
+      return of(result as T);
+    };
+  }
+}

+ 16 - 0
project/frontend-angular/src/app/services/subject.spec.ts

@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { SubjectService } from './subject';
+
+describe('Subject', () => {
+  let service: SubjectService;
+
+  beforeEach(() => {
+    TestBed.configureTestingModule({});
+    service = TestBed.inject(SubjectService);
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+});

+ 48 - 0
project/frontend-angular/src/app/services/subject.ts

@@ -0,0 +1,48 @@
+import {inject, Injectable} from '@angular/core';
+import {HttpClient, HttpHeaders} from "@angular/common/http";
+import {catchError, Observable, of} from "rxjs";
+import { Subject } from '../models/subject';
+
+const httpOptions = {
+  headers: new HttpHeaders({'Content-Type': 'application/json'})
+}
+
+@Injectable({
+  providedIn: 'root'
+})
+export class SubjectService {
+  private subjectsUrl = 'http://localhost:8080/subjects';
+
+  private http = inject(HttpClient);
+
+  getSubject(id: number): Observable<Subject> {
+    const url = `${this.subjectsUrl}/${id}`;
+    return this.http.get<Subject>(url);
+  }
+
+  getTeachersSubjects(): Observable<Subject[]> {
+    return this.http.get<Subject[]>(`${this.subjectsUrl}/teacher`).pipe(
+      catchError(this.handleError<Subject[]>('getTeachersSubjects', 'Error while getting teachers subjects')),
+    );
+  }
+
+  getSubjects(): Observable<Subject[]> {
+    return this.http.get<Subject[]>(this.subjectsUrl).pipe(
+      catchError(this.handleError<Subject[]>('getSubjects', 'Error while getting subjects')),
+    );
+  }
+
+  addSubject(subject: Subject): Observable<Subject> {
+    return this.http.post<Subject>(this.subjectsUrl, subject, httpOptions).pipe(
+      catchError(this.handleError<Subject>('addSubject', 'Subject already exists!')),
+    );
+  }
+
+  private handleError<T>(operation = 'operation', communicate: string, result?: T) {
+    return (error: any): Observable<T> => {
+      console.log('SubjectService: ' + `${operation} failed: ${error.message}`);
+      alert(`${communicate}`);
+      return of(result as T);
+    };
+  }
+}

+ 16 - 0
project/frontend-angular/src/app/services/teacher.spec.ts

@@ -0,0 +1,16 @@
+import { TestBed } from '@angular/core/testing';
+
+import { TeacherService } from './teacher';
+
+describe('Teacher', () => {
+  let service: TeacherService;
+
+  beforeEach(() => {
+    TestBed.configureTestingModule({});
+    service = TestBed.inject(TeacherService);
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+});

+ 40 - 0
project/frontend-angular/src/app/services/teacher.ts

@@ -0,0 +1,40 @@
+import {inject, Injectable} from '@angular/core';
+import {HttpClient, HttpHeaders} from "@angular/common/http";
+import {catchError, Observable, of, tap} from "rxjs";
+import { Teacher } from '../models/teacher';
+
+const httpOptions = {
+  headers: new HttpHeaders({'Content-Type': 'application/json'})
+}
+
+@Injectable({
+  providedIn: 'root'
+})
+export class TeacherService {
+  private teachersUrl = 'http://localhost:8080/teachers';
+
+  private http = inject(HttpClient);
+
+  getTeachers(): Observable<Teacher[]> {
+    return this.http.get<Teacher[]>(this.teachersUrl).pipe(
+      catchError(this.handleError<Teacher[]>('getTeachers', 'Error while getting teachers'))
+    );
+  }
+
+  deleteTeacher(teacher: Teacher | number): Observable<Teacher> {
+    const id = typeof teacher === 'number' ? teacher : teacher.id;
+    const url = `${this.teachersUrl}/${id}`;
+    return this.http.delete<Teacher>(url, httpOptions).pipe(
+      tap(_ => alert(`Successfully deleted teacher with id=${id}`)),
+      catchError(this.handleError<Teacher>('deleteStudent', 'Error while deleting student'))
+    );
+  }
+
+  private handleError<T>(operation = 'operation', communicate: string, result?: T) {
+    return (error: any): Observable<T> => {
+      console.log(`TeacherService: ${operation} failed: ${error.message}`);
+      alert(`${communicate}`);
+      return of(result as T);
+    };
+  }
+}