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 0662893 commit a5353cb
Showing 1 changed file with 17 additions and 40 deletions.
57 changes: 17 additions & 40 deletions internal/repo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ package repo

import (
"context"
"database/sql"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/uptrace/bun"

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

const AccountMaxRetries = 100

type Account struct {
sel selector.S[model.Account]

db *bun.DB
}

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

func (c *Account) CreateAccountWithRandomPenguinId(ctx context.Context) (*model.Account, error) {
Expand Down Expand Up @@ -59,51 +63,24 @@ func (c *Account) CreateAccountWithRandomPenguinId(ctx context.Context) (*model.
}

func (c *Account) GetAccountById(ctx context.Context, accountId string) (*model.Account, error) {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Column("account_id").
Where("account_id = ?", accountId).
Scan(ctx)

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

return &account, nil
return c.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("account_id = ?", accountId)
})
}

func (c *Account) GetAccountByPenguinId(ctx context.Context, penguinId string) (*model.Account, error) {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Where("penguin_id = ?", penguinId).
Scan(ctx)

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

return &account, nil
return c.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("penguin_id = ?", penguinId)
})
}

func (c *Account) IsAccountExistWithId(ctx context.Context, accountId int) bool {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Column("account_id").
Where("account_id = ?", accountId).
Scan(ctx, &account)
account, err := c.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("account_id").Where("account_id = ?", accountId)
})
if err != nil {
return false
}

return account.AccountID > 0
return account != nil
}

0 comments on commit a5353cb

Please sign in to comment.