1
1
"use server" ;
2
2
3
- import { Question , QuestionFullBody , StatusBody } from "@/api/structs" ;
3
+ import { isError , Question , QuestionFullBody , StatusBody } from "@/api/structs" ;
4
4
import { revalidatePath } from "next/cache" ;
5
- import { generateAuthHeaders , generateJSONHeaders } from "@/api/gateway" ;
5
+ import {
6
+ generateAuthHeaders ,
7
+ generateJSONHeaders ,
8
+ verifyUser ,
9
+ } from "@/api/gateway" ;
10
+ import DOMPurify from "isomorphic-dompurify" ;
11
+
12
+ const questionServiceUrl = `${ process . env . NEXT_PUBLIC_NGINX } /${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } ` ;
6
13
7
14
export async function deleteQuestion ( id : number ) : Promise < StatusBody > {
8
- const res = await fetch (
9
- `${ process . env . NEXT_PUBLIC_NGINX } /${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/delete/${ id } ` ,
10
- {
11
- method : "DELETE" ,
12
- headers : generateAuthHeaders ( ) ,
13
- } ,
14
- ) ;
15
+ const verify = await verifyUser ( ) ;
16
+ if ( isError ( verify ) || verify ?. data . isAdmin === false ) {
17
+ return verify as StatusBody ;
18
+ }
19
+
20
+ const res = await fetch ( `${ questionServiceUrl } /questions/delete/${ id } ` , {
21
+ method : "DELETE" ,
22
+ headers : generateAuthHeaders ( ) ,
23
+ } ) ;
15
24
if ( res . ok ) {
16
25
return { status : res . status } ;
17
26
}
@@ -20,10 +29,29 @@ export async function deleteQuestion(id: number): Promise<StatusBody> {
20
29
return json as StatusBody ;
21
30
}
22
31
32
+ export async function fetchAllQuestions ( ) : Promise < StatusBody | Question [ ] > {
33
+ console . log ( "Fetching all questions..." ) ;
34
+ const res = await fetch ( `${ questionServiceUrl } /questions` , {
35
+ method : "GET" ,
36
+ headers : generateAuthHeaders ( ) ,
37
+ cache : "no-store" ,
38
+ } ) ;
39
+ if ( ! res . ok ) {
40
+ return { status : res . status } ;
41
+ }
42
+ const json = await res . json ( ) ;
43
+ return json as Question [ ] ;
44
+ }
45
+
23
46
export async function editQuestion ( question : Question ) : Promise < StatusBody > {
47
+ const verify = await verifyUser ( ) ;
48
+ if ( isError ( verify ) || verify ?. data . isAdmin === false ) {
49
+ return verify as StatusBody ;
50
+ }
51
+
24
52
console . log ( "editing question" , question . id ) ;
25
53
const res = await fetch (
26
- `${ process . env . NEXT_PUBLIC_NGINX } / ${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/replace/${ question . id } ` ,
54
+ `${ questionServiceUrl } /questions/replace/${ question . id } ` ,
27
55
{
28
56
method : "PUT" ,
29
57
body : JSON . stringify ( question ) ,
@@ -42,10 +70,12 @@ export async function editQuestion(question: Question): Promise<StatusBody> {
42
70
export async function addQuestion (
43
71
question : QuestionFullBody ,
44
72
) : Promise < StatusBody > {
73
+ const verify = await verifyUser ( ) ;
74
+ if ( isError ( verify ) || verify ?. data . isAdmin === false ) {
75
+ return verify as StatusBody ;
76
+ }
45
77
console . log ( "Adding question" , question . title ) ;
46
- const url = `${ process . env . NEXT_PUBLIC_NGINX } /${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions` ;
47
- console . log ( url ) ;
48
- const res = await fetch ( url , {
78
+ const res = await fetch ( `${ questionServiceUrl } /questions` , {
49
79
method : "POST" ,
50
80
body : JSON . stringify ( question ) ,
51
81
headers : generateJSONHeaders ( ) ,
@@ -57,3 +87,31 @@ export async function addQuestion(
57
87
const json = await res . json ( ) ;
58
88
return json as StatusBody ;
59
89
}
90
+
91
+ export async function fetchQuestion (
92
+ questionId : number ,
93
+ ) : Promise < Question | StatusBody > {
94
+ try {
95
+ const response = await fetch (
96
+ `${ questionServiceUrl } /questions/solve/${ questionId } ` ,
97
+ {
98
+ method : "GET" ,
99
+ headers : generateAuthHeaders ( ) ,
100
+ cache : "no-store" ,
101
+ } ,
102
+ ) ;
103
+ if ( ! response . ok ) {
104
+ return {
105
+ error : await response . text ( ) ,
106
+ status : response . status ,
107
+ } ;
108
+ }
109
+
110
+ const question = ( await response . json ( ) ) as Question ;
111
+ question . content = DOMPurify . sanitize ( question . content ) ;
112
+ revalidatePath ( `/questions/edit/${ questionId } ` ) ;
113
+ return question ;
114
+ } catch ( err : any ) {
115
+ return { error : err . message , status : 400 } ;
116
+ }
117
+ }
0 commit comments