Skip to content
Snippets Groups Projects
data.service.ts 4.83 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, forkJoin } 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';
pnogal's avatar
pnogal committed
import { CourseDetails } from './models/course-details';
Paulina Nogal's avatar
Paulina Nogal committed
import { Note } from './models/note';

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

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

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

	getDegreePlannerCourseData2(roadmapId: number): Observable<Course> {
		return this.http.get<Course>(this.plannerApiUrl + '/degreePlan/' + roadmapId + '/courses')
			.pipe(catchError(this.errorHandler));
	}

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

pnogal's avatar
pnogal committed
	getCourseDetails(termCode: string, subjectCode: string, courseId: string): Observable<CourseDetails[]> {
		return this.http.get<CourseDetails[]>(this.searchApiUrl + '/course/0000/' + subjectCode + '/' + courseId, httpOptions)
pnogal's avatar
pnogal committed
			.pipe(catchError(this.errorHandler));
	}

Paulina Nogal's avatar
Paulina Nogal committed
	getSubjectsMap(): Observable<Object> {
		return this.http.get(this.searchApiUrl + '/subjectsMap/0000', httpOptions)
		.pipe(catchError(this.errorHandler));
	}
	getFavoriteCourses(): Observable<FavoriteCourse[]> {
		return this.http.get<FavoriteCourse[]>(this.plannerApiUrl + '/favorites')
			.pipe(catchError(this.errorHandler));
	}

	getAllNotes(planId: number): Observable<Note[]> {
		return this.http.get<Note[]>(this.plannerApiUrl + '/degreePlan/' + planId + '/notes')
Paulina Nogal's avatar
Paulina Nogal committed
			.pipe(catchError(this.errorHandler));
	}

	getNote(planId, noteId): Observable<Note[]> {
		return this.http.get<Note[]>(this.plannerApiUrl + '/degreePlan/' + planId + '/notes/' + noteId)
Paulina Nogal's avatar
Paulina Nogal committed
			.pipe(catchError(this.errorHandler));
	}

	updateNote(planId, note): Observable<Note[]> {
		return this.http.put<Note[]>(this.plannerApiUrl + '/degreePlan/' + planId + '/notes/' + note.id, note, httpOptions)
Paulina Nogal's avatar
Paulina Nogal committed
			.pipe(catchError(this.errorHandler));
	}

	createNote(planId, note): Observable<Note[]> {
		return this.http.post<Note[]>(this.plannerApiUrl + '/degreePlan/' + planId + '/notes/', note, httpOptions)
Paulina Nogal's avatar
Paulina Nogal committed
		.pipe(catchError(this.errorHandler));
	}

	removeNote(planId, noteId): Observable<Note[]> {
		return this.http.delete<Note[]>(this.plannerApiUrl + '/degreePlan/' + planId + '/notes/' + noteId, httpOptions)
Paulina Nogal's avatar
Paulina Nogal committed
		.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));
	}

	addCourse(planId, subjectCode, courseId, termCode) {
		return this.http.post(this.plannerApiUrl + '/degreePlan/' + planId + '/courses', {subjectCode, courseId, termCode }, httpOptions)
		.pipe(catchError(this.errorHandler));
	}

	removeCourse(planId, recordId) {
		return this.http.delete(this.plannerApiUrl + '/degreePlan/' + planId + '/courses/' + recordId, httpOptions)
		.pipe(catchError(this.errorHandler));
	}

	updateCourseTerm(planId, recordId, termCode): Observable<Course> {
		return this.http.put<Course>(this.plannerApiUrl + '/degreePlan/' + planId + '/courses/' + recordId + '?termCode=' + termCode, httpOptions)
		.pipe(catchError(this.errorHandler));
	}

test() {
// return this.http.delete(this.plannerApiUrl + '/degreePlan/519260/courses/259445', httpOptions)
Scott Berg's avatar
Scott Berg committed
return this.http.put(this.plannerApiUrl + '/degreePlan/519260/courses/259465?termCode=1174', 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
}