package data import ( "context" "database/sql" "time" ) type AlertRecord struct { AlertType string StationID string Level string IssuedAt time.Time Message string SMSPhone sql.NullString } // InsertAlert writes one alert row; returns inserted id or 0 when skipped by conflict. func InsertAlert(ctx context.Context, ar AlertRecord) (int64, error) { const q = ` INSERT INTO alerts (alert_type, station_id, level, issued_at, message, sms_phone) VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING id` var id int64 err := DB().QueryRowContext(ctx, q, ar.AlertType, ar.StationID, ar.Level, ar.IssuedAt, ar.Message, ar.SMSPhone, ).Scan(&id) if err == sql.ErrNoRows { return 0, nil } if err != nil { return 0, err } return id, nil }