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 a5353cb commit 5741bab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
23 changes: 8 additions & 15 deletions internal/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@ 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 Activity struct {
DB *bun.DB
db *bun.DB

sel selector.S[model.Activity]
}

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

func (c *Activity) GetActivities(ctx context.Context) ([]*model.Activity, error) {
var activities []*model.Activity
err := c.DB.NewSelect().
Model(&activities).
Order("activity_id ASC").
Scan(ctx)

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

return activities, nil
return c.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("activity_id ASC")
})
}
8 changes: 4 additions & 4 deletions internal/repo/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

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

DB *bun.DB
sel selector.S[model.Snapshot]
}

func NewSnapshot(db *bun.DB) *Snapshot {
return &Snapshot{
DB: db,
db: db,
sel: selector.New[model.Snapshot](db),
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ func (s *Snapshot) GetSnapshotsByVersions(ctx context.Context, key string, versi
}

func (s *Snapshot) SaveSnapshot(ctx context.Context, snapshot *model.Snapshot) (*model.Snapshot, error) {
_, err := s.DB.NewInsert().
_, err := s.db.NewInsert().
Model(snapshot).
Exec(ctx)
if err != nil {
Expand Down

0 comments on commit 5741bab

Please sign in to comment.