From 0db79036491f859d3897f13522931a24da87fed9 Mon Sep 17 00:00:00 2001 From: GalvinGao Date: Fri, 10 Feb 2023 15:34:30 -0500 Subject: [PATCH] refactor: use db selector --- internal/repo/drop_matrix_element.go | 6 ++- internal/repo/drop_pattern_element.go | 69 +++++++-------------------- 2 files changed, 20 insertions(+), 55 deletions(-) diff --git a/internal/repo/drop_matrix_element.go b/internal/repo/drop_matrix_element.go index e04adb5d..7983154c 100644 --- a/internal/repo/drop_matrix_element.go +++ b/internal/repo/drop_matrix_element.go @@ -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 { diff --git a/internal/repo/drop_pattern_element.go b/internal/repo/drop_pattern_element.go index f73683ff..69861e22 100644 --- a/internal/repo/drop_pattern_element.go +++ b/internal/repo/drop_pattern_element.go @@ -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) { @@ -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) + }) }