77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package bunmodel
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type PlayerSnapshot struct {
|
|
bun.BaseModel `bun:"table:player_snapshots,alias:ps"`
|
|
|
|
ID int `bun:"id,pk,autoincrement,identity"`
|
|
PlayerID int `bun:"player_id,nullzero"`
|
|
NumVillages int `bun:"num_villages"`
|
|
Points int `bun:"points"`
|
|
Rank int `bun:"rank"`
|
|
TribeID int `bun:"tribe_id,nullzero"`
|
|
Tribe Tribe `bun:"tribe,rel:belongs-to,join:tribe_id=id,join:server_key=server_key"`
|
|
ServerKey string `bun:"server_key,nullzero"`
|
|
Date time.Time `bun:"date,nullzero"`
|
|
CreatedAt time.Time `bun:"created_at,nullzero"`
|
|
|
|
OpponentsDefeated
|
|
}
|
|
|
|
func (ps PlayerSnapshot) ToDomain() (domain.PlayerSnapshot, error) {
|
|
od, err := ps.OpponentsDefeated.ToDomain()
|
|
if err != nil {
|
|
return domain.PlayerSnapshot{}, fmt.Errorf(
|
|
"couldn't construct domain.PlayerSnapshot (id=%d): %w",
|
|
ps.ID,
|
|
err,
|
|
)
|
|
}
|
|
|
|
converted, err := domain.UnmarshalPlayerSnapshotFromDatabase(
|
|
ps.ID,
|
|
ps.PlayerID,
|
|
ps.ServerKey,
|
|
ps.NumVillages,
|
|
ps.Points,
|
|
ps.Rank,
|
|
ps.TribeID,
|
|
od,
|
|
ps.Date,
|
|
ps.CreatedAt,
|
|
)
|
|
if err != nil {
|
|
return domain.PlayerSnapshot{}, fmt.Errorf(
|
|
"couldn't construct domain.PlayerSnapshot (id=%d): %w",
|
|
ps.ID,
|
|
err,
|
|
)
|
|
}
|
|
|
|
return converted, nil
|
|
}
|
|
|
|
type PlayerSnapshots []PlayerSnapshot
|
|
|
|
func (pss PlayerSnapshots) ToDomain() (domain.PlayerSnapshots, error) {
|
|
res := make(domain.PlayerSnapshots, 0, len(pss))
|
|
|
|
for _, ps := range pss {
|
|
converted, err := ps.ToDomain()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res = append(res, converted)
|
|
}
|
|
|
|
return res, nil
|
|
}
|