mtfosbot/model/account.go

62 lines
1.4 KiB
Go
Raw Normal View History

2018-08-28 03:55:38 +00:00
package model
import (
2018-09-24 11:57:12 +00:00
"database/sql"
"errors"
2018-08-28 03:55:38 +00:00
"time"
)
// Account - table
type Account struct {
2018-09-02 01:15:27 +00:00
ID string `db:"id" cc:"id"`
Account string `db:"account" cc:"account"`
Password string `db:"password" cc:"-"`
2018-09-02 01:15:27 +00:00
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
2018-08-28 03:55:38 +00:00
}
// GetAllAccount -
2018-09-10 10:13:27 +00:00
func GetAllAccount() (accs []*Account, err error) {
2018-08-28 03:55:38 +00:00
err = x.Select(&accs, "select * from public.account order by ctime asc")
if err != nil {
return nil, err
}
return
}
// GetAccount -
func GetAccount(account string) (acc *Account, err error) {
2018-09-02 01:15:27 +00:00
acc = &Account{}
err = x.Get(acc, `select * from "public"."account" where "account" = $1`, account)
2018-09-24 11:57:12 +00:00
if err == sql.ErrNoRows {
return nil, nil
}
2018-08-28 03:55:38 +00:00
if err != nil {
return nil, err
}
return
}
// CreateAccount -
func CreateAccount(account, password string) (acc *Account, err error) {
acc = &Account{}
2019-04-01 07:33:06 +00:00
err = x.Get(acc, `insert into "public"."account" ("account", "password", "ctime", "mtime") values ($1, $2, now(), now()) returning *`, account, password)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return
}
// ChangePassword -
func (p *Account) ChangePassword(password string) (err error) {
if len(password) == 0 {
return errors.New("password is empty")
}
_, err = x.Exec(`update "public"."account" set "password" = $1, "mtime" = now() where "id" = $2`, password, p.ID)
p.Password = password
return
}