go-mcm/model/alert.go

46 lines
918 B
Go
Raw Normal View History

2019-10-02 19:03:23 +00:00
package model
import (
"encoding/json"
"git.canopymc.net/Etzelia/go-mcm/internal"
"git.canopymc.net/Etzelia/go-mcm/model/django"
2019-10-02 19:03:23 +00:00
)
type Alert struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Message string `json:"message"`
Seen bool `json:"seen"`
Date string `json:"date"`
Link string `json:"link"`
2019-11-24 21:20:07 +00:00
model *Model `json:"-"`
2019-10-02 19:03:23 +00:00
}
2019-10-03 20:22:22 +00:00
func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
2019-11-24 21:20:07 +00:00
endpoint := q.queryEndpoint("alert", builder.QueryString())
2019-10-02 19:03:23 +00:00
alerts := make([]*Alert, 0)
2019-10-03 20:22:22 +00:00
resp, err := internal.ResponseGet(endpoint)
2019-10-02 19:03:23 +00:00
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &alerts)
if err != nil {
return nil, err
}
2019-11-24 21:20:07 +00:00
for _, alert := range alerts {
alert.model = q
}
2019-10-02 19:03:23 +00:00
return alerts, nil
}
2019-11-24 21:20:07 +00:00
func (a *Alert) Save() (*internal.Status, error) {
endpoint := a.model.endpoint("alert")
return internal.ResponseStatus(endpoint, a.model.Values(a))
}