-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
67 lines (54 loc) · 1.78 KB
/
Copy pathhandlers.go
File metadata and controls
67 lines (54 loc) · 1.78 KB
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
package main
import (
"encoding/json"
"net/http"
"strings"
"github.com/google/uuid"
)
// processReceiptHandler handles POST requests to process a receipt
func processReceiptHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
var receipt Receipt
err := json.NewDecoder(r.Body).Decode(&receipt)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
err = validateReceipt(receipt)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// With current implementation if the same receipt is uploaded multiple times, a new id will be generated each time.
// We could also keep a receipt->id map, but the requirements did not mention this, hence we skipped it.
receiptEnhanced := ReceiptEnhanced{
Id: uuid.New().String(),
Receipt: receipt,
Points: calculatePoints(receipt),
}
receipts[receiptEnhanced.Id] = receiptEnhanced
response := map[string]string{"id": receiptEnhanced.Id}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// getPointsHandler handles GET requests to retrieve points for a specific receipt
func getPointsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
id := strings.TrimPrefix(r.URL.Path, "/receipts/")
id = strings.TrimSuffix(id, "/points")
receiptEnhanced, exists := receipts[id]
if !exists {
http.Error(w, "Receipt not found", http.StatusNotFound)
return
}
points := receiptEnhanced.Points
response := map[string]int{"points": points}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}