Skip to content

Commit

Permalink
fix: merge artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Nov 23, 2023
1 parent a6feddc commit 23a5eeb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/repo/drop_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *DropInfo) GetItemDropSetByStageIdAndRangeId(ctx context.Context, server
}

results = lo.Uniq(results)
sort.Ints(results)
results = sort.IntSlice(results)
return results, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/repo/drop_pattern_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *DropPatternElement) CreateDropPatternElements(ctx context.Context, tx b
func (r *DropPatternElement) GetDropPatternElementsByPatternId(ctx context.Context, patternId int) ([]*model.DropPatternElement, error) {
return r.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("drop_pattern_id = ?", patternId)
})
}, selector.OptionUseZeroLenSliceOnNull)
}

func (r *DropPatternElement) GetDropPatternElementsByPatternIds(ctx context.Context, patternIds []int) ([]*model.DropPatternElement, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/repo/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func NewNotice(db *bun.DB) *Notice {
func (r *Notice) GetNotices(ctx context.Context) ([]*model.Notice, error) {
return r.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("notice_id ASC")
})
}, selector.OptionUseZeroLenSliceOnNull)
}
24 changes: 23 additions & 1 deletion internal/repo/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ func New[T any](db *bun.DB) S[T] {
}
}

type Option int

const OptionUseZeroLenSliceOnNull = iota

type Options []Option

func (o Options) Contains(option Option) bool {
for _, v := range o {
if v == option {
return true
}
}

return false
}

func (r S[T]) SelectOne(ctx context.Context, fn func(q *bun.SelectQuery) *bun.SelectQuery) (*T, error) {
var model T
err := fn(r.DB.NewSelect().Model(&model)).Scan(ctx)
Expand All @@ -32,10 +48,16 @@ func (r S[T]) SelectOne(ctx context.Context, fn func(q *bun.SelectQuery) *bun.Se
return &model, nil
}

func (r S[T]) SelectMany(ctx context.Context, fn func(q *bun.SelectQuery) *bun.SelectQuery) ([]*T, error) {
func (r S[T]) SelectMany(ctx context.Context, fn func(q *bun.SelectQuery) *bun.SelectQuery, options ...Option) ([]*T, error) {
o := Options(options)
var model []*T
err := fn(r.DB.NewSelect().Model(&model)).Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
if o.Contains(OptionUseZeroLenSliceOnNull) {
model = make([]*T, 0)
return model, nil
}

return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
Expand Down

0 comments on commit 23a5eeb

Please sign in to comment.