|
|
@ -2,9 +2,12 @@ package controllers |
|
|
|
|
|
|
|
import ( |
|
|
|
"database/sql" |
|
|
|
"encoding/json" |
|
|
|
"net/http" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"git.aiterp.net/lucifer/lucifer/light" |
|
|
|
|
|
|
|
"git.aiterp.net/lucifer/lucifer/internal/httperr" |
|
|
|
|
|
|
|
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|
|
@ -15,9 +18,10 @@ import ( |
|
|
|
|
|
|
|
// The LightController is a controller for /api/light/.
|
|
|
|
type LightController struct { |
|
|
|
groups models.GroupRepository |
|
|
|
users models.UserRepository |
|
|
|
lights models.LightRepository |
|
|
|
service *light.Service |
|
|
|
groups models.GroupRepository |
|
|
|
users models.UserRepository |
|
|
|
lights models.LightRepository |
|
|
|
} |
|
|
|
|
|
|
|
// getLights (`GET /:id`): Get user by id
|
|
|
@ -57,35 +61,89 @@ func (c *LightController) getLights(w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
|
|
|
|
func (c *LightController) getLight(w http.ResponseWriter, r *http.Request) { |
|
|
|
user := models.UserFromContext(r.Context()) |
|
|
|
|
|
|
|
idStr := mux.Vars(r)["id"] |
|
|
|
id, err := strconv.ParseInt(idStr, 10, 32) |
|
|
|
_, light, err := c.findLight(r) |
|
|
|
if err != nil { |
|
|
|
respond.Error(w, http.StatusBadRequest, "invalid_id", "The light id "+idStr+" is not valid.") |
|
|
|
httperr.Respond(w, err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
light, err := c.lights.FindByID(r.Context(), int(id)) |
|
|
|
if err == sql.ErrNoRows { |
|
|
|
httperr.Respond(w, httperr.NotFound("Light")) |
|
|
|
return |
|
|
|
} else if err != nil { |
|
|
|
httperr.Respond(w, err) |
|
|
|
respond.Data(w, light) |
|
|
|
} |
|
|
|
|
|
|
|
func (c *LightController) updateLight(w http.ResponseWriter, r *http.Request) { |
|
|
|
patch := struct { |
|
|
|
Color *string `json:"color,omitempty"` |
|
|
|
Brightness *int `json:"brightness,omitempty"` |
|
|
|
On *bool `json:"on,omitempty"` |
|
|
|
Name *string `json:"name,omitempty"` |
|
|
|
GroupID *int `json:"groupId,omitempty"` |
|
|
|
}{} |
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil { |
|
|
|
respond.Error(w, http.StatusBadRequest, "invalid_json", "Input is not valid JSON.") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
group, err := c.groups.FindByID(r.Context(), light.GroupID) |
|
|
|
if err == sql.ErrNoRows { |
|
|
|
httperr.Respond(w, httperr.NotFound("Group")) |
|
|
|
return |
|
|
|
} else if err != nil { |
|
|
|
group, light, err := c.findLight(r) |
|
|
|
if err != nil { |
|
|
|
httperr.Respond(w, err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if !group.Permission(user.ID).Read { |
|
|
|
respond.Error(w, http.StatusForbidden, "permission_denied", "You do not have permission to see this light.") |
|
|
|
if patch.Color != nil { |
|
|
|
err := light.SetColor(*patch.Color) |
|
|
|
if err != nil { |
|
|
|
httperr.Respond(w, err) |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if patch.Name != nil { |
|
|
|
if len(*patch.Name) == 0 { |
|
|
|
respond.Error(w, 400, "invalid_name", "The name is empty.") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
light.Name = *patch.Name |
|
|
|
} |
|
|
|
if patch.Brightness != nil { |
|
|
|
if *patch.Brightness < 0 || *patch.Brightness > 255 { |
|
|
|
respond.Error(w, 400, "invalid_brightness", "The brightness must be a value between 0-255 inclusive.") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
light.Brightness = uint8(*patch.Brightness) |
|
|
|
} |
|
|
|
if patch.On != nil { |
|
|
|
light.On = *patch.On |
|
|
|
} |
|
|
|
if patch.GroupID != nil && *patch.GroupID != light.GroupID { |
|
|
|
user := models.UserFromContext(r.Context()) |
|
|
|
|
|
|
|
if !group.Permission(user.ID).Delete { |
|
|
|
respond.Error(w, 403, "cannot_move_out", "You are not permitted to delete lights from group.") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Anyone is allowed to move lights TO group 0 (Lonely Lights) as it's the closest thing there is
|
|
|
|
// to deleting lights.
|
|
|
|
if *patch.GroupID != 0 { |
|
|
|
targetGroup, err := c.groups.FindByID(r.Context(), *patch.GroupID) |
|
|
|
if err != nil { |
|
|
|
respond.Error(w, 404, "group_not_found", "The group could not be found.") |
|
|
|
return |
|
|
|
} |
|
|
|
if !targetGroup.Permission(user.ID).Create { |
|
|
|
respond.Error(w, 403, "cannot_move_in", "You are not permitted to create lights in target group.") |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
light.GroupID = *patch.GroupID |
|
|
|
} |
|
|
|
|
|
|
|
err = c.service.UpdateLight(r.Context(), light) |
|
|
|
if err != nil { |
|
|
|
httperr.Respond(w, err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -98,9 +156,40 @@ func (c *LightController) Mount(router *mux.Router, prefix string) { |
|
|
|
|
|
|
|
sub.HandleFunc("/", c.getLights).Methods("GET") |
|
|
|
sub.HandleFunc("/{id}", c.getLight).Methods("GET") |
|
|
|
sub.HandleFunc("/{id}", c.updateLight).Methods("PATCH", "PUT") |
|
|
|
} |
|
|
|
|
|
|
|
func (c *LightController) findLight(r *http.Request) (models.Group, models.Light, error) { |
|
|
|
user := models.UserFromContext(r.Context()) |
|
|
|
|
|
|
|
idStr := mux.Vars(r)["id"] |
|
|
|
id, err := strconv.ParseInt(idStr, 10, 32) |
|
|
|
if err != nil { |
|
|
|
return models.Group{}, models.Light{}, &httperr.Error{Status: http.StatusForbidden, Kind: "invalid_id", Message: "The light id " + idStr + " is not valid."} |
|
|
|
} |
|
|
|
|
|
|
|
light, err := c.lights.FindByID(r.Context(), int(id)) |
|
|
|
if err == sql.ErrNoRows { |
|
|
|
return models.Group{}, models.Light{}, httperr.NotFound("Light") |
|
|
|
} else if err != nil { |
|
|
|
return models.Group{}, models.Light{}, err |
|
|
|
} |
|
|
|
|
|
|
|
group, err := c.groups.FindByID(r.Context(), light.GroupID) |
|
|
|
if err == sql.ErrNoRows { |
|
|
|
return models.Group{}, models.Light{}, httperr.NotFound("Group") |
|
|
|
} else if err != nil { |
|
|
|
return models.Group{}, models.Light{}, err |
|
|
|
} |
|
|
|
|
|
|
|
if !group.Permission(user.ID).Read { |
|
|
|
return models.Group{}, models.Light{}, &httperr.Error{Status: http.StatusForbidden, Kind: "permission_denied", Message: "Thou canst not see the light."} |
|
|
|
} |
|
|
|
|
|
|
|
return group, light, nil |
|
|
|
} |
|
|
|
|
|
|
|
// NewLightController creates a new LightController.
|
|
|
|
func NewLightController(groups models.GroupRepository, users models.UserRepository, lights models.LightRepository) *LightController { |
|
|
|
return &LightController{groups: groups, users: users, lights: lights} |
|
|
|
func NewLightController(service *light.Service, groups models.GroupRepository, users models.UserRepository, lights models.LightRepository) *LightController { |
|
|
|
return &LightController{service: service, groups: groups, users: users, lights: lights} |
|
|
|
} |