diff --git a/handlers/downtimes.go b/handlers/downtimes.go index a409b535..264a5b70 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -108,6 +108,12 @@ func apiCreateDowntimeHandler(w http.ResponseWriter, r *http.Request) { return } + if downtime.CheckOverlapping() { + err := fmt.Errorf("downtime interval overlapping error") + sendErrorJson(err, w, r) + return + } + s, err := services.FindFirstFromDB(downtime.ServiceId) if err != nil { sendErrorJson(err, w, r) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index ab4e237f..47b8f35c 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -74,6 +74,20 @@ func FindLatestDowntimeOfService(service int64) Downtime { return downtime } +func (c *Downtime) CheckOverlapping() bool { + var downtimes []Downtime + q := db.Where("service = ?", c.ServiceId) + q = q.Where("\"end\" IS NULL or \"end\" >= ?", c.Start) + if c.End != nil { + q = q.Where("start <= ?", c.End) + } + q = q.Find(&downtimes) + if len(downtimes) > 0 { + return true + } + return false +} + func FindAll(vars map[string]string) (*[]Downtime, error) { var downtime []Downtime var start time.Time