From c43416241a8fa9909f0405715c4f9b5190548833 Mon Sep 17 00:00:00 2001 From: kRhythm53 <87687733+kRhythm53@users.noreply.github.com> Date: Tue, 31 May 2022 17:04:04 +0530 Subject: [PATCH] overlapping intervals (#24) * overlapping intervals * commentted changes * fmted Co-authored-by: Rhythm <35167328+kRhythm@users.noreply.github.com> --- handlers/downtimes.go | 6 ++++++ types/downtimes/database.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) 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