-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
231 lines (193 loc) · 5.7 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/go-playground/validator/v10"
"log"
"net/http"
)
type UpdateFatwa struct {
Title string `json:"title"`
Question string `json:"question" validate:"required"`
Answer string `json:"answer" validate:"required"`
Link string `json:"link"`
Author string `json:"author" validate:"required"`
Topic string `json:"topic"`
Lang string `json:"lang" validate:"required"`
}
type CreateFatwa struct {
//Id string `json:"id" validate:"required"`
Title string `json:"title"`
Question string `json:"question" validate:"required"`
Answer string `json:"answer" validate:"required"`
Link string `json:"link"`
Author string `json:"author" validate:"required"`
Topic string `json:"topic"`
Lang string `json:"lang" validate:"required"`
// CreatedAt time.Time `json:"createdat" dynamodbav:"createdat"`
// UpdatedAt time.Time `json:"updatedat" dynamodbav:"updatedat"`
}
var validate *validator.Validate = validator.New()
func Router(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Received req %#v", req)
switch req.HTTPMethod {
case "GET":
return processGet(ctx, req)
case "POST":
return processPost(ctx, req)
case "DELETE":
return processDelete(ctx, req)
case "PUT":
return processPut(ctx, req)
default:
return clientError(http.StatusMethodNotAllowed)
}
}
func processGet(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
id, ok := req.PathParameters["id"]
if !ok {
return processGetFatawa(ctx)
} else {
return processGetFatwa(ctx, id)
}
}
func processGetFatwa(ctx context.Context, id string) (events.APIGatewayProxyResponse, error) {
log.Printf("Received GET fatwa request with id = %s", id)
fatwa, err := GetItem(ctx, id)
if err != nil {
return serverError(err)
}
if fatwa == nil {
return clientError(http.StatusNotFound)
}
json, err := json.Marshal(fatwa)
if err != nil {
return serverError(err)
}
log.Printf("Successfully fetched fatwa item %s", json)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(json),
}, nil
}
func processGetFatawa(ctx context.Context) (events.APIGatewayProxyResponse, error) {
log.Print("Received GET fatawa request")
fatawa, err := ListItems(ctx)
if err != nil {
return serverError(err)
}
json, err := json.Marshal(fatawa)
if err != nil {
return serverError(err)
}
log.Printf("Successfully fetched fatawa: %s", json)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(json),
}, nil
}
func processPost(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var createFatwa CreateFatwa
err := json.Unmarshal([]byte(req.Body), &createFatwa)
if err != nil {
log.Printf("Can't unmarshal body: %v", err)
return clientError(http.StatusUnprocessableEntity)
}
err = validate.Struct(&createFatwa)
if err != nil {
log.Printf("Invalid body: %v", err)
return clientError(http.StatusBadRequest)
}
log.Printf("Received POST request with item: %+v", createFatwa)
res, err := InsertItem(ctx, createFatwa)
if err != nil {
return serverError(err)
}
log.Printf("Inserted new fatwa: %+v", res)
json, err := json.Marshal(res)
if err != nil {
return serverError(err)
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusCreated,
Body: string(json),
Headers: map[string]string{
"Location": fmt.Sprintf("/fatwa/%s", res.Id),
},
}, nil
}
func processDelete(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
id, ok := req.PathParameters["id"]
if !ok {
return clientError(http.StatusBadRequest)
}
log.Printf("Received DELETE request with id = %s", id)
fatwa, err := DeleteItem(ctx, id)
if err != nil {
return serverError(err)
}
if fatwa == nil {
return clientError(http.StatusNotFound)
}
json, err := json.Marshal(fatwa)
if err != nil {
return serverError(err)
}
log.Printf("Successfully deleted fatwa item %+v", fatwa)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(json),
}, nil
}
func processPut(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
id, ok := req.PathParameters["id"]
if !ok {
return clientError(http.StatusBadRequest)
}
var updateFatwa UpdateFatwa
err := json.Unmarshal([]byte(req.Body), &updateFatwa)
if err != nil {
log.Printf("Can't unmarshal body: %v", err)
return clientError(http.StatusUnprocessableEntity)
}
err = validate.Struct(&updateFatwa)
if err != nil {
log.Printf("Invalid body: %v", err)
return clientError(http.StatusBadRequest)
}
log.Printf("Received PUT request with item: %+v", updateFatwa)
res, err := UpdateItem(ctx, id, updateFatwa)
if err != nil {
return serverError(err)
}
if res == nil {
return clientError(http.StatusNotFound)
}
log.Printf("Updated fatwa: %+v", res)
json, err := json.Marshal(res)
if err != nil {
return serverError(err)
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(json),
Headers: map[string]string{
"Location": fmt.Sprintf("/fatwa/%s", res.Id),
},
}, nil
}
func clientError(status int) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
Body: http.StatusText(status),
StatusCode: status,
}, nil
}
func serverError(err error) (events.APIGatewayProxyResponse, error) {
log.Println(err.Error())
return events.APIGatewayProxyResponse{
Body: http.StatusText(http.StatusInternalServerError),
StatusCode: http.StatusInternalServerError,
}, nil
}