// Libraries import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Location } from '@angular/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; // Services import { ConfigService } from '@app/core/config.service'; // Models import { Note } from '@app/core/models/note'; import { Term } from '@app/core/models/term'; import { Course } from '@app/core/models/course'; import { DegreePlan } from '@app/core/models/degree-plan'; const HTTP_OPTIONS = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @Injectable({ providedIn: 'root' }) export class DegreePlannerApiService { constructor(private http: HttpClient, private config: ConfigService) {} public getAllDegreePlans(): Observable<DegreePlan[]> { return this.http.get<DegreePlan[]>(this.degreePlanEndpoint()); } public getPlan(roadmapId: number): Observable<DegreePlan> { return this.http.get<DegreePlan>(this.degreePlanEndpoint(roadmapId)); } public getActiveTerms(): Observable<Term[]> { return this.http.get<Term[]>(this.searchEndpoint('terms')); } public getAllNotes(roadmapId: number): Observable<Note[]> { return this.http.get<Note[]>(this.degreePlanEndpoint(roadmapId, 'notes')); } public getAllCourses(roadmapId: number): Observable<Course[]> { return this.http.get<Course[]>( this.degreePlanEndpoint(roadmapId, 'courses') ); } public createNote( planId: number, termCode: string, noteText: string ): Observable<Note> { const payload = { termCode: termCode, note: noteText }; return this.http.post<Note>( this.degreePlanEndpoint(planId, 'notes'), payload, HTTP_OPTIONS ); } public updateNote( planId: number, termCode: string, noteText: string, noteId: number ): Observable<Note> { const payload = { termCode: termCode, note: noteText, id: noteId }; return this.http .put<null>( this.degreePlanEndpoint(planId, 'notes', noteId), payload, HTTP_OPTIONS ) .pipe(map(() => payload)); } public deleteNote(planId: number, noteId: number): Observable<null> { return this.http.delete<null>( this.degreePlanEndpoint(planId, 'notes', noteId), HTTP_OPTIONS ); } /** * Helper function for building API endpoint URLs */ private degreePlanEndpoint(...parts: any[]): string { return ['degreePlan'] .concat(parts.map(part => part.toString())) .reduce((soFar, next) => { return Location.joinWithSlash(soFar, next); }, this.config.apiPlannerUrl); } private searchEndpoint(...parts: any[]): string { return parts .map(part => part.toString()) .reduce((soFar, next) => { return Location.joinWithSlash(soFar, next); }, this.config.apiSearchUrl); } }