go-gallery/models/account.go

86 lines
1.9 KiB
Go
Raw Normal View History

2018-04-12 09:41:21 +00:00
package models
import (
2018-04-20 06:33:52 +00:00
"errors"
2018-04-12 09:41:21 +00:00
"time"
2018-04-20 06:33:52 +00:00
"git.trj.tw/golang/utils/dbtool"
2018-04-12 09:41:21 +00:00
)
// Account - Account table struct
type Account struct {
2018-04-24 09:49:58 +00:00
ID string `sql:"id" cc:"id"`
Account string `sql:"account" cc:"account"`
Password string `sql:"password" cc:"-"`
Nick string `sql:"nick" cc:"nick"`
Email string `sql:"email" cc:"email"`
Ctime time.Time `sql:"ctime" cc:"ctime"`
Mtime time.Time `sql:"mtime" cc:"mtime"`
2018-04-12 09:41:21 +00:00
}
// GetAllAccount - all account
func GetAllAccount() ([]Account, error) {
var accs []Account
2018-04-20 06:33:52 +00:00
rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where 1`)
if err != nil {
return nil, err
}
2018-04-24 09:49:58 +00:00
defer rows.Close()
2018-04-20 06:33:52 +00:00
err = dbtool.ScanToStructAll(rows, &accs)
return accs, nil
2018-04-12 09:41:21 +00:00
}
2018-04-17 10:04:36 +00:00
// GetAccount -
func GetAccount(account string) (acc *Account, err error) {
2018-04-20 06:33:52 +00:00
acc = &Account{}
2018-04-18 10:21:29 +00:00
2018-04-20 06:33:52 +00:00
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
}
2018-04-24 09:49:58 +00:00
defer rows.Close()
2018-04-18 10:21:29 +00:00
2018-04-20 06:33:52 +00:00
ok, err := dbtool.ScanToStruct(rows, acc)
2018-04-18 10:21:29 +00:00
if err != nil || !ok {
2018-04-17 10:04:36 +00:00
return nil, err
}
return
2018-04-12 09:41:21 +00:00
}
2018-04-18 10:21:29 +00:00
// Get -
func (a *Account) Get() (bool, error) {
2018-04-20 06:33:52 +00:00
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
}
2018-04-24 09:49:58 +00:00
defer rows.Close()
2018-04-20 06:33:52 +00:00
ok, err := dbtool.ScanToStruct(rows, a)
2018-04-18 10:21:29 +00:00
if err != nil {
return false, err
}
return ok, nil
}
// Create -
func (a *Account) Create() error {
2018-04-20 06:33:52 +00:00
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
}
2018-04-24 09:49:58 +00:00
defer row.Close()
2018-04-20 06:33:52 +00:00
ok, err := dbtool.ScanToStruct(row, a)
if err != nil {
return err
}
if !ok {
return errors.New("data returning fail")
}
return nil
2018-04-18 10:21:29 +00:00
}