upgrade from go-pg to bun

This commit is contained in:
dhax 2024-08-31 18:57:54 +02:00
parent f59f129354
commit 1886be62bc
23 changed files with 415 additions and 385 deletions

View file

@ -1,17 +1,20 @@
package database
import (
"context"
"database/sql"
"github.com/dhax/go-base/models"
"github.com/go-pg/pg"
"github.com/uptrace/bun"
)
// ProfileStore implements database operations for profile management.
type ProfileStore struct {
db *pg.DB
db *bun.DB
}
// NewProfileStore returns a ProfileStore implementation.
func NewProfileStore(db *pg.DB) *ProfileStore {
func NewProfileStore(db *bun.DB) *ProfileStore {
return &ProfileStore{
db: db,
}
@ -19,16 +22,26 @@ func NewProfileStore(db *pg.DB) *ProfileStore {
// Get gets an profile by account ID.
func (s *ProfileStore) Get(accountID int) (*models.Profile, error) {
p := models.Profile{AccountID: accountID}
_, err := s.db.Model(&p).
p := &models.Profile{AccountID: accountID}
err := s.db.NewSelect().
Model(p).
Where("account_id = ?", accountID).
SelectOrInsert()
Scan(context.Background())
return &p, err
if err == sql.ErrNoRows {
_, err = s.db.NewInsert().
Model(p).
Exec(context.Background())
}
return p, err
}
// Update updates profile.
func (s *ProfileStore) Update(p *models.Profile) error {
err := s.db.Update(p)
_, err := s.db.NewUpdate().
Model(p).
WherePK().
Exec(context.Background())
return err
}