/
alert.go
41 lines (30 loc) · 788 Bytes
/
alert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package repositories
import (
"github.com/train-cat/api-train/app/filters"
"github.com/train-cat/api-train/app/models"
)
type alert struct{}
// Alert namespace
var Alert alert
// Persist on alert in database
func (r alert) Persist(s *models.StopTime, i *models.AlertInput) (*models.Alert, error) {
a := i.ToEntity()
var err error
a.CodeTrain = s.Trip.Code
a.StationID = s.StationID
a.Station = s.Station
a.Action, err = Action.FindOne(*a.ActionID)
if err != nil {
return nil, err
}
err = db.Save(a).Error
return a, err
}
// FindAll alerts filtered
func (r alert) FindAll(f filters.Filter) (*models.Collection, error) {
alerts := []*models.Alert{}
db := db.Model(models.Alert{}).
Preload("Station").
Preload("Action")
return NewCollection(f, db, &alerts)
}