go-mcm/model/player.go

48 lines
1.0 KiB
Go
Raw Normal View History

2019-10-02 19:03:23 +00:00
package model
import (
"encoding/json"
2019-10-03 20:22:22 +00:00
"go.etztech.xyz/go-mcm/internal"
2019-10-02 19:03:23 +00:00
"go.etztech.xyz/go-mcm/model/django"
)
type Player struct {
ID int64 `json:"id"`
AuthUserID int64 `json:"auth_user_id"`
UUID string `json:"uuid"`
Username string `json:"username"`
ApplicationID int64 `json:"application_id"`
FirstSeen string `json:"first_seen"`
LastSeen string `json:"last_seen"`
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) Player(builder *django.Builder) ([]*Player, error) {
2019-11-24 21:20:07 +00:00
endpoint := q.queryEndpoint("player", builder.QueryString())
2019-10-02 19:03:23 +00:00
players := make([]*Player, 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, &players)
if err != nil {
return nil, err
}
2019-11-24 21:20:07 +00:00
for _, player := range players {
player.model = q
}
2019-10-02 19:03:23 +00:00
return players, nil
}
2019-11-24 21:20:07 +00:00
func (p *Player) Save() (*internal.Status, error) {
endpoint := p.model.endpoint("player")
return internal.ResponseStatus(endpoint, p.model.Values(p))
}