1
- import { HttpClient , HttpParams } from '@angular/common/http' ;
1
+ import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ;
2
2
import { Injectable } from '@angular/core' ;
3
3
import { API_CONFIG } from '../api.config' ;
4
4
import { Observable } from 'rxjs' ;
5
- import { QuestionResponse } from './question.model' ;
5
+ import { SingleQuestionResponse , QuestionResponse , QuestionBody } from './question.model' ;
6
6
import { TopicResponse } from './topic.model' ;
7
7
8
8
@Injectable ( {
@@ -11,6 +11,12 @@ import { TopicResponse } from './topic.model';
11
11
export class QuestionService {
12
12
private baseUrl = API_CONFIG . baseUrl ;
13
13
14
+ private httpOptions = {
15
+ headers : new HttpHeaders ( {
16
+ 'Content-Type' : 'application/json' ,
17
+ } ) ,
18
+ } ;
19
+
14
20
constructor ( private http : HttpClient ) { }
15
21
16
22
getQuestions (
@@ -38,7 +44,34 @@ export class QuestionService {
38
44
return this . http . get < QuestionResponse > ( this . baseUrl + '/questions' , { params } ) ;
39
45
}
40
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
+
41
62
getTopics ( ) : Observable < TopicResponse > {
42
63
return this . http . get < TopicResponse > ( this . baseUrl + '/questions/topics' ) ;
43
64
}
65
+
66
+ addQuestion ( question : QuestionBody ) : Observable < SingleQuestionResponse > {
67
+ return this . http . post < SingleQuestionResponse > ( this . baseUrl + '/questions' , question , this . httpOptions ) ;
68
+ }
69
+
70
+ updateQuestion ( id : number , question : QuestionBody ) : Observable < SingleQuestionResponse > {
71
+ return this . http . put < SingleQuestionResponse > ( this . baseUrl + '/questions/' + id , question , this . httpOptions ) ;
72
+ }
73
+
74
+ deleteQuestion ( id : number ) : Observable < SingleQuestionResponse > {
75
+ return this . http . delete < SingleQuestionResponse > ( this . baseUrl + '/questions/' + id ) ;
76
+ }
44
77
}
0 commit comments