package service import ( "context" "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) ([]model.Alert, error) { var alerts []model.Alert if err := db.WithContext(ctx).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 } // UpdateAlertStatus updates the status of an alert by id. Returns gorm.ErrRecordNotFound if the alert does not exist. func UpdateAlertStatus(id string, status string, db *gorm.DB, ctx context.Context) error { result := db.WithContext(ctx).Model(&model.Alert{}).Where("id = ?", id).Update("status", status) if result.Error != nil { return result.Error } if result.RowsAffected == 0 { return gorm.ErrRecordNotFound } return nil }