overlapping intervals (#24)

* overlapping intervals

* commentted changes

* fmted

Co-authored-by: Rhythm <35167328+kRhythm@users.noreply.github.com>
pull/1097/head
kRhythm53 2022-05-31 17:04:04 +05:30 committed by GitHub
parent 932f3fdb33
commit c43416241a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -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)

View File

@ -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