diff --git a/go.mod b/go.mod index db9d65c5..f0fba648 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 6c0377bc..063d36dd 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/repo/account.go b/internal/repo/account.go index 200f55e3..35fe293f 100644 --- a/internal/repo/account.go +++ b/internal/repo/account.go @@ -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 { diff --git a/internal/repo/activity.go b/internal/repo/activity.go index 37d256e2..bbd92a76 100644 --- a/internal/repo/activity.go +++ b/internal/repo/activity.go @@ -10,8 +10,7 @@ import ( ) type Activity struct { - db *bun.DB - + db *bun.DB sel selector.S[model.Activity] } diff --git a/internal/repo/drop_info.go b/internal/repo/drop_info.go index f0c8f094..ce95f239 100644 --- a/internal/repo/drop_info.go +++ b/internal/repo/drop_info.go @@ -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" @@ -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 { @@ -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), @@ -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). @@ -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 { diff --git a/internal/repo/snapshot.go b/internal/repo/snapshot.go index dc426bad..ef027501 100644 --- a/internal/repo/snapshot.go +++ b/internal/repo/snapshot.go @@ -10,8 +10,7 @@ import ( ) type Snapshot struct { - db *bun.DB - + db *bun.DB sel selector.S[model.Snapshot] }