Skip to content

Commit

Permalink
refactor: use db selector
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Feb 10, 2023
1 parent 5741bab commit 9d0006c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 59 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/ahmetb/go-linq/v3 v3.2.0
github.com/ansrivas/fiberprometheus/v2 v2.5.0
github.com/antonmedv/expr v1.12.0
github.com/avast/retry-go/v3 v3.1.1
github.com/avast/retry-go/v4 v4.3.3
github.com/davecgh/go-spew v1.1.1
github.com/dchest/uniuri v1.2.0
github.com/felixge/fgprof v0.9.3
Expand Down Expand Up @@ -38,7 +38,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/swaggo/swag v1.8.10
github.com/tidwall/gjson v1.14.4
github.com/uptrace/bun v1.1.11
github.com/uptrace/bun v1.1.10
github.com/uptrace/bun/dialect/pgdialect v1.1.10
github.com/uptrace/bun/driver/pgdriver v1.1.10
github.com/uptrace/bun/extra/bundebug v1.1.10
Expand Down Expand Up @@ -67,7 +67,6 @@ require (
github.com/DataDog/datadog-go/v5 v5.0.2 // indirect
github.com/DataDog/gostackparse v0.5.0 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/avast/retry-go/v4 v4.3.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ github.com/ansrivas/fiberprometheus/v2 v2.5.0/go.mod h1:j7n4IeswBRIdeQ6Lr8lYqBq6
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/antonmedv/expr v1.12.0 h1:hIOn7jjY86E09PXvn9zgdt2FbWVru0ud9Rm5DbNoYNw=
github.com/antonmedv/expr v1.12.0/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
github.com/avast/retry-go/v3 v3.1.1 h1:49Scxf4v8PmiQ/nY0aY3p0hDueqSmc7++cBbtiDGu2g=
github.com/avast/retry-go/v3 v3.1.1/go.mod h1:6cXRK369RpzFL3UQGqIUp9Q7GDrams+KsYWrfNA1/nQ=
github.com/avast/retry-go/v4 v4.3.3 h1:G56Bp6mU0b5HE1SkaoVjscZjlQb0oy4mezwY/cGH19w=
github.com/avast/retry-go/v4 v4.3.3/go.mod h1:rg6XFaiuFYII0Xu3RDbZQkxCofFwruZKW8oEF1jpWiU=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
Expand Down Expand Up @@ -476,8 +474,8 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/uptrace/bun v1.1.11 h1:PVT+DHdLX13tIJaBoT0tkjh1TOWOybW/pz3TKiOB9lo=
github.com/uptrace/bun v1.1.11/go.mod h1:ysoB7l3gioKLBaZH9wKuJAoeBiOthb/hTyKdbXKcaxg=
github.com/uptrace/bun v1.1.10 h1:wx80lg32mWlOV1gNvmPTbR9znt6A2fYvvURzU5dzSPE=
github.com/uptrace/bun v1.1.10/go.mod h1:pOWJSVU+W0jp3+cbn/TtHdcG/h+QxagECw4EYdKiaSw=
github.com/uptrace/bun/dialect/pgdialect v1.1.10 h1:H25ieb5OJV5ywvKvswF++wMTnePRaGRiSNkYCRXrQxc=
github.com/uptrace/bun/dialect/pgdialect v1.1.10/go.mod h1:leDSw/IC70/GYPIU3zC8fkOZpJaJ28f51OMT1VnZiY8=
github.com/uptrace/bun/driver/pgdriver v1.1.10 h1:xmabc3eG5rz8FadFF3GrQiGs5XvXlz6CJUMZhne+1sM=
Expand Down
3 changes: 1 addition & 2 deletions internal/repo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
const AccountMaxRetries = 100

type Account struct {
db *bun.DB
sel selector.S[model.Account]

db *bun.DB
}

func NewAccount(db *bun.DB) *Account {
Expand Down
3 changes: 1 addition & 2 deletions internal/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

type Activity struct {
db *bun.DB

db *bun.DB
sel selector.S[model.Activity]
}

Expand Down
63 changes: 17 additions & 46 deletions internal/repo/drop_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"exusiai.dev/gommon/constant"
"github.com/ahmetb/go-linq/v3"
"github.com/pkg/errors"
"github.com/samber/lo"
Expand All @@ -17,64 +18,34 @@ import (
"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/pkg/pgqry"
"exusiai.dev/gommon/constant"
"exusiai.dev/backend-next/internal/repo/selector"
)

type DropInfo struct {
DB *bun.DB
db *bun.DB
sel selector.S[model.DropInfo]
}

func NewDropInfo(db *bun.DB) *DropInfo {
return &DropInfo{DB: db}
return &DropInfo{db: db, sel: selector.New[model.DropInfo](db)}
}

func (s *DropInfo) GetDropInfo(ctx context.Context, id int) (*model.DropInfo, error) {
var dropInfo model.DropInfo
err := s.DB.NewSelect().
Model(&dropInfo).
Where("id = ?", id).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

return &dropInfo, nil
return s.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("id = ?", id)
})
}

func (s *DropInfo) GetDropInfosByServerAndStageId(ctx context.Context, server string, stageId int) ([]*model.DropInfo, error) {
var dropInfo []*model.DropInfo
err := s.DB.NewSelect().
Model(&dropInfo).
Where("stage_id = ?", stageId).
Where("server = ?", server).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

return dropInfo, nil
return s.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("stage_id = ?", stageId).Where("server = ?", server)
})
}

func (s *DropInfo) GetDropInfosByServer(ctx context.Context, server string) ([]*model.DropInfo, error) {
var dropInfo []*model.DropInfo
err := s.DB.NewSelect().
Model(&dropInfo).
Where("server = ?", server).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

return dropInfo, nil
return s.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("server = ?", server)
})
}

type DropInfoQuery struct {
Expand All @@ -86,7 +57,7 @@ type DropInfoQuery struct {
func (s *DropInfo) GetForCurrentTimeRange(ctx context.Context, query *DropInfoQuery) ([]*model.DropInfo, error) {
var dropInfo []*model.DropInfo
err := pgqry.New(
s.DB.NewSelect().
s.db.NewSelect().
Model(&dropInfo).
Where("di.server = ?", query.Server).
Where("st.ark_stage_id = ?", query.ArkStageId),
Expand All @@ -108,7 +79,7 @@ func (s *DropInfo) GetForCurrentTimeRange(ctx context.Context, query *DropInfoQu

func (s *DropInfo) GetItemDropSetByStageIdAndRangeId(ctx context.Context, server string, stageId int, rangeId int) ([]int, error) {
var results []int
err := s.DB.NewSelect().
err := s.db.NewSelect().
Column("di.item_id").
Model((*model.DropInfo)(nil)).
Where("di.server = ?", server).
Expand Down Expand Up @@ -186,7 +157,7 @@ func (s *DropInfo) GetDropInfosWithFilters(ctx context.Context, server string, t
}
}
}
if err := s.DB.NewSelect().TableExpr("drop_infos as di").Column("di.stage_id", "di.item_id", "di.accumulable").
if err := s.db.NewSelect().TableExpr("drop_infos as di").Column("di.stage_id", "di.item_id", "di.accumulable").
Where(whereBuilder.String(), server, constant.DropTypeRecognitionOnly, bun.In(stageIdFilter), bun.In(itemIdFilter)).
Join("JOIN time_ranges AS tr ON tr.range_id = di.range_id").
Scan(ctx, &results); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/repo/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

type Snapshot struct {
db *bun.DB

db *bun.DB
sel selector.S[model.Snapshot]
}

Expand Down

0 comments on commit 9d0006c

Please sign in to comment.