add change password and create accoutn model

This commit is contained in:
Jay 2018-12-02 11:46:16 +08:00
parent 1529e2ea50
commit 51c3ed0e66

View File

@ -2,6 +2,7 @@ package model
import ( import (
"database/sql" "database/sql"
"errors"
"time" "time"
) )
@ -35,3 +36,26 @@ func GetAccount(account string) (acc *Account, err error) {
} }
return return
} }
// CreateAccount -
func CreateAccount(account, password string) (acc *Account, err error) {
acc = &Account{}
err = x.Get(acc, `insert into "public"."account" ("account", "password", "ctime", "mtime") values ($1, $2, now(), now())`, 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
}