|
| 1 | +import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http'; |
| 2 | +import { Injectable } from '@angular/core'; |
| 3 | +import { API_CONFIG } from '../app/api.config'; |
| 4 | +import { catchError, Observable, throwError } from 'rxjs'; |
| 5 | +import { SingleQuestionResponse, QuestionResponse, QuestionBody } from '../app/questions/question.model'; |
| 6 | +import { TopicResponse } from '../app/questions/topic.model'; |
| 7 | + |
| 8 | +@Injectable({ |
| 9 | + providedIn: 'root', |
| 10 | +}) |
| 11 | +export class QuestionService { |
| 12 | + private baseUrl = API_CONFIG.baseUrl; |
| 13 | + |
| 14 | + private httpOptions = { |
| 15 | + headers: new HttpHeaders({ |
| 16 | + 'Content-Type': 'application/json', |
| 17 | + }), |
| 18 | + }; |
| 19 | + |
| 20 | + constructor(private http: HttpClient) {} |
| 21 | + |
| 22 | + getQuestions( |
| 23 | + title?: string, |
| 24 | + description?: string, |
| 25 | + topics?: string[], |
| 26 | + difficulty?: string, |
| 27 | + ): Observable<QuestionResponse> { |
| 28 | + let params = new HttpParams(); |
| 29 | + |
| 30 | + if (title) { |
| 31 | + params = params.append('title', title); |
| 32 | + } |
| 33 | + if (description) { |
| 34 | + params = params.append('description', description); |
| 35 | + } |
| 36 | + if (topics && topics.length > 0) { |
| 37 | + params = params.append('topics', topics.join(',')); |
| 38 | + } |
| 39 | + if (difficulty) { |
| 40 | + params = params.append('difficulty', difficulty); |
| 41 | + } |
| 42 | + |
| 43 | + // send request |
| 44 | + return this.http.get<QuestionResponse>(this.baseUrl + '/questions', { params }); |
| 45 | + } |
| 46 | + |
| 47 | + getQuestionByID(id: number): Observable<QuestionResponse> { |
| 48 | + return this.http.get<QuestionResponse>(this.baseUrl + '/questions/' + id); |
| 49 | + } |
| 50 | + |
| 51 | + getQuestionByParam(topics: string[], difficulty: string, limit?: number): Observable<QuestionResponse> { |
| 52 | + let params = new HttpParams(); |
| 53 | + |
| 54 | + if (limit) { |
| 55 | + params = params.append('limit', limit); |
| 56 | + } |
| 57 | + params = params.append('topics', topics.join(',')).append('difficulty', difficulty); |
| 58 | + |
| 59 | + return this.http.get<QuestionResponse>(this.baseUrl + '/questions/search', { params }); |
| 60 | + } |
| 61 | + |
| 62 | + getTopics(): Observable<TopicResponse> { |
| 63 | + return this.http.get<TopicResponse>(this.baseUrl + '/questions/topics'); |
| 64 | + } |
| 65 | + |
| 66 | + addQuestion(question: QuestionBody): Observable<SingleQuestionResponse> { |
| 67 | + return this.http |
| 68 | + .post<SingleQuestionResponse>(this.baseUrl + '/questions', question, this.httpOptions) |
| 69 | + .pipe(catchError(this.handleError)); |
| 70 | + } |
| 71 | + |
| 72 | + updateQuestion(id: number, question: QuestionBody): Observable<SingleQuestionResponse> { |
| 73 | + return this.http |
| 74 | + .put<SingleQuestionResponse>(this.baseUrl + '/questions/' + id, question, this.httpOptions) |
| 75 | + .pipe(catchError(this.handleError)); |
| 76 | + } |
| 77 | + |
| 78 | + deleteQuestion(id: number): Observable<SingleQuestionResponse> { |
| 79 | + return this.http |
| 80 | + .delete<SingleQuestionResponse>(this.baseUrl + '/questions/' + id) |
| 81 | + .pipe(catchError(this.handleError)); |
| 82 | + } |
| 83 | + |
| 84 | + handleError(error: HttpErrorResponse) { |
| 85 | + return throwError(error); |
| 86 | + } |
| 87 | +} |
0 commit comments