-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (116 loc) · 3.31 KB
/
main.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
package main
import (
"encoding/json"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type Book struct {
ISBN string `json:"pk"`
Author string `json:"sk"`
Title string `json:"title"`
ItemCount int `json:"itemCount"`
}
func router(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
switch req.RequestContext.HTTP.Method {
case "GET":
return show(req)
case "POST":
return create(req)
case "PATCH":
return update(req)
default:
return clientError(http.StatusMethodNotAllowed)
}
}
// GET request must use pk,sk as JSON
func show(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Unmarshal Request JSON to Book struct
bookReq, err := unmarshalBookJson(req)
if err != nil {
return clientError(http.StatusUnprocessableEntity)
}
// Validate Request, see utils for details
if !validateReadRequest(bookReq.ISBN, bookReq.Author) {
return clientError(http.StatusBadRequest)
}
// Get the book response from DynamoDB based on the pk,sk pair
bookRes, err := getItem(bookReq.ISBN, bookReq.Author)
if err != nil {
return serverError(err)
}
if bookRes == nil {
return clientError(http.StatusNotFound)
}
// APIGateway Body needs to be JSON, so we convert here
js, err := json.Marshal(bookRes)
if err != nil {
return serverError(err)
}
return events.APIGatewayV2HTTPResponse{
StatusCode: http.StatusOK,
Body: string(js),
}, nil
}
// POST request must use Book fields as JSON
func create(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Validate header content-type is JSON
if !validateJsonFormat(req) {
return clientError(http.StatusNotAcceptable)
}
// Unmarshal Request JSON to Book struct
bookReq, err := unmarshalBookJson(req)
if err != nil {
return clientError(http.StatusUnprocessableEntity)
}
// Validate Request, see utils for details
if !validateWriteRequest(bookReq.ISBN, bookReq.Author, bookReq.Title) {
return clientError(http.StatusBadRequest)
}
// putItem returns new Book or err
bookRes, err := putItem(bookReq)
if err != nil {
return serverError(err)
}
// APIGateway Body needs to be JSON, so we convert here
js, err := json.Marshal(bookRes)
if err != nil {
return serverError(err)
}
return events.APIGatewayV2HTTPResponse{
StatusCode: 201,
Body: string(js),
}, nil
}
// PATCH request must use Book fields as JSON
func update(req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
// Unmarshal Request JSON to Book struct
bookReq, err := unmarshalBookJson(req)
if err != nil {
return clientError(http.StatusUnprocessableEntity)
}
// Validate Request, see utils for details
if !validateReadRequest(bookReq.ISBN, bookReq.Author) {
return clientError(http.StatusBadRequest)
}
// Get the book response from DynamoDB based on the pk,sk pair
bookRes, err := incrementItem(bookReq.ISBN, bookReq.Author)
if err != nil {
return serverError(err)
}
if bookRes == nil {
return clientError(http.StatusNotFound)
}
// APIGateway Body needs to be JSON, so we convert here
js, err := json.Marshal(bookRes)
if err != nil {
return serverError(err)
}
return events.APIGatewayV2HTTPResponse{
StatusCode: http.StatusOK,
Body: string(js),
}, nil
}
func main() {
lambda.Start(router)
}