forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
35 lines (31 loc) · 831 Bytes
/
set.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
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/sirupsen/logrus"
)
// SetHandler Sets the value, and returns it in a resp
func (c *Controller) SetHandler(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
payload := resp{Status: "error"}
r.ParseForm()
val := r.FormValue("score")
score, err := strconv.ParseInt(val, 10, 64)
if err != nil {
logrus.WithField("error", err).Error("failed to parse input")
w.WriteHeader(http.StatusBadRequest)
enc.Encode(&payload)
return
}
if err := c.db.SetScore(score); err != nil {
logrus.WithField("error", err).Error("failed to set the score")
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(&payload)
return
}
w.WriteHeader(http.StatusOK)
payload.Value = score
payload.Status = "ok"
enc.Encode(&payload)
}