86 lines
1.9 KiB
Go
86 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 `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"`
|
|
}
|
|
|
|
// 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
|
|
}
|
|
defer rows.Close()
|
|
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
|
|
}
|
|
defer rows.Close()
|
|
|
|
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
|
|
}
|
|
defer rows.Close()
|
|
|
|
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
|
|
}
|
|
defer row.Close()
|
|
|
|
ok, err := dbtool.ScanToStruct(row, a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return errors.New("data returning fail")
|
|
}
|
|
|
|
return nil
|
|
}
|