-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (58 loc) · 1.51 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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type item struct {
Description string `json:"shortDescription"`
Price string `json:"price"`
}
type receipt struct {
ID uuid.UUID `json:"id"`
Points int `json:"points"`
Retailer string `json:"retailer"`
PurchaseDate string `json:"purchaseDate"`
PurchaseTime string `json:"purchaseTime"`
Items []item `json:"items"`
Total string `json:"total"`
}
type proccess_response struct {
ID uuid.UUID `json:"id"`
}
type points_response struct {
Points int `json:"points"`
}
var receipts = []receipt{}
func getReceipts(c *gin.Context) {
c.IndentedJSON(http.StatusOK, receipts)
}
func processReceipt(c *gin.Context) {
var data receipt
if err := c.BindJSON(&data); err != nil {
return
}
var id = uuid.New()
data.ID = id
data.Points = calculateAllPoints(data)
receipts = append(receipts, data)
c.IndentedJSON(http.StatusCreated, proccess_response{ID: id})
}
func countPoints(c *gin.Context) {
id := c.Param("id")
for _, r := range receipts {
if r.ID.String() == id {
points := r.Points
c.IndentedJSON(http.StatusOK, points_response{Points: points})
return
}
}
c.IndentedJSON(http.StatusNotFound, map[string]string{"error": "provided id does not exist"})
}
func main() {
router := gin.Default()
router.GET("/receipts", getReceipts)
router.POST("/receipts/process", processReceipt)
router.GET("/receipts/:id/points", countPoints)
router.Run()
}