package models

import (
	"time"
)

// Account - Account table struct
type Account struct {
	ID       string    `xorm:"id <-" cc:"id"`
	Account  string    `xorm:"account" cc:"account"`
	Password string    `xorm:"password" cc:"-"`
	Nick     string    `xorm:"nick" cc:"nick"`
	Email    string    `xorm:"email" cc:"email"`
	Ctime    time.Time `xorm:"ctime created" cc:"ctime"`
	Mtime    time.Time `xorm:"mtime updated" cc:"mtime"`
}

// GetAllAccount - all account
func GetAllAccount() ([]Account, error) {
	var accs []Account
	err := x.Table("account").Find(&accs)
	return accs, err
}

// GetAccount -
func GetAccount(account string) (acc *Account, err error) {
	acc = &Account{
		Account: account,
	}

	ok, err := acc.Get()

	if err != nil || !ok {
		return nil, err
	}

	return
}

// Get -
func (a *Account) Get() (bool, error) {
	ok, err := x.Table("account").Get(a)
	if err != nil {
		return false, err
	}

	return ok, nil
}

// Create -
func (a *Account) Create() error {
	_, err := x.Table("account").Insert(a)
	return err
}
