package service import ( "context" "time" "github.com/AFASystems/presence/internal/pkg/model" "gorm.io/gorm" ) func InsertAlert(alert model.Alert, db *gorm.DB, ctx context.Context) error { if err := db.WithContext(ctx).Create(&alert).Error; err != nil { return err } return nil } func DeleteAlertByTrackerID(trackerID string, db *gorm.DB, ctx context.Context) error { if err := db.WithContext(ctx).Where("id = ?", trackerID).Delete(&model.Alert{}).Error; err != nil { return err } return nil } func GetAllAlerts(db *gorm.DB, ctx context.Context, limit, offset int) ([]model.Alert, error) { var alerts []model.Alert if err := db.WithContext(ctx).Limit(limit).Offset(offset).Find(&alerts).Error; err != nil { return []model.Alert{}, err } return alerts, nil } func GetAlertById(id string, db *gorm.DB, ctx context.Context) (model.Alert, error) { var alert model.Alert if err := db.WithContext(ctx).First(&alert, id).Error; err != nil { return alert, err } return alert, nil } type AlertPatchBody struct { Status string `json:"status"` Operator string `json:"operator"` } // UpdateAlertStatus updates the status of an alert by id. Returns gorm.ErrRecordNotFound if the alert does not exist. func UpdateAlertStatus(id string, body AlertPatchBody, db *gorm.DB, ctx context.Context) error { result := db.WithContext(ctx).Model(&model.Alert{}).Where("id = ?", id).Updates(model.Alert{Status: body.Status, Operator: body.Operator, ResolutionTimestamp: time.Now()}) if result.Error != nil { return result.Error } if result.RowsAffected == 0 { return gorm.ErrRecordNotFound } return nil }