go-gallery/models/account.go

82 lines
1.9 KiB
Go

package models
import (
"errors"
"time"
"git.trj.tw/golang/utils/dbtool"
)
// 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
rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where 1`)
if err != nil {
return nil, err
}
err = dbtool.ScanToStructAll(rows, &accs)
return accs, nil
}
// GetAccount -
func GetAccount(account string) (acc *Account, err error) {
acc = &Account{}
rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where "account" = $1`, account)
if err != nil {
return nil, err
}
ok, err := dbtool.ScanToStruct(rows, acc)
if err != nil || !ok {
return nil, err
}
return
}
// Get -
func (a *Account) Get() (bool, error) {
rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where "account" = $1`, a.Account)
if err != nil {
return false, err
}
ok, err := dbtool.ScanToStruct(rows, a)
if err != nil {
return false, err
}
return ok, nil
}
// Create -
func (a *Account) Create() error {
row, err := x.Query(`insert into "public"."account"
("account", "password", "nick", "email") values ($1, $2, $3, $4) returning *`, a.Account, a.Password, a.Nick, a.Email)
if err != nil {
return err
}
ok, err := dbtool.ScanToStruct(row, a)
if err != nil {
return err
}
if !ok {
return errors.New("data returning fail")
}
return nil
}