1
1
import { HttpClient , HttpErrorResponse , HttpHeaders , HttpParams } from '@angular/common/http' ;
2
2
import { Injectable } from '@angular/core' ;
3
- import { API_CONFIG } from '../app/api.config' ;
4
3
import { catchError , Observable , throwError } from 'rxjs' ;
5
4
import {
6
5
SingleQuestionResponse ,
@@ -9,20 +8,23 @@ import {
9
8
MessageOnlyResponse ,
10
9
} from '../app/questions/question.model' ;
11
10
import { TopicResponse } from '../app/questions/topic.model' ;
11
+ import { ApiService } from './api.service' ;
12
12
13
13
@Injectable ( {
14
14
providedIn : 'root' ,
15
15
} )
16
- export class QuestionService {
17
- private baseUrl = API_CONFIG . baseUrl + ' /questions';
16
+ export class QuestionService extends ApiService {
17
+ protected apiPath = 'question /questions';
18
18
19
19
private httpOptions = {
20
20
headers : new HttpHeaders ( {
21
21
'Content-Type' : 'application/json' ,
22
22
} ) ,
23
23
} ;
24
24
25
- constructor ( private http : HttpClient ) { }
25
+ constructor ( private http : HttpClient ) {
26
+ super ( ) ;
27
+ }
26
28
27
29
getQuestions (
28
30
title ?: string ,
@@ -46,11 +48,11 @@ export class QuestionService {
46
48
}
47
49
48
50
// send request
49
- return this . http . get < QuestionResponse > ( this . baseUrl , { params } ) ;
51
+ return this . http . get < QuestionResponse > ( this . apiUrl , { params } ) ;
50
52
}
51
53
52
- getQuestionByID ( id : number ) : Observable < SingleQuestionResponse > {
53
- return this . http . get < SingleQuestionResponse > ( this . baseUrl + '/' + id ) ;
54
+ getQuestionByID ( id : number ) : Observable < QuestionResponse > {
55
+ return this . http . get < QuestionResponse > ( this . apiUrl + '/' + id ) ;
54
56
}
55
57
56
58
getQuestionByParam ( topics : string [ ] , difficulty : string , limit ?: number ) : Observable < QuestionResponse > {
@@ -61,32 +63,32 @@ export class QuestionService {
61
63
}
62
64
params = params . append ( 'topics' , topics . join ( ',' ) ) . append ( 'difficulty' , difficulty ) ;
63
65
64
- return this . http . get < QuestionResponse > ( this . baseUrl + '/search' , { params } ) ;
66
+ return this . http . get < QuestionResponse > ( this . apiUrl + '/search' , { params } ) ;
65
67
}
66
68
67
69
getTopics ( ) : Observable < TopicResponse > {
68
- return this . http . get < TopicResponse > ( this . baseUrl + '/topics' ) ;
70
+ return this . http . get < TopicResponse > ( this . apiUrl + '/topics' ) ;
69
71
}
70
72
71
73
addQuestion ( question : QuestionBody ) : Observable < SingleQuestionResponse > {
72
74
return this . http
73
- . post < SingleQuestionResponse > ( this . baseUrl , question , this . httpOptions )
75
+ . post < SingleQuestionResponse > ( this . apiUrl , question , this . httpOptions )
74
76
. pipe ( catchError ( this . handleError ) ) ;
75
77
}
76
78
77
79
updateQuestion ( id : number , question : QuestionBody ) : Observable < SingleQuestionResponse > {
78
80
return this . http
79
- . put < SingleQuestionResponse > ( this . baseUrl + '/' + id , question , this . httpOptions )
81
+ . put < SingleQuestionResponse > ( this . apiUrl + '/' + id , question , this . httpOptions )
80
82
. pipe ( catchError ( this . handleError ) ) ;
81
83
}
82
84
83
85
deleteQuestion ( id : number ) : Observable < SingleQuestionResponse > {
84
- return this . http . delete < SingleQuestionResponse > ( this . baseUrl + '/' + id ) . pipe ( catchError ( this . handleError ) ) ;
86
+ return this . http . delete < SingleQuestionResponse > ( this . apiUrl + '/' + id ) . pipe ( catchError ( this . handleError ) ) ;
85
87
}
86
88
87
89
deleteQuestions ( ids : number [ ] ) : Observable < MessageOnlyResponse > {
88
90
return this . http
89
- . post < MessageOnlyResponse > ( this . baseUrl + '/delete' , { ids } , this . httpOptions )
91
+ . post < MessageOnlyResponse > ( this . apiUrl + '/delete' , { ids } , this . httpOptions )
90
92
. pipe ( catchError ( this . handleError ) ) ;
91
93
}
92
94
0 commit comments