-
Notifications
You must be signed in to change notification settings - Fork 0
/
issueApi.go
218 lines (184 loc) · 5.52 KB
/
issueApi.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
log.Println("Init issueApi")
functions.HTTP("issue/{slug}", routeIssueApi)
}
type Issue struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
CreatedAt string `json:"created_at"`
}
type ResData struct {
Success bool `json:"success"`
Data any `json:"data"`
Error any `json:"error"`
}
func ResSuccess(w http.ResponseWriter, data any) error {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ResData { true, data, nil })
log.Printf("%d\n", 200);
return nil
}
func ResError(w http.ResponseWriter, status int, err error, data any) error {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ResData { false, data, fmt.Sprintf("%v", err) })
log.Printf("%d %v\n", status, err);
return nil
}
func routeIssueApi(w http.ResponseWriter, r *http.Request) {
var err error
var status int
method := r.Method
slug := r.PathValue("slug")
log.Printf("%s /issue/%s\n", method, slug);
switch slug {
case "list":
switch method {
case "GET": err, status = listIssues(w, r), 500
default: err, status = fmt.Errorf("unsupported method: %s", method), 405
}
case "new":
switch method {
case "POST": err, status = insertIssue(w, r, slug), 500
default: err, status = fmt.Errorf("unsupported method: %s", method), 405
}
default:
switch method {
case "GET": err, status = getIssue(w, r, slug), 500
case "POST": err, status = updateIssue(w, r, slug), 500
case "DELETE": err, status = deleteIssue(w, r, slug), 500
default: err, status = fmt.Errorf("unsupported method: %s", method), 405
}
}
if (err != nil) {
ResError(w, status, err, nil)
}
}
func listIssues(w http.ResponseWriter, r *http.Request) error {
db, err := OpenDB();
if err != nil {
return err
}
query := "SELECT id, title, description, created_at FROM issues ORDER BY created_at desc LIMIT 100"
rows, err := db.Query(query)
if err != nil {
return fmt.Errorf("Database.Query: %w", err)
}
defer rows.Close()
items := []Issue{}
var id string
var title string
var description string;
var created_at string;
for rows.Next() {
err := rows.Scan(&id, &title, &description, &created_at);
if err != nil {
return fmt.Errorf("Database.Scan: %w", err)
}
items = append(items, Issue { id, title, description, created_at })
}
return ResSuccess(w, items)
}
func getIssue(w http.ResponseWriter, r *http.Request, slug string) error {
db, err := OpenDB();
if err != nil {
return err
}
var id string
var title string
var description string;
var created_at string;
query := "SELECT id, title, description, created_at FROM issues WHERE id = $1 LIMIT 1"
err = db.QueryRow(query, slug).Scan(&id, &title, &description, &created_at)
if err != nil {
return fmt.Errorf("Database.Query: %w", err)
}
item := Issue { id, title, description, created_at }
return ResSuccess(w, item)
}
func insertIssue(w http.ResponseWriter, r *http.Request, slug string) error {
db, err := OpenDB();
if err != nil {
return err
}
contentType := r.Header.Get("Content-Type")
if (contentType != "application/json") {
err = fmt.Errorf("unsupported content type: %s", contentType)
return ResError(w, 400, err, nil)
}
var d struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
}
err = json.NewDecoder(r.Body).Decode(&d)
if err != nil {
err = fmt.Errorf("request body invalid: %w", err)
return ResError(w, 400, err, nil)
}
if (d.Id == "" ||d.Title == "" || d.Description == "") {
err = fmt.Errorf("missing required fields")
return ResError(w, 400, err, d)
}
query := "INSERT INTO issues (id, title, description) VALUES ($1, $2, $3)"
_, err = db.Exec(query, d.Id, d.Title, d.Description)
if err != nil {
return fmt.Errorf("Database.Exec: %w", err)
}
item := Issue { d.Id, d.Title, d.Description, "" }
return ResSuccess(w, item)
}
func updateIssue(w http.ResponseWriter, r *http.Request, slug string) error {
db, err := OpenDB();
if err != nil {
return err
}
contentType := r.Header.Get("Content-Type")
if (contentType != "application/json") {
err = fmt.Errorf("unsupported content type: %s", contentType)
return ResError(w, 400, err, nil)
}
var d struct {
Title string `json:"title"`
Description string `json:"description"`
}
err = json.NewDecoder(r.Body).Decode(&d)
if err != nil {
err = fmt.Errorf("request body invalid: %w", err)
return ResError(w, 400, err, nil)
}
if (d.Title == "" || d.Description == "") {
err = fmt.Errorf("missing required fields")
return ResError(w, 400, err, d)
}
query := "UPDATE issues SET title = $2, description = $3 WHERE id = $1"
_, err = db.Exec(query, slug, d.Title, d.Description)
if err != nil {
return fmt.Errorf("Database.Exec: %w", err)
}
item := Issue { slug, d.Title, d.Description, "" }
return ResSuccess(w, item)
}
func deleteIssue(w http.ResponseWriter, r *http.Request, slug string) error {
db, err := OpenDB();
if err != nil {
return err
}
query := "DELETE FROM issues WHERE id = $1"
_, err = db.Exec(query, slug)
if err != nil {
return fmt.Errorf("Database.Exec: %w", err)
}
item := Issue { slug, "", "", "" }
return ResSuccess(w, item)
}