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 d8aa0e3 commit fbab51d
Showing 1 changed file with 28 additions and 77 deletions.
105 changes: 28 additions & 77 deletions internal/repo/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,39 @@ import (
"exusiai.dev/backend-next/internal/model"
modelv2 "exusiai.dev/backend-next/internal/model/v2"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/repo/selector"
)

type Stage struct {
db *bun.DB
db *bun.DB
v2sel selector.S[modelv2.Stage]
v3sel selector.S[model.Stage]
}

func NewStage(db *bun.DB) *Stage {
return &Stage{db: db}
return &Stage{
db: db,
v2sel: selector.New[modelv2.Stage](db),
v3sel: selector.New[model.Stage](db),
}
}

func (c *Stage) GetStages(ctx context.Context) ([]*model.Stage, error) {
var stages []*model.Stage
err := c.db.NewSelect().
Model(&stages).
Order("stage_id ASC").
Scan(ctx)

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

return stages, nil
return c.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("stage_id ASC")
})
}

func (c *Stage) GetStageById(ctx context.Context, stageId int) (*model.Stage, error) {
var stage model.Stage
err := c.db.NewSelect().
Model(&stage).
Where("stage_id = ?", stageId).
Scan(ctx)

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

return &stage, nil
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("stage_id = ?", stageId)
})
}

func (c *Stage) GetStageByArkId(ctx context.Context, arkStageId string) (*model.Stage, error) {
var stage model.Stage
err := c.db.NewSelect().
Model(&stage).
Where("ark_stage_id = ?", arkStageId).
Scan(ctx)

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

return &stage, nil
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("ark_stage_id = ?", arkStageId)
})
}

func (c *Stage) shimStageQuery(ctx context.Context, server string, stages *[]*modelv2.Stage, t time.Time) error {
Expand Down Expand Up @@ -160,50 +137,24 @@ func (c *Stage) GetShimStageByArkId(ctx context.Context, arkStageId string, serv
}

func (c *Stage) GetStageExtraProcessTypeByArkId(ctx context.Context, arkStageId string) (null.String, error) {
var stage model.Stage
err := c.db.NewSelect().
Model(&stage).
Column("st.extra_process_type").
Where("st.ark_stage_id = ?", arkStageId).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return null.NewString("", false), pgerr.ErrNotFound
} else if err != nil {
stage, err := c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("st.extra_process_type").Where("st.ark_stage_id = ?", arkStageId)
})
if err != nil {
return null.NewString("", false), err
}

return stage.ExtraProcessType, nil
}

func (c *Stage) SearchStageByCode(ctx context.Context, code string) (*model.Stage, error) {
var stage model.Stage
err := c.db.NewSelect().
Model(&stage).
Where("\"code\"::TEXT ILIKE ?", "%"+code+"%").
Scan(ctx)

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

return &stage, nil
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("\"code\"::TEXT ILIKE ?", "%"+code+"%")
})
}

func (c *Stage) GetGachaBoxStages(ctx context.Context) ([]*model.Stage, error) {
var stages []*model.Stage
err := c.db.NewSelect().
Model(&stages).
Where("extra_process_type = ?", constant.ExtraProcessTypeGachaBox).
Scan(ctx)

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

return stages, nil
return c.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("st.extra_process_type = ?", constant.ExtraProcessTypeGachaBox)
})
}

0 comments on commit fbab51d

Please sign in to comment.