-
Notifications
You must be signed in to change notification settings - Fork 1
/
articles.go
181 lines (153 loc) · 4.62 KB
/
articles.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
package api
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"google.golang.org/api/iterator"
)
// ArticlesType represents the articles collection in the database
type ArticlesType []map[string]interface{}
// ArticleFieldsType defines the structure of the fields in an article from the articles collection.
type ArticleFieldsType struct {
ID string `firestore:"id"`
Name string `firestore:"name"`
Price float64 `firestore:"price"`
Type string `firestore:"type"`
Year string `firestore:"year"`
Image string `firestore:"image"`
Description string `firestore:"description"`
Slug string `firestore:"slug"`
}
// DeleteType represents the body expected structure of a delete http call
type DeleteType struct {
ID string `json:"id"`
}
// ArticleAPI is an HTTP Cloud Function with a request parameter.
func ArticleAPI(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
conf := &firebase.Config{ProjectID: "***YOUR-PROJECT-ID***"}
app, err := firebase.NewApp(ctx, conf)
if err != nil {
log.Printf("error initializing app: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
client, err := app.Firestore(ctx)
if err != nil {
log.Printf("Firestore init: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer client.Close()
// Set CORS headers for the preflight request
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Max-Age", "3600")
w.WriteHeader(http.StatusNoContent)
return
}
// Set CORS headers for the main request.
w.Header().Set("Access-Control-Allow-Origin", "*")
switch method := r.Method; method {
case http.MethodGet:
getArticles(ctx, client, w)
case http.MethodPost:
setArticle(ctx, client, w, r)
case http.MethodDelete:
deleteArticle(ctx, client, w, r)
case http.MethodPut:
updateArticle(ctx, client, w, r)
default:
http.Error(w, "UNSUPPORTED METHOD", http.StatusNotFound)
}
}
func getArticles(ctx context.Context, client *firestore.Client, w http.ResponseWriter) {
var articles ArticlesType
iter := client.Collection("articles").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Printf("Iteration over documents failed %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
articles = append(articles, doc.Data())
}
json.NewEncoder(w).Encode(articles)
}
func setArticle(ctx context.Context, client *firestore.Client, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
defer r.Body.Close()
var newArticle ArticleFieldsType
err = json.Unmarshal(body, &newArticle)
if err != nil {
log.Printf("Unmarshalling json failed %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = client.Collection("articles").Doc(newArticle.ID).Create(ctx, &newArticle)
if err != nil {
log.Printf("Collection update failed %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func deleteArticle(ctx context.Context, client *firestore.Client, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Reading request body failed %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer r.Body.Close()
var Body DeleteType
err = json.Unmarshal(body, &Body)
if err != nil {
log.Printf("Unmarshalling json failed %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = client.Collection("articles").Doc(Body.ID).Delete(ctx)
if err != nil {
log.Printf("Document deletion failed %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func updateArticle(ctx context.Context, client *firestore.Client, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Reading request body failed %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer r.Body.Close()
var Body ArticleFieldsType
err = json.Unmarshal(body, &Body)
if err != nil {
log.Printf("Unmarshalling json failed %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = client.Collection("articles").Doc(Body.ID).Set(ctx, &Body)
if err != nil {
log.Printf("Document update failed %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}