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.
 
 
 
 

51 lines
1.4 KiB

  1. package controller
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "github.com/AFASystems/presence/internal/pkg/api/response"
  7. "github.com/AFASystems/presence/internal/pkg/service"
  8. "github.com/gorilla/mux"
  9. "gorm.io/gorm"
  10. )
  11. func AlertsListController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  12. return func(w http.ResponseWriter, r *http.Request) {
  13. alerts, err := service.GetAllAlerts(db, ctx)
  14. if err != nil {
  15. response.InternalError(w, "failed to list alerts", err)
  16. return
  17. }
  18. response.JSON(w, http.StatusOK, alerts)
  19. }
  20. }
  21. func ListAlertsByTrackerIDController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  22. return func(w http.ResponseWriter, r *http.Request) {
  23. id := mux.Vars(r)["id"]
  24. alert, err := service.GetAlertById(id, db, ctx)
  25. if err != nil {
  26. if errors.Is(err, gorm.ErrRecordNotFound) {
  27. response.NotFound(w, "alert not found")
  28. return
  29. }
  30. response.InternalError(w, "failed to get alert", err)
  31. return
  32. }
  33. response.JSON(w, http.StatusOK, alert)
  34. }
  35. }
  36. func AlertDeleteController(db *gorm.DB, ctx context.Context) http.HandlerFunc {
  37. return func(w http.ResponseWriter, r *http.Request) {
  38. id := mux.Vars(r)["id"]
  39. if err := service.DeleteAlertByTrackerID(id, db, ctx); err != nil {
  40. response.InternalError(w, "failed to delete alert", err)
  41. return
  42. }
  43. response.JSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  44. }
  45. }