-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
279 lines (263 loc) · 6.82 KB
/
server.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"context"
"log"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/hansmrtn/pay-party-api/models"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoInstance struct {
Client *mongo.Client
DB *mongo.Database
}
var mongoURI string
var dbCollection string
var dbName string
var port string
var mg MongoInstance
func init() {
err := godotenv.Load(".env")
if err != nil {
log.Print("Error loading .env file")
}
// Get env variables
mongoURI = os.Getenv("DATABASE_URL")
dbCollection = os.Getenv("DATABASE_COLLECTION")
dbName = os.Getenv("DATABASE_NAME")
port = os.Getenv("PORT")
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
if err != nil {
log.Fatal(err)
}
ctx, stop := context.WithTimeout(context.Background(), 30*time.Second)
defer stop()
err = client.Connect(ctx)
db := client.Database(dbName)
if err != nil {
log.Fatal(err)
}
mg = MongoInstance{
Client: client,
DB: db,
}
}
// Get all parties
func GetAllParties(ctx *fiber.Ctx) error {
query := bson.D{{}}
cursor, err := mg.DB.Collection(dbCollection).Find(ctx.Context(), query)
if err != nil {
log.Fatal(err.Error())
return ctx.Status(500).SendString(err.Error())
}
var parties []models.Party = make([]models.Party, 0)
// iterate the cursor and decode each item into a party
if err := cursor.All(ctx.Context(), &parties); err != nil {
return ctx.Status(500).SendString(err.Error())
}
return ctx.JSON(parties)
}
// Get a party
func GetParty(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
party := new(models.Party)
query := bson.D{{Key: "_id", Value: partyID}}
err = mg.DB.Collection(dbCollection).FindOne(ctx.Context(), query).Decode(&party)
if err != nil {
// ErrNoDocuments means that the filter did not match any documents in the collection
if err == mongo.ErrNoDocuments {
return ctx.SendStatus(404)
}
return ctx.SendStatus(500)
}
return ctx.Status(200).JSON(party)
}
// Create a new party
func NewParty(ctx *fiber.Ctx) error {
collection := mg.DB.Collection(dbCollection)
party := new(models.Party)
if err := ctx.BodyParser(party); err != nil {
return ctx.Status(400).SendString(err.Error())
}
// ensure mongo always sets generated ObjectIDs
party.ID = ""
insertRes, err := collection.InsertOne(ctx.Context(), party)
if err != nil {
return ctx.Status(500).SendString(err.Error())
}
filter := bson.D{{Key: "_id", Value: insertRes.InsertedID}}
createdRecord := collection.FindOne(ctx.Context(), filter)
createdParty := &models.Party{}
createdRecord.Decode(createdParty)
return ctx.Status(200).JSON(createdParty)
}
// Update a party -- Deprecated
func UpdateParty(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
party := new(models.Party)
if err := ctx.BodyParser(party); err != nil {
return ctx.Status(400).SendString(err.Error())
}
query := bson.D{{Key: "_id", Value: partyID}}
update := bson.D{
{Key: "$set",
Value: bson.D{
{Key: "ballots", Value: party.Ballots},
{Key: "receipts", Value: party.Receipts},
},
},
}
err = mg.DB.Collection(dbCollection).FindOneAndUpdate(ctx.Context(), query, update).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return ctx.SendStatus(404)
}
return ctx.SendStatus(500)
}
// return updated ObjectId
party.ID = ctx.Params("id")
return ctx.Status(200).JSON(party)
}
// Push a txn receipt to the party receipts
func AddPartyReceipt(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
receipt := new(models.Receipt)
if err := ctx.BodyParser(receipt); err != nil {
return ctx.Status(400).SendString(err.Error())
}
query := bson.D{{Key: "_id", Value: partyID}}
update := bson.D{
{Key: "$push",
Value: bson.D{
{Key: "receipts", Value: receipt},
},
},
}
err = mg.DB.Collection(dbCollection).FindOneAndUpdate(ctx.Context(), query, update).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return ctx.SendStatus(404)
}
return ctx.SendStatus(500)
}
return ctx.Status(200).JSON(receipt)
}
// Push a ballot to party ballots array
func AddPartyBallot(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
ballot := new(models.Ballot)
if err := ctx.BodyParser(ballot); err != nil {
return ctx.Status(400).SendString(err.Error())
}
query := bson.D{{Key: "_id", Value: partyID}}
update := bson.D{
{Key: "$push",
Value: bson.D{
{Key: "ballots", Value: ballot},
},
},
}
err = mg.DB.Collection(dbCollection).FindOneAndUpdate(ctx.Context(), query, update).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return ctx.SendStatus(404)
}
return ctx.SendStatus(500)
}
return ctx.Status(200).JSON(ballot)
}
// Push a Note to the party notes array
func AddPartyNote(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
note := new(models.Note)
if err := ctx.BodyParser(note); err != nil {
return ctx.Status(400).SendString(err.Error())
}
query := bson.D{{Key: "_id", Value: partyID}}
update := bson.D{
{Key: "$push",
Value: bson.D{
{Key: "notes", Value: note},
},
},
}
err = mg.DB.Collection(dbCollection).FindOneAndUpdate(ctx.Context(), query, update).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return ctx.SendStatus(404)
}
return ctx.SendStatus(500)
}
return ctx.Status(200).JSON(note)
}
// Delete party
// Docs: https://docs.mongodb.com/manual/reference/command/delete/
func DeleteParty(ctx *fiber.Ctx) error {
partyID, err := primitive.ObjectIDFromHex(
ctx.Params("id"),
)
if err != nil {
return ctx.SendStatus(400)
}
// find and delete the party with the given ID
query := bson.D{{Key: "_id", Value: partyID}}
result, err := mg.DB.Collection(dbCollection).DeleteOne(ctx.Context(), &query)
if err != nil {
return ctx.SendStatus(500)
}
if result.DeletedCount < 1 {
return ctx.SendStatus(404)
}
// the record was deleted
return ctx.SendStatus(204)
}
func main() {
// Create Fiber App
app := fiber.New()
// Use CORS
// TODO: Configure CORS
app.Use(cors.New())
// App routes
app.Get("/parties", GetAllParties)
app.Get("/party/:id", GetParty)
app.Post("/party", NewParty)
app.Put("/party/:id/vote", AddPartyBallot)
app.Put("/party/:id/distribute", AddPartyReceipt)
app.Put("/party/:id/note", AddPartyNote)
app.Delete("/party/:id", DeleteParty)
err := app.Listen(":" + port)
if err != nil {
log.Fatal(err)
}
}