statping/core/incidents.go

86 lines
2.2 KiB
Go
Raw Normal View History

2019-06-24 22:21:38 +00:00
package core
import (
"github.com/hunterlong/statping/types"
"time"
)
type Incident struct {
*types.Incident
}
type IncidentUpdate struct {
*types.IncidentUpdate
}
2020-02-01 03:53:00 +00:00
// ReturnIncident returns *core.Incident based off a *types.Incident
func ReturnIncident(u *types.Incident) *Incident {
return &Incident{u}
}
// SelectIncident returns the Incident based on the Incident's ID.
func SelectIncident(id int64) (*Incident, error) {
var incident Incident
2020-02-19 04:07:22 +00:00
err := Database(incident).Where("id = ?", id).First(&incident)
2020-02-01 03:53:00 +00:00
return &incident, err.Error()
}
2019-06-24 22:21:38 +00:00
// AllIncidents will return all incidents and updates recorded
func AllIncidents() []*Incident {
var incidents []*Incident
2020-02-19 04:07:22 +00:00
Database(incidents).Find(&incidents).Order("id desc")
2019-06-24 22:21:38 +00:00
for _, i := range incidents {
var updates []*types.IncidentUpdate
2020-02-19 04:07:22 +00:00
Database(updates).Find(&updates).Order("id desc")
2019-06-24 22:21:38 +00:00
i.Updates = updates
}
return incidents
}
// Incidents will return the all incidents for a service
func (s *Service) Incidents() []*Incident {
var incidentArr []*Incident
2020-02-19 04:07:22 +00:00
Database(incidentArr).Where("service = ?", s.Id).Order("id desc").Find(&incidentArr)
2019-06-24 22:21:38 +00:00
return incidentArr
}
// AllUpdates will return the all updates for an incident
func (i *Incident) AllUpdates() []*IncidentUpdate {
var updatesArr []*IncidentUpdate
2020-02-19 04:07:22 +00:00
Database(updatesArr).Where("incident = ?", i.Id).Order("id desc").Find(&updatesArr)
2019-06-24 22:21:38 +00:00
return updatesArr
}
// Delete will remove a incident
func (i *Incident) Delete() error {
2020-02-19 04:07:22 +00:00
err := Database(i).Delete(i)
2020-01-26 21:01:43 +00:00
return err.Error()
2019-06-24 22:21:38 +00:00
}
// Create will create a incident and insert it into the database
func (i *Incident) Create() (int64, error) {
2020-01-04 02:03:59 +00:00
i.CreatedAt = time.Now().UTC()
2020-02-19 04:07:22 +00:00
db := Database(i).Create(i)
2020-01-26 21:01:43 +00:00
return i.Id, db.Error()
2019-06-24 22:21:38 +00:00
}
// Update will update a incident
func (i *Incident) Update() (int64, error) {
2020-01-04 02:03:59 +00:00
i.UpdatedAt = time.Now().UTC()
2020-02-19 04:07:22 +00:00
db := Database(i).Update(i)
2020-01-26 21:01:43 +00:00
return i.Id, db.Error()
2019-06-24 22:21:38 +00:00
}
// Delete will remove a incident update
func (i *IncidentUpdate) Delete() error {
2020-02-19 04:07:22 +00:00
err := Database(i).Delete(i)
2020-01-26 21:01:43 +00:00
return err.Error()
2019-06-24 22:21:38 +00:00
}
// Create will create a incident update and insert it into the database
func (i *IncidentUpdate) Create() (int64, error) {
2020-01-04 02:03:59 +00:00
i.CreatedAt = time.Now().UTC()
2020-02-19 04:07:22 +00:00
db := Database(i).Create(i)
2020-01-26 21:01:43 +00:00
return i.Id, db.Error()
2019-06-24 22:21:38 +00:00
}