-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
63 lines (48 loc) · 1.29 KB
/
handlers.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
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
var receipts = make(map[string]receipt) // id:receipt
type ProcessReceiptResponse struct {
ID string `json:"id"`
}
type GetPointsByReceiptIdResponse struct {
Points int `json:"points"`
}
func postReceipt(c *gin.Context) {
contentLength := c.Request.ContentLength
if contentLength == 0 {
c.JSON(http.StatusBadRequest, "Error: body request is not present!")
return
}
validatedReceipt, _ := c.Get("validatedReceipt")
incomingReceipt := validatedReceipt.(receipt)
// Generate receipt id here
incomingReceipt.ID = uuid.New().String()
receipts[incomingReceipt.ID] = incomingReceipt
response := ProcessReceiptResponse{
ID: incomingReceipt.ID,
}
c.IndentedJSON(http.StatusOK, response)
}
func getPointsByReceiptId(c *gin.Context) {
receiptId := c.Param("id")
if receiptId == "" {
c.JSON(http.StatusBadRequest, "Error: receiptId value is not present in the request!")
return
}
r, ok := receipts[receiptId]
if !ok {
errorMessage := fmt.Sprintf("Error: Can't find receipt with id: %s", receiptId)
c.JSON(http.StatusNotFound, errorMessage)
return
}
points := CalculatePoints(&r)
response := GetPointsByReceiptIdResponse{
Points: points,
}
c.IndentedJSON(http.StatusOK, response)
}