All checks were successful
ci/woodpecker/push/govulncheck Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful
ci/woodpecker/tag/deployment Pipeline was successful
ci/woodpecker/cron/govulncheck Pipeline was successful
Reviewed-on: #52
207 lines
5.8 KiB
Go
207 lines
5.8 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/bun/bunmodel"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type ServerSnapshotBunRepository struct {
|
|
db bun.IDB
|
|
}
|
|
|
|
func NewServerSnapshotBunRepository(db bun.IDB) *ServerSnapshotBunRepository {
|
|
return &ServerSnapshotBunRepository{db: db}
|
|
}
|
|
|
|
func (repo *ServerSnapshotBunRepository) Create(
|
|
ctx context.Context,
|
|
params ...domain.CreateServerSnapshotParams,
|
|
) error {
|
|
if len(params) == 0 {
|
|
return nil
|
|
}
|
|
|
|
now := time.Now()
|
|
snapshots := make(bunmodel.ServerSnapshots, 0, len(params))
|
|
|
|
for _, p := range params {
|
|
snapshots = append(snapshots, bunmodel.ServerSnapshot{
|
|
ServerKey: p.ServerKey(),
|
|
NumPlayers: p.NumPlayers(),
|
|
NumActivePlayers: p.NumActivePlayers(),
|
|
NumInactivePlayers: p.NumInactivePlayers(),
|
|
NumTribes: p.NumTribes(),
|
|
NumActiveTribes: p.NumActiveTribes(),
|
|
NumInactiveTribes: p.NumInactiveTribes(),
|
|
NumVillages: p.NumVillages(),
|
|
NumPlayerVillages: p.NumPlayerVillages(),
|
|
NumBarbarianVillages: p.NumBarbarianVillages(),
|
|
NumBonusVillages: p.NumBonusVillages(),
|
|
Date: p.Date(),
|
|
CreatedAt: now,
|
|
})
|
|
}
|
|
|
|
if _, err := repo.db.NewInsert().
|
|
Model(&snapshots).
|
|
Ignore().
|
|
Returning("").
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("something went wrong while inserting server snapshots into the db: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (repo *ServerSnapshotBunRepository) List(
|
|
ctx context.Context,
|
|
params domain.ListServerSnapshotsParams,
|
|
) (domain.ListServerSnapshotsResult, error) {
|
|
var serverSnapshots bunmodel.ServerSnapshots
|
|
|
|
if err := repo.db.NewSelect().
|
|
Model(&serverSnapshots).
|
|
Apply(listServerSnapshotsParamsApplier{params: params}.apply).
|
|
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return domain.ListServerSnapshotsResult{}, fmt.Errorf("couldn't select server snapshots from the db: %w", err)
|
|
}
|
|
|
|
converted, err := serverSnapshots.ToDomain()
|
|
if err != nil {
|
|
return domain.ListServerSnapshotsResult{}, err
|
|
}
|
|
|
|
return domain.NewListServerSnapshotsResult(separateListResultAndNext(converted, params.Limit()))
|
|
}
|
|
|
|
func (repo *ServerSnapshotBunRepository) ListWithRelations(
|
|
ctx context.Context,
|
|
params domain.ListServerSnapshotsParams,
|
|
) (domain.ListServerSnapshotsWithRelationsResult, error) {
|
|
var serverSnapshots bunmodel.ServerSnapshots
|
|
|
|
if err := repo.db.NewSelect().
|
|
Model(&serverSnapshots).
|
|
Apply(listServerSnapshotsParamsApplier{params: params}.apply).
|
|
Relation("Server", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
return q.Column(bunmodel.ServerMetaColumns...)
|
|
}).
|
|
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return domain.ListServerSnapshotsWithRelationsResult{}, fmt.Errorf(
|
|
"couldn't select server snapshots from the db: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
converted, err := serverSnapshots.ToDomainWithRelations()
|
|
if err != nil {
|
|
return domain.ListServerSnapshotsWithRelationsResult{}, err
|
|
}
|
|
|
|
return domain.NewListServerSnapshotsWithRelationsResult(separateListResultAndNext(converted, params.Limit()))
|
|
}
|
|
|
|
func (repo *ServerSnapshotBunRepository) Delete(ctx context.Context, serverKey string, dateLTE time.Time) error {
|
|
if _, err := repo.db.NewDelete().
|
|
Model((*bunmodel.ServerSnapshot)(nil)).
|
|
Where("server_key = ?", serverKey).
|
|
Where("date <= ?", dateLTE).
|
|
Returning("NULL").
|
|
Exec(ctx); err != nil {
|
|
return fmt.Errorf("couldn't delete server snapshots: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type listServerSnapshotsParamsApplier struct {
|
|
params domain.ListServerSnapshotsParams
|
|
}
|
|
|
|
func (a listServerSnapshotsParamsApplier) apply(q *bun.SelectQuery) *bun.SelectQuery {
|
|
if serverKeys := a.params.ServerKeys(); len(serverKeys) > 0 {
|
|
q = q.Where("ss.server_key IN (?)", bun.In(serverKeys))
|
|
}
|
|
|
|
for _, s := range a.params.Sort() {
|
|
column, dir, err := a.sortToColumnAndDirection(s)
|
|
if err != nil {
|
|
return q.Err(err)
|
|
}
|
|
|
|
q.OrderExpr("? ?", column, dir.Bun())
|
|
}
|
|
|
|
return q.Limit(a.params.Limit() + 1).Apply(a.applyCursor)
|
|
}
|
|
|
|
func (a listServerSnapshotsParamsApplier) 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 {
|
|
var err error
|
|
var el cursorPaginationApplierDataElement
|
|
|
|
el.column, el.direction, err = a.sortToColumnAndDirection(s)
|
|
if err != nil {
|
|
return q.Err(err)
|
|
}
|
|
|
|
switch s {
|
|
case domain.ServerSnapshotSortIDASC,
|
|
domain.ServerSnapshotSortIDDESC:
|
|
el.value = cursor.ID()
|
|
el.unique = true
|
|
case domain.ServerSnapshotSortServerKeyASC,
|
|
domain.ServerSnapshotSortServerKeyDESC:
|
|
el.value = cursor.ServerKey()
|
|
case domain.ServerSnapshotSortDateASC,
|
|
domain.ServerSnapshotSortDateDESC:
|
|
el.value = cursor.Date()
|
|
default:
|
|
return q.Err(fmt.Errorf("%s: %w", s.String(), errInvalidSortValue))
|
|
}
|
|
|
|
cursorApplier.data = append(cursorApplier.data, el)
|
|
}
|
|
|
|
return q.Apply(cursorApplier.apply)
|
|
}
|
|
|
|
func (a listServerSnapshotsParamsApplier) sortToColumnAndDirection(
|
|
s domain.ServerSnapshotSort,
|
|
) (bun.Safe, sortDirection, error) {
|
|
switch s {
|
|
case domain.ServerSnapshotSortDateASC:
|
|
return "ss.date", sortDirectionASC, nil
|
|
case domain.ServerSnapshotSortDateDESC:
|
|
return "ss.date", sortDirectionDESC, nil
|
|
case domain.ServerSnapshotSortIDASC:
|
|
return "ss.id", sortDirectionASC, nil
|
|
case domain.ServerSnapshotSortIDDESC:
|
|
return "ss.id", sortDirectionDESC, nil
|
|
case domain.ServerSnapshotSortServerKeyASC:
|
|
return "ss.server_key", sortDirectionASC, nil
|
|
case domain.ServerSnapshotSortServerKeyDESC:
|
|
return "ss.server_key", sortDirectionDESC, nil
|
|
default:
|
|
return "", 0, fmt.Errorf("%s: %w", s.String(), errInvalidSortValue)
|
|
}
|
|
}
|