The main server, and probably only repository in this org.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.8 KiB

  1. package controllers
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "git.aiterp.net/lucifer/lucifer/internal/respond"
  9. "git.aiterp.net/lucifer/lucifer/models"
  10. "github.com/gorilla/mux"
  11. )
  12. // The GroupController is a controller for all user inports.
  13. type GroupController struct {
  14. groups models.GroupRepository
  15. users models.UserRepository
  16. lights models.LightRepository
  17. }
  18. // getGroups (`GET /:id`): Get user by id
  19. func (c *GroupController) getGroups(w http.ResponseWriter, r *http.Request) {
  20. session := models.SessionFromContext(r.Context())
  21. if session == nil {
  22. respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in")
  23. return
  24. }
  25. user, err := c.users.FindByID(r.Context(), session.UserID)
  26. if err != nil {
  27. respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in")
  28. return
  29. }
  30. groups, err := c.groups.ListByUser(r.Context(), user)
  31. if err != nil && err != sql.ErrNoRows {
  32. log.Printf("Getting groups for user %s (%d) failed: %s", user.Name, user.ID, err)
  33. respond.Error(w, http.StatusInternalServerError, "internal_error", "Failed to get groups.")
  34. return
  35. }
  36. respond.Data(w, groups)
  37. }
  38. // getGroup (`GET /:id`): Get user by id
  39. func (c *GroupController) getGroup(w http.ResponseWriter, r *http.Request) {
  40. session := models.SessionFromContext(r.Context())
  41. if session == nil {
  42. respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in")
  43. return
  44. }
  45. idStr := mux.Vars(r)["id"]
  46. id, err := strconv.ParseInt(idStr, 10, 32)
  47. if err != nil {
  48. respond.Error(w, http.StatusBadRequest, "invalid_id", "The id"+idStr+"is not valid.")
  49. return
  50. }
  51. group, err := c.groups.FindByID(r.Context(), int(id))
  52. if err != nil || !group.Permission(session.UserID).Read {
  53. respond.Error(w, http.StatusNotFound, "not_found", "The group cannot be found or you are not authorized to view it.")
  54. return
  55. }
  56. respond.Data(w, group)
  57. }
  58. // getGroupLights (`GET /:id/light/`): Get user by id
  59. func (c *GroupController) getGroupLights(w http.ResponseWriter, r *http.Request) {
  60. session := models.SessionFromContext(r.Context())
  61. if session == nil {
  62. respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in")
  63. return
  64. }
  65. idStr := mux.Vars(r)["id"]
  66. id, err := strconv.ParseInt(idStr, 10, 32)
  67. if err != nil {
  68. respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.")
  69. return
  70. }
  71. group, err := c.groups.FindByID(r.Context(), int(id))
  72. if err != nil || !group.Permission(session.UserID).Read {
  73. respond.Error(w, http.StatusNotFound, "group_not_found", "The group cannot be found or you are not authorized to view it.")
  74. return
  75. }
  76. lights, err := c.lights.ListByGroup(r.Context(), group)
  77. if err != nil && err != sql.ErrNoRows {
  78. log.Printf("Getting lights for group %s (%d) failed: %s", group.Name, group.ID, err)
  79. respond.Error(w, http.StatusInternalServerError, "internal_error", "Failed to get groups.")
  80. return
  81. }
  82. respond.Data(w, lights)
  83. }
  84. // getGroupLight (`GET /:group_id/light/:id`): Get user by id
  85. func (c *GroupController) getGroupLight(w http.ResponseWriter, r *http.Request) {
  86. session := models.SessionFromContext(r.Context())
  87. if session == nil {
  88. respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in")
  89. return
  90. }
  91. groupIDStr := mux.Vars(r)["group_id"]
  92. groupID, err := strconv.ParseInt(groupIDStr, 10, 32)
  93. if err != nil {
  94. respond.Error(w, http.StatusBadRequest, "invalid_id", "The group id "+groupIDStr+" is not valid.")
  95. return
  96. }
  97. idStr := mux.Vars(r)["id"]
  98. id, err := strconv.ParseInt(idStr, 10, 32)
  99. if err != nil {
  100. respond.Error(w, http.StatusBadRequest, "invalid_id", "The light id "+idStr+" is not valid.")
  101. return
  102. }
  103. group, err := c.groups.FindByID(r.Context(), int(groupID))
  104. if err != nil || !group.Permission(session.UserID).Read {
  105. respond.Error(w, http.StatusNotFound, "group_not_found", "The group cannot be found or you are not authorized to view it.")
  106. return
  107. }
  108. light, err := c.lights.FindByID(r.Context(), int(id))
  109. if err != nil || light.GroupID != group.ID {
  110. fmt.Println(light, id)
  111. respond.Error(w, http.StatusNotFound, "light_not_found", "The light cannot be found in this group.")
  112. return
  113. }
  114. respond.Data(w, light)
  115. }
  116. // Mount mounts the controller
  117. func (c *GroupController) Mount(router *mux.Router, prefix string) {
  118. sub := router.PathPrefix(prefix).Subrouter()
  119. sub.HandleFunc("/", c.getGroups).Methods("GET")
  120. sub.HandleFunc("/{id}", c.getGroup).Methods("GET")
  121. sub.HandleFunc("/{id}/light/", c.getGroupLights).Methods("GET")
  122. sub.HandleFunc("/{group_id}/light/{id}", c.getGroupLight).Methods("GET")
  123. }
  124. // NewGroupController creates a new GroupController.
  125. func NewGroupController(groups models.GroupRepository, users models.UserRepository, lights models.LightRepository) *GroupController {
  126. return &GroupController{groups: groups, users: users, lights: lights}
  127. }