This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/bundb/player_snapshot.go
Dawid Wysokiński badb7276fb
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
refactor: check if len() > 0 instead of != nil for slices
2023-02-08 07:04:22 +01:00

175 lines
4.6 KiB
Go

package bundb
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"gitea.dwysokinski.me/twhelp/core/internal/bundb/internal/model"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
"github.com/uptrace/bun"
)
var (
playerSnapshotOrders = []string{"ps.server_key ASC"}
)
type PlayerSnapshot struct {
db *bun.DB
}
func NewPlayerSnapshot(db *bun.DB) *PlayerSnapshot {
return &PlayerSnapshot{db: db}
}
func (p *PlayerSnapshot) Create(ctx context.Context, params ...domain.CreatePlayerSnapshotParams) error {
if len(params) == 0 {
return nil
}
snapshots := make([]model.PlayerSnapshot, 0, len(params))
for _, param := range params {
snapshots = append(snapshots, model.NewPlayerSnapshot(param))
}
if _, err := p.db.NewInsert().
Model(&snapshots).
Returning("NULL").
Exec(ctx); err != nil {
return fmt.Errorf("something went wrong while inserting player snapshots into the db: %w", err)
}
return nil
}
func (p *PlayerSnapshot) List(ctx context.Context, params domain.ListPlayerSnapshotsParams) ([]domain.PlayerSnapshot, error) {
var snapshots []model.PlayerSnapshot
if err := p.db.NewSelect().
Model(&snapshots).
Order(playerSnapshotOrders...).
Apply(listPlayerSnapshotsParamsApplier{params}.apply).
Scan(ctx); err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("couldn't select player snapshots from the db: %w", err)
}
result := make([]domain.PlayerSnapshot, 0, len(snapshots))
for _, snapshot := range snapshots {
result = append(result, snapshot.ToDomain())
}
return result, nil
}
func (p *PlayerSnapshot) ListCountWithRelations(
ctx context.Context,
params domain.ListPlayerSnapshotsParams,
) ([]domain.PlayerSnapshotWithRelations, int64, error) {
var snapshots []model.PlayerSnapshot
paramsApplier := listPlayerSnapshotsParamsApplier{params}
subQ := p.db.NewSelect().
Column("id").
Model(&model.PlayerSnapshot{}).
Order(playerSnapshotOrders...).
Apply(paramsApplier.apply)
cntQ := p.db.NewSelect().
Model(&model.PlayerSnapshot{}).
Apply(paramsApplier.applyFilters)
q := p.db.NewSelect().
Model(&snapshots).
Order(playerSnapshotOrders...).
Apply(paramsApplier.applySort).
Where("ps.id IN (?)", subQ).
Relation("Tribe", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column(tribeMetaColumns...)
})
count, err := scanAndCount(ctx, cntQ, q)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, 0, fmt.Errorf("couldn't select player snapshots from the db: %w", err)
}
result := make([]domain.PlayerSnapshotWithRelations, 0, len(snapshots))
for _, snapshot := range snapshots {
result = append(result, snapshot.ToDomainWithRelations())
}
return result, int64(count), nil
}
func (p *PlayerSnapshot) Delete(ctx context.Context, serverKey string, createdAtLTE time.Time) error {
if _, err := p.db.NewDelete().
Model(&model.PlayerSnapshot{}).
Where("server_key = ?", serverKey).
Where("created_at <= ?", createdAtLTE).
Returning("NULL").
Exec(ctx); err != nil {
return fmt.Errorf("couldn't delete player snapshots: %w", err)
}
return nil
}
type listPlayerSnapshotsParamsApplier struct {
params domain.ListPlayerSnapshotsParams
}
func (l listPlayerSnapshotsParamsApplier) apply(q *bun.SelectQuery) *bun.SelectQuery {
return q.Apply(l.applySort).Apply(l.applyFilters).Apply(l.applyPagination)
}
func (l listPlayerSnapshotsParamsApplier) applyFilters(q *bun.SelectQuery) *bun.SelectQuery {
if len(l.params.ServerKeys) > 0 {
q = q.Where("ps.server_key IN (?)", bun.In(l.params.ServerKeys))
}
if len(l.params.PlayerIDs) > 0 {
q = q.Where("ps.player_id IN (?)", bun.In(l.params.PlayerIDs))
}
return q
}
func (l listPlayerSnapshotsParamsApplier) applySort(q *bun.SelectQuery) *bun.SelectQuery {
if len(l.params.Sort) == 0 {
return q
}
orders := make([]string, 0, len(l.params.Sort))
for i, s := range l.params.Sort {
column, err := playerSnapshotSortByToColumn(s.By)
if err != nil {
return q.Err(fmt.Errorf("playerSnapshotSortByToColumn (index=%d): %w", i, err))
}
direction, err := sortDirectionToString(s.Direction)
if err != nil {
return q.Err(fmt.Errorf("sortDirectionToString (index=%d): %w", i, err))
}
orders = append(orders, column+" "+direction)
}
return q.Order(orders...)
}
func (l listPlayerSnapshotsParamsApplier) applyPagination(q *bun.SelectQuery) *bun.SelectQuery {
return (paginationApplier{pagination: l.params.Pagination}).apply(q)
}
func playerSnapshotSortByToColumn(sortBy domain.PlayerSnapshotSortBy) (string, error) {
switch sortBy {
case domain.PlayerSnapshotSortByID:
return "ps.id", nil
case domain.PlayerSnapshotSortByDate:
return "ps.date", nil
}
return "", fmt.Errorf("%w: %d", domain.ErrUnsupportedSortBy, sortBy)
}