Skip to content
Snippets Groups Projects
data.service.ts 2.07 KiB
Newer Older
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { Injectable } from '@angular/core';
import { throwError, Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { ConfigService } from './config.service';
import { Course } from './models/course';
import { DegreePlan } from './models/degree-plan';
import { Term } from './models/term';
import { FavoriteCourse } from './models/favorite-course';

const httpOptions = {
	headers: new HttpHeaders({
		'Content-Type':  'application/json'
	})
};
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed

@Injectable()
export class DataService {
	private plannerApiUrl: string;
	private searchApiUrl: string;
pnogal's avatar
pnogal committed
	constructor(private http: HttpClient, private configService: ConfigService) {
		this.plannerApiUrl = this.configService.apiPlannerUrl;
		this.searchApiUrl = this.configService.apiSearchUrl;
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
	}
pnogal's avatar
pnogal committed

	getDegreePlans(): Observable<DegreePlan[]> {
		return this.http.get<DegreePlan[]>(this.plannerApiUrl + '/degreePlan')
			.pipe(catchError(this.errorHandler));
	}

	getDegreePlannerCourseData(): Observable<Course[]> {
		return this.http.get<Course[]>(this.plannerApiUrl + '/degreePlan/520224/termcourses')
			.pipe(catchError(this.errorHandler));
	}

	getTerms(): Observable<Term[]> {
		return this.http.get<Term[]>(this.searchApiUrl + '/terms')
			.pipe(catchError(this.errorHandler));
pnogal's avatar
pnogal committed
	}

	getFavoriteCourses(): Observable<FavoriteCourse[]> {
		return this.http.get<FavoriteCourse[]>(this.plannerApiUrl + '/favorites')
			.pipe(catchError(this.errorHandler));
	}

	saveFavoriteCourse(subjectCode: string, courseId: string): Observable<FavoriteCourse> {
		return this.http.post<FavoriteCourse>(this.plannerApiUrl + 'favorites/' + subjectCode + '/' + courseId, httpOptions)
		.pipe(catchError(this.errorHandler));
	}

	removeFavoriteCourse(subjectCode, courseId): Observable<FavoriteCourse> {
		return this.http.delete<FavoriteCourse>(this.plannerApiUrl + 'favorites/' + subjectCode + '/' + courseId, httpOptions)
		.pipe(catchError(this.errorHandler));
	}

Joe Van Boxtel's avatar
Joe Van Boxtel committed
	private errorHandler(error: HttpErrorResponse) {
pnogal's avatar
pnogal committed
		return throwError(error || 'Server Error');
	}
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
}