improve documentation

This commit is contained in:
dhax 2017-09-27 22:42:43 +02:00
parent 232463e1db
commit 0826963742
16 changed files with 80 additions and 28 deletions

View file

@ -5,16 +5,19 @@ import (
"github.com/go-pg/pg"
)
// AccountStore implements database operations for account management by user.
type AccountStore struct {
db *pg.DB
}
// NewAccountStore returns an AccountStore.
func NewAccountStore(db *pg.DB) *AccountStore {
return &AccountStore{
db: db,
}
}
// Get an account by ID.
func (s *AccountStore) Get(id int) (*models.Account, error) {
a := models.Account{ID: id}
err := s.db.Model(&a).
@ -24,6 +27,7 @@ func (s *AccountStore) Get(id int) (*models.Account, error) {
return &a, err
}
// Update an account.
func (s *AccountStore) Update(a *models.Account) error {
_, err := s.db.Model(a).
Column("email", "name").
@ -31,6 +35,7 @@ func (s *AccountStore) Update(a *models.Account) error {
return err
}
// Delete an account.
func (s *AccountStore) Delete(a *models.Account) error {
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
if _, err := tx.Model(&models.Token{}).
@ -48,6 +53,7 @@ func (s *AccountStore) Delete(a *models.Account) error {
return err
}
// UpdateToken updates a jwt refresh token.
func (s *AccountStore) UpdateToken(t *models.Token) error {
_, err := s.db.Model(t).
Column("identifier").
@ -55,11 +61,13 @@ func (s *AccountStore) UpdateToken(t *models.Token) error {
return err
}
// DeleteToken deletes a jwt refresh token.
func (s *AccountStore) DeleteToken(t *models.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

@ -8,19 +8,23 @@ import (
)
var (
// ErrUniqueEmailConstraint provides error message for already registered email address.
ErrUniqueEmailConstraint = errors.New("email already registered")
)
// AdmAccountStore implements database operations for account management by admin.
type AdmAccountStore struct {
db *pg.DB
}
// NewAdmAccountStore returns an AccountStore.
func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
return &AdmAccountStore{
db: db,
}
}
// List applies a filter and returns paginated array of matching results and total count.
func (s *AdmAccountStore) List(f models.AccountFilter) (*[]models.Account, int, error) {
var a []models.Account
count, err := s.db.Model(&a).
@ -32,6 +36,7 @@ func (s *AdmAccountStore) List(f models.AccountFilter) (*[]models.Account, int,
return &a, count, nil
}
// Create creates a new account.
func (s *AdmAccountStore) Create(a *models.Account) error {
count, _ := s.db.Model(a).
Where("email = ?email").
@ -55,17 +60,20 @@ func (s *AdmAccountStore) Create(a *models.Account) error {
return err
}
// Get account by ID.
func (s *AdmAccountStore) Get(id int) (*models.Account, error) {
a := models.Account{ID: id}
err := s.db.Select(&a)
return &a, err
}
// Update account.
func (s *AdmAccountStore) Update(a *models.Account) error {
err := s.db.Update(a)
return err
}
// Delete account.
func (s *AdmAccountStore) Delete(a *models.Account) error {
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
if _, err := tx.Model(&models.Token{}).

View file

@ -7,16 +7,19 @@ import (
"github.com/go-pg/pg"
)
// AuthStore implements database operations for account authentication.
type AuthStore struct {
db *pg.DB
}
// NewAuthStore return an AuthStore.
func NewAuthStore(db *pg.DB) *AuthStore {
return &AuthStore{
db: db,
}
}
// GetByID returns an account by ID.
func (s *AuthStore) GetByID(id int) (*models.Account, error) {
a := models.Account{ID: id}
err := s.db.Model(&a).
@ -26,6 +29,7 @@ func (s *AuthStore) GetByID(id int) (*models.Account, error) {
return &a, err
}
// GetByEmail returns an account by email.
func (s *AuthStore) GetByEmail(e string) (*models.Account, error) {
a := models.Account{Email: e}
err := s.db.Model(&a).
@ -35,6 +39,7 @@ func (s *AuthStore) GetByEmail(e string) (*models.Account, error) {
return &a, err
}
// GetByRefreshToken returns an account and refresh token by token identifier.
func (s *AuthStore) GetByRefreshToken(t string) (*models.Account, *models.Token, error) {
token := models.Token{Token: t}
err := s.db.Model(&token).
@ -53,6 +58,7 @@ func (s *AuthStore) GetByRefreshToken(t string) (*models.Account, *models.Token,
return &a, &token, err
}
// UpdateAccount upates account data related to authentication.
func (s *AuthStore) UpdateAccount(a *models.Account) error {
_, err := s.db.Model(a).
Column("last_login").
@ -60,6 +66,7 @@ func (s *AuthStore) UpdateAccount(a *models.Account) error {
return err
}
// SaveRefreshToken creates or updates a refresh token.
func (s *AuthStore) SaveRefreshToken(t *models.Token) error {
var err error
if t.ID == 0 {
@ -70,11 +77,13 @@ func (s *AuthStore) SaveRefreshToken(t *models.Token) error {
return err
}
// DeleteRefreshToken deletes a refresh token.
func (s *AuthStore) DeleteRefreshToken(t *models.Token) error {
err := s.db.Delete(t)
return err
}
// PurgeExpiredToken deletes expired refresh token.
func (s *AuthStore) PurgeExpiredToken() error {
_, err := s.db.Model(&models.Token{}).
Where("expiry < ?", time.Now()).

View file

@ -6,7 +6,7 @@ import (
"github.com/go-pg/migrations"
)
const AccountTable = `
const accountTable = `
CREATE TABLE accounts (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
@ -19,7 +19,7 @@ roles text[] NOT NULL DEFAULT '{"user"}',
PRIMARY KEY (id)
)`
const TokenTable = `
const tokenTable = `
CREATE TABLE tokens (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
@ -34,8 +34,8 @@ PRIMARY KEY (id)
func init() {
up := []string{
AccountTable,
TokenTable,
accountTable,
tokenTable,
}
down := []string{

View file

@ -6,7 +6,7 @@ import (
"github.com/go-pg/migrations"
)
const ProfileTable = `
const profileTable = `
CREATE TABLE profiles (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
@ -23,7 +23,7 @@ INSERT INTO profiles(account_id) VALUES(2);
func init() {
up := []string{
ProfileTable,
profileTable,
bootstrapAccountProfiles,
}

View file

@ -9,6 +9,7 @@ import (
"github.com/go-pg/pg"
)
// Migrate runs go-pg migrations
func Migrate(args []string) {
db, err := database.DBConn()
if err != nil {
@ -33,6 +34,7 @@ func Migrate(args []string) {
}
// Reset runs reverts all migrations to version 0 and then applies all migrations to latest
func Reset() {
db, err := database.DBConn()
if err != nil {

View file

@ -9,7 +9,7 @@ import (
"github.com/go-pg/pg"
)
// DBConn returns a postgres connection pool
// DBConn returns a postgres connection pool.
func DBConn() (*pg.DB, error) {
opts, err := pg.ParseURL(viper.GetString("database_url"))