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 8c066e9 commit 5420359
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
28 changes: 14 additions & 14 deletions internal/repo/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,44 @@ func NewItem(db *bun.DB) *Item {
}
}

func (c *Item) GetItems(ctx context.Context) ([]*model.Item, error) {
return c.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetItems(ctx context.Context) ([]*model.Item, error) {
return r.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("item_id ASC")
})
}

func (c *Item) GetItemById(ctx context.Context, itemId int) (*model.Item, error) {
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetItemById(ctx context.Context, itemId int) (*model.Item, error) {
return r.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("item_id = ?", itemId)
})
}

func (c *Item) GetItemByArkId(ctx context.Context, arkItemId string) (*model.Item, error) {
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetItemByArkId(ctx context.Context, arkItemId string) (*model.Item, error) {
return r.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("ark_item_id = ?", arkItemId)
})
}

func (c *Item) GetShimItems(ctx context.Context) ([]*modelv2.Item, error) {
return c.v2sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetShimItems(ctx context.Context) ([]*modelv2.Item, error) {
return r.v2sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("item_id ASC")
})
}

func (c *Item) GetShimItemByArkId(ctx context.Context, itemId string) (*modelv2.Item, error) {
return c.v2sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetShimItemByArkId(ctx context.Context, itemId string) (*modelv2.Item, error) {
return r.v2sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("ark_item_id = ?", itemId)
})
}

func (c *Item) SearchItemByName(ctx context.Context, name string) (*model.Item, error) {
return c.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) SearchItemByName(ctx context.Context, name string) (*model.Item, error) {
return r.v3sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("\"name\"::TEXT ILIKE ?", "%"+name+"%")
})
}

func (c *Item) GetRecruitTagItems(ctx context.Context) ([]*model.Item, error) {
return c.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
func (r *Item) GetRecruitTagItems(ctx context.Context) ([]*model.Item, error) {
return r.v3sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("type = ?", constant.RecruitItemType).Order("item_id ASC")
})
}
24 changes: 8 additions & 16 deletions internal/repo/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@ package repo

import (
"context"
"database/sql"

"github.com/pkg/errors"
"github.com/uptrace/bun"

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

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

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

func (c *Notice) GetNotices(ctx context.Context) ([]*model.Notice, error) {
var notice []*model.Notice
err := c.DB.NewSelect().
Model(&notice).
Order("notice_id ASC").
Scan(ctx)

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

return notice, nil
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")
})
}

0 comments on commit 5420359

Please sign in to comment.