2024-01-15 07:00:08 +00:00
|
|
|
package bunmodel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TribeSnapshot struct {
|
|
|
|
bun.BaseModel `bun:"table:tribe_snapshots,alias:ts"`
|
|
|
|
|
|
|
|
ID int `bun:"id,pk,autoincrement,identity"`
|
|
|
|
TribeID int `bun:"tribe_id,nullzero"`
|
|
|
|
ServerKey string `bun:"server_key,nullzero"`
|
2024-03-19 06:01:38 +00:00
|
|
|
Tribe Tribe `bun:"tribe,rel:belongs-to,join:tribe_id=id,join:server_key=server_key"`
|
2024-01-15 07:00:08 +00:00
|
|
|
NumMembers int `bun:"num_members"`
|
|
|
|
NumVillages int `bun:"num_villages"`
|
|
|
|
Points int `bun:"points"`
|
|
|
|
AllPoints int `bun:"all_points"`
|
|
|
|
Rank int `bun:"rank"`
|
|
|
|
Dominance float64 `bun:"dominance"`
|
|
|
|
Date time.Time `bun:"date,nullzero"`
|
|
|
|
CreatedAt time.Time `bun:"created_at,nullzero"`
|
|
|
|
|
|
|
|
OpponentsDefeated
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts TribeSnapshot) ToDomain() (domain.TribeSnapshot, error) {
|
|
|
|
od, err := ts.OpponentsDefeated.ToDomain()
|
|
|
|
if err != nil {
|
|
|
|
return domain.TribeSnapshot{}, fmt.Errorf(
|
|
|
|
"couldn't construct domain.TribeSnapshot (id=%d): %w",
|
|
|
|
ts.ID,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
converted, err := domain.UnmarshalTribeSnapshotFromDatabase(
|
|
|
|
ts.ID,
|
|
|
|
ts.TribeID,
|
|
|
|
ts.ServerKey,
|
|
|
|
ts.NumMembers,
|
|
|
|
ts.NumVillages,
|
|
|
|
ts.Points,
|
|
|
|
ts.AllPoints,
|
|
|
|
ts.Rank,
|
|
|
|
od,
|
|
|
|
ts.Dominance,
|
|
|
|
ts.Date,
|
|
|
|
ts.CreatedAt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return domain.TribeSnapshot{}, fmt.Errorf(
|
|
|
|
"couldn't construct domain.TribeSnapshot (id=%d): %w",
|
|
|
|
ts.ID,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted, nil
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:01:38 +00:00
|
|
|
func (ts TribeSnapshot) ToDomainWithRelations() (domain.TribeSnapshotWithRelations, error) {
|
|
|
|
converted, err := ts.ToDomain()
|
|
|
|
if err != nil {
|
|
|
|
return domain.TribeSnapshotWithRelations{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tribe, err := ts.Tribe.ToMeta()
|
|
|
|
if err != nil {
|
|
|
|
return domain.TribeSnapshotWithRelations{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted.WithRelations(tribe), nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 07:00:08 +00:00
|
|
|
type TribeSnapshots []TribeSnapshot
|
|
|
|
|
|
|
|
func (tss TribeSnapshots) ToDomain() (domain.TribeSnapshots, error) {
|
2024-03-12 06:52:41 +00:00
|
|
|
return sliceToDomain(tss)
|
2024-01-15 07:00:08 +00:00
|
|
|
}
|
2024-03-19 06:01:38 +00:00
|
|
|
|
|
|
|
func (tss TribeSnapshots) ToDomainWithRelations() (domain.TribeSnapshotsWithRelations, error) {
|
|
|
|
return sliceToDomainWithRelations(tss)
|
|
|
|
}
|