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 9d0006c commit 0db7903
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 55 deletions.
6 changes: 4 additions & 2 deletions internal/repo/drop_matrix_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"github.com/uptrace/bun"

"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/repo/selector"
)

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

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

func (s *DropMatrixElement) BatchSaveElements(ctx context.Context, elements []*model.DropMatrixElement, server string) error {
Expand Down
69 changes: 16 additions & 53 deletions internal/repo/drop_pattern_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,34 @@ import (

"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/model/types"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/repo/selector"
)

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

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

func (r *DropPatternElement) GetDropPatternElements(ctx context.Context) ([]*model.DropPatternElement, error) {
var elements []*model.DropPatternElement
err := r.DB.NewSelect().
Model(&elements).
Scan(ctx)

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

return elements, nil
return r.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q
})
}

func (r *DropPatternElement) GetDropPatternElementById(ctx context.Context, id int) (*model.DropPatternElement, error) {
var DropPatternElement model.DropPatternElement
err := r.DB.NewSelect().
Model(&DropPatternElement).
Where("id = ?", id).
Scan(ctx)

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

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

func (r *DropPatternElement) GetDropPatternElementByHash(ctx context.Context, hash string) (*model.DropPatternElement, error) {
var DropPatternElement model.DropPatternElement
err := r.DB.NewSelect().
Model(&DropPatternElement).
Where("hash = ?", hash).
Scan(ctx)

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

return &DropPatternElement, nil
return r.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("hash = ?", hash)
})
}

func (r *DropPatternElement) CreateDropPatternElements(ctx context.Context, tx bun.Tx, patternId int, drops []*types.Drop) ([]*model.DropPatternElement, error) {
Expand Down Expand Up @@ -94,16 +66,7 @@ func (r *DropPatternElement) CreateDropPatternElements(ctx context.Context, tx b
}

func (r *DropPatternElement) GetDropPatternElementsByPatternId(ctx context.Context, patternId int) ([]*model.DropPatternElement, error) {
var elements []*model.DropPatternElement
err := r.DB.NewSelect().
Model(&elements).
Where("drop_pattern_id = ?", patternId).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
elements = make([]*model.DropPatternElement, 0)
} else if err != nil {
return nil, err
}
return elements, nil
return r.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("drop_pattern_id = ?", patternId)
})
}

0 comments on commit 0db7903

Please sign in to comment.