package api import ( "encoding/json" "errors" "git.nemunai.re/filicop/server/models" ) func init() { router.GET("/vehicles/:vid/locations/", apiHandler(vehicleHandler(func (vehicle filicop.Vehicle, _ []byte) (interface{}, error) { return vehicle.GetLocations() }))) router.POST("/vehicles/:vid/locations/", apiHandler(vehicleHandler(addVehicleLocation))) router.DELETE("/vehicles/:vid/locations/", apiHandler(vehicleHandler(func (vehicle filicop.Vehicle, _ []byte) (interface{}, error) { return vehicle.ClearVehicleLocations() }))) router.GET("/fleet/:fid/vehicles/:vid/locations/", apiHandler(vehicleInFleetHandler(func (vehicle filicop.Vehicle, _ []byte) (interface{}, error) { return vehicle.GetLocations() }))) router.POST("/fleet/:fid/vehicles/:vid/locations/", apiHandler(vehicleInFleetHandler(addVehicleLocation))) router.DELETE("/fleet/:fid/vehicles/:vid/locations/", apiHandler(vehicleInFleetHandler(func (vehicle filicop.Vehicle, _ []byte) (interface{}, error) { return vehicle.ClearVehicleLocations() }))) } func parseVehicleLocationJSON(body []byte, next func (filicop.Point) (filicop.VehicleLocation, error)) (filicop.VehicleLocation, error) { var up filicop.Point if err := json.Unmarshal(body, &up); err != nil { return filicop.VehicleLocation{}, err } if up.X == 0 || up.Y == 0 { return filicop.VehicleLocation{}, errors.New("Vehicle's coordinates are invalid") } return next(up) } func addVehicleLocation(vehicle filicop.Vehicle, body []byte) (interface{}, error) { return parseVehicleLocationJSON(body, vehicle.NewLocation) }