Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

106 linhas
2.9 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 struct {
  76. Status string `json:"status"`
  77. }
  78. if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
  79. response.BadRequest(w, "invalid request body")
  80. return
  81. }
  82. if err := validation.Var(body.Status, "required"); err != nil {
  83. response.BadRequest(w, err.Error())
  84. return
  85. }
  86. if err := service.UpdateAlertStatus(id, body.Status, db, ctx); err != nil {
  87. if errors.Is(err, gorm.ErrRecordNotFound) {
  88. response.NotFound(w, "alert not found")
  89. return
  90. }
  91. response.InternalError(w, "failed to update alert status", err)
  92. return
  93. }
  94. response.JSON(w, http.StatusOK, map[string]string{"status": "updated"})
  95. }
  96. }