Gisle Aune
5 years ago
5 changed files with 127 additions and 30 deletions
-
2cmd/lucifer-server/main.go
-
26controllers/group-controller.go
-
106controllers/light-controller.go
-
18database/sqlite/group-repository.go
-
5database/sqlite/light-repository.go
@ -0,0 +1,106 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"database/sql" |
|||
"net/http" |
|||
"strconv" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/httperr" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|||
"git.aiterp.net/lucifer/lucifer/models" |
|||
"github.com/gorilla/mux" |
|||
"golang.org/x/sync/errgroup" |
|||
) |
|||
|
|||
// The LightController is a controller for /api/light/.
|
|||
type LightController struct { |
|||
groups models.GroupRepository |
|||
users models.UserRepository |
|||
lights models.LightRepository |
|||
} |
|||
|
|||
// getLights (`GET /:id`): Get user by id
|
|||
func (c *LightController) getLights(w http.ResponseWriter, r *http.Request) { |
|||
user := models.UserFromContext(r.Context()) |
|||
|
|||
groups, err := c.groups.ListByUser(r.Context(), *user) |
|||
if err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
allLights := make([]models.Light, 0, len(groups)*8) |
|||
|
|||
eg, egCtx := errgroup.WithContext(r.Context()) |
|||
for i := range groups { |
|||
group := groups[i] |
|||
|
|||
eg.Go(func() error { |
|||
lights, err := c.lights.ListByGroup(egCtx, group) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
allLights = append(allLights, lights...) |
|||
|
|||
return nil |
|||
}) |
|||
} |
|||
|
|||
if err := eg.Wait(); err != nil { |
|||
httperr.Respond(w, err) |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, allLights) |
|||
} |
|||
|
|||
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) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The light id "+idStr+" is not valid.") |
|||
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) |
|||
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 { |
|||
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.") |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, light) |
|||
} |
|||
|
|||
// Mount mounts the controller
|
|||
func (c *LightController) Mount(router *mux.Router, prefix string) { |
|||
sub := router.PathPrefix(prefix).Subrouter() |
|||
|
|||
sub.HandleFunc("/", c.getLights).Methods("GET") |
|||
sub.HandleFunc("/{id}", c.getLight).Methods("GET") |
|||
} |
|||
|
|||
// 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} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue