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.
 
 
 
 

110 lines
3.0 KiB

  1. package controller
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "log/slog"
  7. "net/http"
  8. "strconv"
  9. "github.com/AFASystems/presence/internal/pkg/api/response"
  10. "github.com/AFASystems/presence/internal/pkg/service"
  11. "github.com/AFASystems/presence/internal/pkg/validation"
  12. "github.com/gorilla/mux"
  13. "gorm.io/gorm"
  14. )
  15. func AlertsListController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  16. return func(w http.ResponseWriter, r *http.Request) {
  17. query := r.URL.Query()
  18. lStr := query.Get("limit")
  19. if lStr == "" {
  20. lStr = "100"
  21. }
  22. limit, err := strconv.Atoi(lStr)
  23. if err != nil {
  24. slog.Error("invalid limit parameter", "value", lStr)
  25. response.BadRequest(w, "invalid limit parameter")
  26. return
  27. }
  28. oStr := query.Get("offset")
  29. if oStr == "" {
  30. oStr = "0"
  31. }
  32. offset, err := strconv.Atoi(oStr)
  33. if err != nil {
  34. slog.Error("invalid offset parameter", "value", oStr)
  35. response.BadRequest(w, "invalid offset parameter")
  36. return
  37. }
  38. alerts, err := service.GetAllAlerts(db, ctx, limit, offset)
  39. if err != nil {
  40. response.InternalError(w, "failed to list alerts", err)
  41. return
  42. }
  43. response.JSON(w, http.StatusOK, alerts)
  44. }
  45. }
  46. func ListAlertsByTrackerIDController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  47. return func(w http.ResponseWriter, r *http.Request) {
  48. id := mux.Vars(r)["id"]
  49. alert, err := service.GetAlertById(id, db, ctx)
  50. if err != nil {
  51. if errors.Is(err, gorm.ErrRecordNotFound) {
  52. response.NotFound(w, "alert not found")
  53. return
  54. }
  55. response.InternalError(w, "failed to get alert", err)
  56. return
  57. }
  58. response.JSON(w, http.StatusOK, alert)
  59. }
  60. }
  61. func AlertDeleteController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  62. return func(w http.ResponseWriter, r *http.Request) {
  63. id := mux.Vars(r)["id"]
  64. if err := service.DeleteAlertByTrackerID(id, db, ctx); err != nil {
  65. response.InternalError(w, "failed to delete alert", err)
  66. return
  67. }
  68. response.JSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  69. }
  70. }
  71. // AlertUpdateStatusController updates an alert's status by id. Body: {"status": "resolved"} (or other status string).
  72. func AlertUpdateStatusController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  73. return func(w http.ResponseWriter, r *http.Request) {
  74. id := mux.Vars(r)["id"]
  75. var body service.AlertPatchBody
  76. if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
  77. response.BadRequest(w, "invalid request body")
  78. return
  79. }
  80. if err := validation.Var(body.Status, "required"); err != nil {
  81. response.BadRequest(w, err.Error())
  82. return
  83. }
  84. if err := validation.Var(body.Operator, "required"); err != nil {
  85. response.BadRequest(w, err.Error())
  86. return
  87. }
  88. if err := service.UpdateAlertStatus(id, body, db, ctx); err != nil {
  89. if errors.Is(err, gorm.ErrRecordNotFound) {
  90. response.NotFound(w, "alert not found")
  91. return
  92. }
  93. response.InternalError(w, "failed to update alert status", err)
  94. return
  95. }
  96. response.JSON(w, http.StatusOK, map[string]string{"status": "updated"})
  97. }
  98. }