github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/database/profileStore.go (about)

     1  package database
     2  
     3  import (
     4  	"github.com/dhax/go-base/models"
     5  	"github.com/go-pg/pg"
     6  )
     7  
     8  // ProfileStore implements database operations for profile management.
     9  type ProfileStore struct {
    10  	db *pg.DB
    11  }
    12  
    13  // NewProfileStore returns a ProfileStore implementation.
    14  func NewProfileStore(db *pg.DB) *ProfileStore {
    15  	return &ProfileStore{
    16  		db: db,
    17  	}
    18  }
    19  
    20  // Get gets an profile by account ID.
    21  func (s *ProfileStore) Get(accountID int) (*models.Profile, error) {
    22  	p := models.Profile{AccountID: accountID}
    23  	_, err := s.db.Model(&p).
    24  		Where("account_id = ?", accountID).
    25  		SelectOrInsert()
    26  
    27  	return &p, err
    28  }
    29  
    30  // Update updates profile.
    31  func (s *ProfileStore) Update(p *models.Profile) error {
    32  	err := s.db.Update(p)
    33  	return err
    34  }