Skip to content

Commit bbbf308

Browse files
committed
Add API calls
1 parent ca65293 commit bbbf308

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

frontend/src/app/questions/question.model.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ export interface QuestionResponse {
44
data?: Question[] | null;
55
}
66

7+
export interface SingleQuestionResponse {
8+
status: string;
9+
message: string;
10+
data: Question;
11+
}
12+
713
export interface Question {
8-
_id: string;
14+
_id?: string;
915
id: number;
1016
description: string;
1117
difficulty: string;
1218
title: string;
1319
topics?: string[];
1420
}
21+
22+
export interface QuestionBody {
23+
description?: string;
24+
difficulty?: string;
25+
title?: string;
26+
topics?: string[];
27+
}

frontend/src/app/questions/question.service.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { HttpClient, HttpParams } from '@angular/common/http';
1+
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { API_CONFIG } from '../api.config';
44
import { Observable } from 'rxjs';
5-
import { QuestionResponse } from './question.model';
5+
import { SingleQuestionResponse, QuestionResponse, QuestionBody } from './question.model';
66
import { TopicResponse } from './topic.model';
77

88
@Injectable({
@@ -11,6 +11,12 @@ import { TopicResponse } from './topic.model';
1111
export class QuestionService {
1212
private baseUrl = API_CONFIG.baseUrl;
1313

14+
private httpOptions = {
15+
headers: new HttpHeaders({
16+
'Content-Type': 'application/json',
17+
}),
18+
};
19+
1420
constructor(private http: HttpClient) {}
1521

1622
getQuestions(
@@ -38,7 +44,34 @@ export class QuestionService {
3844
return this.http.get<QuestionResponse>(this.baseUrl + '/questions', { params });
3945
}
4046

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+
4162
getTopics(): Observable<TopicResponse> {
4263
return this.http.get<TopicResponse>(this.baseUrl + '/questions/topics');
4364
}
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+
}
4477
}

0 commit comments

Comments
 (0)