// PUT /points/:id func editPoint(w http.ResponseWriter, r *http.Request) { badRequestError := Response{"error", 400, "Bad Request", nil} var ( point Point rpoint PointFromRequest ) routes.ReadJson(r, &rpoint) params := r.URL.Query() id, _ := strconv.Atoi(params.Get(":id")) err := Points.Find(bson.M{"id": id}).One(&point) if err != nil { fmt.Println(err) routes.ServeJson(w, badRequestError) return } if rpoint.Ssid != nil { point.Ssid = *rpoint.Ssid } if rpoint.Mac != nil { point.Mac = *rpoint.Mac } if rpoint.Password != nil { point.Password = *rpoint.Password } if rpoint.Latitude != nil { point.Latitude = *rpoint.Latitude } if rpoint.Longitude != nil { point.Longitude = *rpoint.Longitude } Points.Update(bson.M{"id": id}, point) if err != nil { fmt.Println(err) routes.ServeJson(w, badRequestError) return } routes.ServeJson(w, point) }
func ParseJsonWatch(req *http.Request) (w *watchdb.Watch, err error) { jw := &JsonWatch{} err = routes.ReadJson(req, jw) if err != nil { return } if jw.DataSourceName == "" { err = errors.New("missing provider") return } dsn := jw.DataSourceName path := jw.MatchExpr.Path method := jw.MatchExpr.Method tag := jw.Tag echo := ParseJsonEcho(jw.Echo) w = watchdb.NewWatch(dsn, path, method, tag, echo) return }
// POST /points func newPoint(w http.ResponseWriter, r *http.Request) { badRequestError := Response{"error", 400, "Bad Request", nil} var ( point Point rpoint PointFromRequest ) routes.ReadJson(r, &rpoint) if rpoint.Ssid == nil || rpoint.Latitude == nil || rpoint.Longitude == nil { routes.ServeJson(w, badRequestError) return } point.Ssid = *rpoint.Ssid point.Latitude = *rpoint.Latitude point.Longitude = *rpoint.Longitude point.Id = GetCounter("points") IncCounter("points") if rpoint.Mac != nil { point.Mac = *rpoint.Mac } if rpoint.Password != nil { point.Password = *rpoint.Password } err := Points.Insert(&point) if err != nil { routes.ServeJson(w, badRequestError) return } routes.ServeJson(w, point) }