// Tick counter by code func (c *Context) CounterByCodeTickHandler(rw web.ResponseWriter, req *web.Request) { code := parseStringPathParameter(req, "code") var counter orm.Counter db.Where(&orm.Counter{Code: code}).First(&counter) if counter.ID == 0 { rw.WriteHeader(http.StatusNotFound) rw.Write([]byte("Counter not found")) return } counterEvent := orm.NewTickCounterEvent(counter) db.Create(&counterEvent) counter.Reading = counterEvent.Reading counter.LastTick = counterEvent.Timestamp db.Save(counter) marshal(rw, counterEvent) }
// Correct counter by code func (c *Context) CounterByCodeCorrectHandler(rw web.ResponseWriter, req *web.Request) { code := parseStringPathParameter(req, "code") var counter orm.Counter db.Where(&orm.Counter{Code: code}).First(&counter) if counter.ID == 0 { rw.WriteHeader(http.StatusNotFound) rw.Write([]byte("Counter not found")) return } hah, err := ioutil.ReadAll(req.Body) fmt.Println(hah) if err != nil { fmt.Println(err) rw.WriteHeader(http.StatusInternalServerError) rw.Write([]byte("Error reading body")) return } newReading, err := parseUintFromString(string(hah)) if err != nil { fmt.Println(err) rw.WriteHeader(http.StatusBadRequest) rw.Write([]byte("Malformed value")) return } delta := int64(newReading) - int64(counter.Reading) counter.Reading = newReading db.Save(counter) counterEvent := orm.NewAbsCorrCounterEvent(counter, delta) db.Create(&counterEvent) marshal(rw, counterEvent) }