-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
127 lines (115 loc) · 2.92 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
type createParams struct {
Puzzle [81]int `json:"puzzle"`
Solution [81]int `json:"solution"`
}
type errorBody struct {
Error string `json:"error"`
Message string `json:"message"`
}
type createErrorBody struct {
Error string `json:"error"`
Message string `json:"message"`
ID string `json:"id"`
}
type stats struct {
Count int `json:"count"`
}
func statsIndex(repo repo, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
count := repo.GetPuzzleCount()
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(stats{Count: count}); err != nil {
panic(err)
}
}
func puzzleIndex(repo repo, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(http.StatusOK)
sudoku, err := repo.RandomSudoku()
if err != nil {
w.WriteHeader(http.StatusNotFound)
body := errorBody{
Error: "Error fetching random puzzle",
Message: err.Error(),
}
if err := json.NewEncoder(w).Encode(body); err != nil {
panic(err)
}
} else {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(sudoku); err != nil {
panic(err)
}
}
}
func puzzleShow(repo repo, w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.String(), "/")
id := parts[len(parts)-1]
fmt.Println(id)
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
sudoku, err := repo.GetSudoku(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
body := errorBody{
Error: "Error fetching puzzle",
Message: err.Error(),
}
if err := json.NewEncoder(w).Encode(body); err != nil {
panic(err)
}
} else {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(sudoku); err != nil {
panic(err)
}
}
}
func puzzleCreate(repo repo, w http.ResponseWriter, r *http.Request) {
var params createParams
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, ¶ms); err != nil {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
return
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
sudoku, err := repo.CreateSudoku(params.Puzzle, params.Solution)
if err != nil {
w.WriteHeader(http.StatusConflict)
body := createErrorBody{
Error: "Error creating puzzle",
Message: err.Error(),
ID: sudoku.ID,
}
if err.Error() == "Puzzle already exists" {
duplicate.Inc()
}
if err := json.NewEncoder(w).Encode(body); err != nil {
panic(err)
}
} else {
added.Inc()
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(sudoku); err != nil {
panic(err)
}
}
}