separate profile model from account model

This commit is contained in:
dhax 2017-10-13 00:11:57 +02:00
parent f729160209
commit c3271d7dcf
15 changed files with 152 additions and 83 deletions

View file

@ -23,7 +23,7 @@ func (s *AccountStore) Get(id int) (*auth.Account, error) {
a := auth.Account{ID: id}
err := s.db.Model(&a).
Where("account.id = ?id").
Column("account.*", "Profile", "Token").
Column("account.*", "Token").
First()
return &a, err
}
@ -67,9 +67,3 @@ func (s *AccountStore) DeleteToken(t *auth.Token) error {
err := s.db.Delete(t)
return err
}
// UpdateProfile updates corresponding account profile.
func (s *AccountStore) UpdateProfile(p *models.Profile) error {
err := s.db.Update(p)
return err
}

View file

@ -9,7 +9,6 @@ import (
const profileTable = `
CREATE TABLE profiles (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
account_id int NOT NULL REFERENCES accounts(id),
theme text NOT NULL DEFAULT 'default',

34
database/profileStore.go Normal file
View file

@ -0,0 +1,34 @@
package database
import (
"github.com/dhax/go-base/models"
"github.com/go-pg/pg"
)
// ProfileStore implements database operations for profile management.
type ProfileStore struct {
db *pg.DB
}
// NewProfileStore returns a ProfileStore implementation.
func NewProfileStore(db *pg.DB) *ProfileStore {
return &ProfileStore{
db: db,
}
}
// 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).
Where("account_id = ?", accountID).
SelectOrInsert()
return &p, err
}
// Update updates profile.
func (s *ProfileStore) Update(p *models.Profile) error {
err := s.db.Update(p)
return err
}