zrok/controller/store/account.go

54 lines
1.5 KiB
Go
Raw Normal View History

2022-07-25 22:03:51 +02:00
package store
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type Account struct {
Model
2022-09-09 16:20:05 +02:00
Email string
2022-07-25 22:03:51 +02:00
Password string
Token string
}
func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) {
2022-09-09 16:20:05 +02:00
stmt, err := tx.Prepare("insert into accounts (email, password, token) values (?, ?, ?)")
2022-07-25 22:03:51 +02:00
if err != nil {
return 0, errors.Wrap(err, "error preparing accounts insert statement")
}
2022-09-09 16:20:05 +02:00
res, err := stmt.Exec(a.Email, a.Password, a.Token)
2022-07-25 22:03:51 +02:00
if err != nil {
return 0, errors.Wrap(err, "error executing accounts insert statement")
}
id, err := res.LastInsertId()
if err != nil {
return 0, errors.Wrap(err, "error retrieving last accounts insert id")
}
return int(id), nil
}
2022-07-29 19:27:00 +02:00
func (self *Store) GetAccount(id int, tx *sqlx.Tx) (*Account, error) {
a := &Account{}
if err := tx.QueryRowx("select * from accounts where id = ?", id).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by id")
}
return a, nil
}
2022-09-09 16:20:05 +02:00
func (self *Store) FindAccountWithEmail(email string, tx *sqlx.Tx) (*Account, error) {
2022-08-02 19:23:31 +02:00
a := &Account{}
2022-09-09 16:20:05 +02:00
if err := tx.QueryRowx("select * from accounts where email = ?", email).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by email")
2022-08-02 19:23:31 +02:00
}
return a, nil
}
2022-07-25 22:03:51 +02:00
func (self *Store) FindAccountWithToken(token string, tx *sqlx.Tx) (*Account, error) {
a := &Account{}
if err := tx.QueryRowx("select * from accounts where token = ?", token).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by token")
}
return a, nil
}