1
1
import { generateAuthHeaders , generateJSONHeaders } from "@/api/gateway" ;
2
2
import { NextRequest , NextResponse } from "next/server" ;
3
+ import { Question } from "@/api/structs" ;
3
4
4
5
export async function GET ( ) {
5
6
try {
@@ -8,22 +9,54 @@ export async function GET() {
8
9
{
9
10
method : "GET" ,
10
11
headers : generateAuthHeaders ( ) ,
11
- }
12
+ } ,
12
13
) ;
13
14
if ( ! response . ok ) {
14
15
return NextResponse . json (
15
16
{
16
17
error : await response . text ( ) ,
17
18
status : response . status ,
18
19
} ,
19
- { status : response . status }
20
+ { status : response . status } ,
20
21
) ;
21
22
}
22
23
return response ;
23
24
} catch ( err : any ) {
24
25
return NextResponse . json (
25
26
{ error : err . message , status : 400 } ,
26
- { status : 400 }
27
+ { status : 400 } ,
28
+ ) ;
29
+ }
30
+ }
31
+
32
+ export async function PUT ( request : NextRequest ) {
33
+ const body = ( await request . json ( ) ) as Question ;
34
+ try {
35
+ const response = await fetch (
36
+ `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/replace/${ body . id } ` ,
37
+ {
38
+ method : "PUT" ,
39
+ body : JSON . stringify ( body ) ,
40
+ headers : generateJSONHeaders ( ) ,
41
+ } ,
42
+ ) ;
43
+ if ( response . ok ) {
44
+ return NextResponse . json (
45
+ { status : response . status } ,
46
+ { status : response . status } ,
47
+ ) ;
48
+ }
49
+ return NextResponse . json (
50
+ {
51
+ error : ( await response . json ( ) ) [ "Error adding question: " ] ,
52
+ status : response . status ,
53
+ } ,
54
+ { status : response . status } ,
55
+ ) ;
56
+ } catch ( err : any ) {
57
+ return NextResponse . json (
58
+ { error : err . message , status : 400 } ,
59
+ { status : 400 } ,
27
60
) ;
28
61
}
29
62
}
@@ -37,25 +70,25 @@ export async function POST(request: NextRequest) {
37
70
method : "POST" ,
38
71
body : JSON . stringify ( body ) ,
39
72
headers : generateJSONHeaders ( ) ,
40
- }
73
+ } ,
41
74
) ;
42
75
if ( response . ok ) {
43
76
return NextResponse . json (
44
77
{ status : response . status } ,
45
- { status : response . status }
78
+ { status : response . status } ,
46
79
) ;
47
80
}
48
81
return NextResponse . json (
49
82
{
50
83
error : ( await response . json ( ) ) [ "Error adding question: " ] ,
51
84
status : response . status ,
52
85
} ,
53
- { status : response . status }
86
+ { status : response . status } ,
54
87
) ;
55
88
} catch ( err : any ) {
56
89
return NextResponse . json (
57
90
{ error : err . message , status : 400 } ,
58
- { status : 400 }
91
+ { status : 400 } ,
59
92
) ;
60
93
}
61
94
}
@@ -65,7 +98,7 @@ export async function DELETE(request: NextRequest) {
65
98
if ( body . qid === undefined ) {
66
99
return NextResponse . json (
67
100
{ error : "No ID specified." , status : 400 } ,
68
- { status : 400 }
101
+ { status : 400 } ,
69
102
) ;
70
103
}
71
104
try {
@@ -74,7 +107,7 @@ export async function DELETE(request: NextRequest) {
74
107
{
75
108
method : "DELETE" ,
76
109
headers : generateAuthHeaders ( ) ,
77
- }
110
+ } ,
78
111
) ;
79
112
if ( response . ok ) {
80
113
// NextResponse doesn't support 204.
@@ -85,12 +118,12 @@ export async function DELETE(request: NextRequest) {
85
118
error : ( await response . json ( ) ) [ "Error deleting question: " ] ,
86
119
status : response . status ,
87
120
} ,
88
- { status : response . status }
121
+ { status : response . status } ,
89
122
) ;
90
123
} catch ( err : any ) {
91
124
return NextResponse . json (
92
125
{ error : `Bad request: ${ err . message } ` , status : 400 } ,
93
- { status : 400 }
126
+ { status : 400 } ,
94
127
) ;
95
128
}
96
129
}
0 commit comments