2024-01-02 09:54:11 +00:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-01-14 10:04:17 +00:00
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/bun/bunmodel"
|
2024-01-02 09:54:11 +00:00
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"github.com/uptrace/bun/dialect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type VillageBunRepository struct {
|
|
|
|
db bun.IDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVillageBunRepository(db bun.IDB) *VillageBunRepository {
|
|
|
|
return &VillageBunRepository{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *VillageBunRepository) CreateOrUpdate(ctx context.Context, params ...domain.CreateVillageParams) error {
|
|
|
|
if len(params) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-06 09:26:56 +00:00
|
|
|
now := time.Now()
|
2024-01-02 09:54:11 +00:00
|
|
|
villages := make(bunmodel.Villages, 0, len(params))
|
|
|
|
|
|
|
|
for _, p := range params {
|
|
|
|
base := p.Base()
|
|
|
|
villages = append(villages, bunmodel.Village{
|
|
|
|
ID: base.ID(),
|
|
|
|
ServerKey: p.ServerKey(),
|
|
|
|
Name: base.Name(),
|
|
|
|
Points: base.Points(),
|
|
|
|
X: base.X(),
|
|
|
|
Y: base.Y(),
|
|
|
|
Continent: base.Continent(),
|
|
|
|
Bonus: base.Bonus(),
|
|
|
|
PlayerID: base.PlayerID(),
|
|
|
|
ProfileURL: base.ProfileURL().String(),
|
2024-01-06 09:26:56 +00:00
|
|
|
CreatedAt: now,
|
2024-01-02 09:54:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
q := repo.db.NewInsert().
|
|
|
|
Model(&villages)
|
|
|
|
|
|
|
|
//nolint:exhaustive
|
|
|
|
switch q.Dialect().Name() {
|
|
|
|
case dialect.PG:
|
|
|
|
q = q.On("CONFLICT ON CONSTRAINT villages_pkey DO UPDATE")
|
|
|
|
case dialect.SQLite:
|
|
|
|
q = q.On("CONFLICT(id, server_key) DO UPDATE")
|
|
|
|
default:
|
|
|
|
q = q.Err(errors.New("unsupported dialect"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := q.
|
|
|
|
Set("name = EXCLUDED.name").
|
|
|
|
Set("points = EXCLUDED.points").
|
|
|
|
Set("x = EXCLUDED.x").
|
|
|
|
Set("y = EXCLUDED.y").
|
|
|
|
Set("continent = EXCLUDED.continent").
|
|
|
|
Set("bonus = EXCLUDED.bonus").
|
|
|
|
Set("player_id = EXCLUDED.player_id").
|
|
|
|
Set("profile_url = EXCLUDED.profile_url").
|
|
|
|
Returning("").
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
return fmt.Errorf("something went wrong while inserting villages into the db: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-05 06:14:41 +00:00
|
|
|
func (repo *VillageBunRepository) List(
|
|
|
|
ctx context.Context,
|
|
|
|
params domain.ListVillagesParams,
|
|
|
|
) (domain.ListVillagesResult, error) {
|
2024-01-02 09:54:11 +00:00
|
|
|
var villages bunmodel.Villages
|
|
|
|
|
|
|
|
if err := repo.db.NewSelect().
|
|
|
|
Model(&villages).
|
|
|
|
Apply(listVillagesParamsApplier{params: params}.apply).
|
|
|
|
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
2024-03-05 06:14:41 +00:00
|
|
|
return domain.ListVillagesResult{}, fmt.Errorf("couldn't select villages from the db: %w", err)
|
2024-01-02 09:54:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 06:14:41 +00:00
|
|
|
converted, err := villages.ToDomain()
|
|
|
|
if err != nil {
|
|
|
|
return domain.ListVillagesResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain.NewListVillagesResult(separateListResultAndNext(converted, params.Limit()))
|
2024-01-02 09:54:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:35:26 +00:00
|
|
|
func (repo *VillageBunRepository) ListWithRelations(
|
|
|
|
ctx context.Context,
|
|
|
|
params domain.ListVillagesParams,
|
|
|
|
) (domain.ListVillagesWithRelationsResult, error) {
|
|
|
|
var villages bunmodel.Villages
|
|
|
|
|
|
|
|
if err := repo.db.NewSelect().
|
|
|
|
Model(&villages).
|
|
|
|
Apply(listVillagesParamsApplier{params: params}.apply).
|
|
|
|
Relation("Player", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
return q.Column(bunmodel.PlayerMetaColumns...)
|
|
|
|
}).
|
|
|
|
Relation("Player.Tribe", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
return q.Column(bunmodel.TribeMetaColumns...)
|
|
|
|
}).
|
|
|
|
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return domain.ListVillagesWithRelationsResult{}, fmt.Errorf("couldn't select players from the db: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
converted, err := villages.ToDomainWithRelations()
|
|
|
|
if err != nil {
|
|
|
|
return domain.ListVillagesWithRelationsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain.NewListVillagesWithRelationsResult(separateListResultAndNext(converted, params.Limit()))
|
|
|
|
}
|
|
|
|
|
2024-01-02 09:54:11 +00:00
|
|
|
func (repo *VillageBunRepository) Delete(ctx context.Context, serverKey string, ids ...int) error {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := repo.db.NewDelete().
|
|
|
|
Model((*bunmodel.Village)(nil)).
|
|
|
|
Where("id IN (?)", bun.In(ids)).
|
|
|
|
Where("server_key = ?", serverKey).
|
|
|
|
Returning("").
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
return fmt.Errorf("couldn't delete villages: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type listVillagesParamsApplier struct {
|
|
|
|
params domain.ListVillagesParams
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (a listVillagesParamsApplier) apply(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
if ids := a.params.IDs(); len(ids) > 0 {
|
|
|
|
q = q.Where("village.id IN (?)", bun.In(ids))
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverKeys := a.params.ServerKeys(); len(serverKeys) > 0 {
|
|
|
|
q = q.Where("village.server_key IN (?)", bun.In(serverKeys))
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:14:31 +00:00
|
|
|
if coords := a.params.Coords(); len(coords) > 0 {
|
|
|
|
converted := make([][]int, 0, len(coords))
|
|
|
|
for _, c := range coords {
|
|
|
|
converted = append(converted, []int{c.X(), c.Y()})
|
|
|
|
}
|
|
|
|
|
|
|
|
q = q.Where("(village.x, village.y) in (?)", bun.In(converted))
|
|
|
|
}
|
|
|
|
|
2024-03-09 07:38:23 +00:00
|
|
|
if playerIDs := a.params.PlayerIDs(); len(playerIDs) > 0 {
|
|
|
|
q = q.Where("village.player_id IN (?)", bun.In(playerIDs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tribeIDs := a.params.TribeIDs(); len(tribeIDs) > 0 {
|
|
|
|
q = q.Relation("Player.Tribe", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
return q.ExcludeColumn("*")
|
|
|
|
}).
|
|
|
|
Where("player__tribe.id IN (?)", bun.In(tribeIDs))
|
|
|
|
}
|
|
|
|
|
2024-01-02 09:54:11 +00:00
|
|
|
for _, s := range a.params.Sort() {
|
2024-03-14 07:06:17 +00:00
|
|
|
column, dir, err := a.sortToColumnAndDirection(s)
|
|
|
|
if err != nil {
|
|
|
|
return q.Err(err)
|
2024-01-02 09:54:11 +00:00
|
|
|
}
|
2024-03-14 07:06:17 +00:00
|
|
|
|
|
|
|
q.OrderExpr("? ?", column, dir.Bun())
|
2024-01-02 09:54:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 06:14:41 +00:00
|
|
|
return q.Limit(a.params.Limit() + 1).Apply(a.applyCursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a listVillagesParamsApplier) applyCursor(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
cursor := a.params.Cursor()
|
|
|
|
|
|
|
|
if cursor.IsZero() {
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
sort := a.params.Sort()
|
|
|
|
cursorApplier := cursorPaginationApplier{
|
|
|
|
data: make([]cursorPaginationApplierDataElement, 0, len(sort)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range sort {
|
2024-03-14 07:06:17 +00:00
|
|
|
var err error
|
2024-03-05 06:14:41 +00:00
|
|
|
var el cursorPaginationApplierDataElement
|
|
|
|
|
2024-03-14 07:06:17 +00:00
|
|
|
el.column, el.direction, err = a.sortToColumnAndDirection(s)
|
|
|
|
if err != nil {
|
|
|
|
return q.Err(err)
|
|
|
|
}
|
|
|
|
|
2024-03-05 06:14:41 +00:00
|
|
|
switch s {
|
2024-03-14 07:06:17 +00:00
|
|
|
case domain.VillageSortIDASC,
|
|
|
|
domain.VillageSortIDDESC:
|
2024-03-05 06:14:41 +00:00
|
|
|
el.value = cursor.ID()
|
|
|
|
el.unique = true
|
2024-03-14 07:06:17 +00:00
|
|
|
case domain.VillageSortServerKeyASC,
|
|
|
|
domain.VillageSortServerKeyDESC:
|
2024-03-05 06:14:41 +00:00
|
|
|
el.value = cursor.ServerKey()
|
|
|
|
default:
|
|
|
|
return q.Err(fmt.Errorf("%s: %w", s.String(), errInvalidSortValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorApplier.data = append(cursorApplier.data, el)
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.Apply(cursorApplier.apply)
|
2024-01-02 09:54:11 +00:00
|
|
|
}
|
2024-03-14 07:06:17 +00:00
|
|
|
|
|
|
|
func (a listVillagesParamsApplier) sortToColumnAndDirection(
|
|
|
|
s domain.VillageSort,
|
|
|
|
) (bun.Safe, sortDirection, error) {
|
|
|
|
switch s {
|
|
|
|
case domain.VillageSortIDASC:
|
|
|
|
return "village.id", sortDirectionASC, nil
|
|
|
|
case domain.VillageSortIDDESC:
|
|
|
|
return "village.id", sortDirectionDESC, nil
|
|
|
|
case domain.VillageSortServerKeyASC:
|
|
|
|
return "village.server_key", sortDirectionASC, nil
|
|
|
|
case domain.VillageSortServerKeyDESC:
|
|
|
|
return "village.server_key", sortDirectionDESC, nil
|
|
|
|
default:
|
|
|
|
return "", 0, fmt.Errorf("%s: %w", s.String(), errInvalidSortValue)
|
|
|
|
}
|
|
|
|
}
|