Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

58 righe
1.6 KiB

  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "github.com/AFASystems/presence/internal/pkg/model"
  6. "gorm.io/gorm"
  7. )
  8. func InsertAlert(alert model.Alert, db *gorm.DB, ctx context.Context) error {
  9. if err := db.WithContext(ctx).Create(&alert).Error; err != nil {
  10. return err
  11. }
  12. return nil
  13. }
  14. func DeleteAlertByTrackerID(trackerID string, db *gorm.DB, ctx context.Context) error {
  15. if err := db.WithContext(ctx).Where("id = ?", trackerID).Delete(&model.Alert{}).Error; err != nil {
  16. return err
  17. }
  18. return nil
  19. }
  20. func GetAllAlerts(db *gorm.DB, ctx context.Context, limit, offset int) ([]model.Alert, error) {
  21. var alerts []model.Alert
  22. if err := db.WithContext(ctx).Limit(limit).Offset(offset).Find(&alerts).Error; err != nil {
  23. return []model.Alert{}, err
  24. }
  25. return alerts, nil
  26. }
  27. func GetAlertById(id string, db *gorm.DB, ctx context.Context) (model.Alert, error) {
  28. var alert model.Alert
  29. if err := db.WithContext(ctx).First(&alert, id).Error; err != nil {
  30. return alert, err
  31. }
  32. return alert, nil
  33. }
  34. type AlertPatchBody struct {
  35. Status string `json:"status"`
  36. Operator string `json:"operator"`
  37. }
  38. // UpdateAlertStatus updates the status of an alert by id. Returns gorm.ErrRecordNotFound if the alert does not exist.
  39. func UpdateAlertStatus(id string, body AlertPatchBody, db *gorm.DB, ctx context.Context) error {
  40. result := db.WithContext(ctx).Model(&model.Alert{}).Where("id = ?", id).Updates(model.Alert{Status: body.Status, Operator: body.Operator, ResolutionTimestamp: time.Now()})
  41. if result.Error != nil {
  42. return result.Error
  43. }
  44. if result.RowsAffected == 0 {
  45. return gorm.ErrRecordNotFound
  46. }
  47. return nil
  48. }