2022-07-25 22:03:51 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
Model
|
2023-01-13 19:16:10 +01:00
|
|
|
Email string
|
2023-01-23 18:50:24 +01:00
|
|
|
Salt string
|
2023-01-13 19:16:10 +01:00
|
|
|
Password string
|
|
|
|
Token string
|
|
|
|
Limitless bool
|
2023-03-09 21:08:59 +01:00
|
|
|
Deleted bool
|
2022-07-25 22:03:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) {
|
2024-01-08 16:50:19 +01:00
|
|
|
stmt, err := tx.Prepare("insert into accounts (email, salt, password, token, limitless) values (lower($1), $2, $3, $4, $5) returning id")
|
2022-07-25 22:03:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing accounts insert statement")
|
|
|
|
}
|
2022-10-21 15:31:12 +02:00
|
|
|
var id int
|
2023-01-23 18:50:24 +01:00
|
|
|
if err := stmt.QueryRow(a.Email, a.Salt, a.Password, a.Token, a.Limitless).Scan(&id); err != nil {
|
2022-07-25 22:03:51 +02:00
|
|
|
return 0, errors.Wrap(err, "error executing accounts insert statement")
|
|
|
|
}
|
2022-10-21 15:31:12 +02:00
|
|
|
return id, nil
|
2022-07-25 22:03:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) GetAccount(id int, tx *sqlx.Tx) (*Account, error) {
|
2022-07-29 19:27:00 +02:00
|
|
|
a := &Account{}
|
2022-10-21 15:31:12 +02:00
|
|
|
if err := tx.QueryRowx("select * from accounts where id = $1", id).StructScan(a); err != nil {
|
2022-07-29 19:27:00 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account by id")
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindAccountWithEmail(email string, tx *sqlx.Tx) (*Account, error) {
|
2022-08-02 19:23:31 +02:00
|
|
|
a := &Account{}
|
2024-01-08 16:50:19 +01:00
|
|
|
if err := tx.QueryRowx("select * from accounts where email = lower($1) and not deleted", email).StructScan(a); err != nil {
|
2022-09-09 16:20:05 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account by email")
|
2022-08-02 19:23:31 +02:00
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindAccountWithEmailAndDeleted(email string, tx *sqlx.Tx) (*Account, error) {
|
2023-03-10 16:57:59 +01:00
|
|
|
a := &Account{}
|
2024-01-08 16:50:19 +01:00
|
|
|
if err := tx.QueryRowx("select * from accounts where email = lower($1)", email).StructScan(a); err != nil {
|
2023-03-10 16:57:59 +01:00
|
|
|
return nil, errors.Wrap(err, "error selecting acount by email")
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindAccountWithToken(token string, tx *sqlx.Tx) (*Account, error) {
|
2022-07-25 22:03:51 +02:00
|
|
|
a := &Account{}
|
2023-03-10 16:57:59 +01:00
|
|
|
if err := tx.QueryRowx("select * from accounts where token = $1 and not deleted", token).StructScan(a); err != nil {
|
2022-07-25 22:03:51 +02:00
|
|
|
return nil, errors.Wrap(err, "error selecting account by token")
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
2023-01-18 20:05:10 +01:00
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) UpdateAccount(a *Account, tx *sqlx.Tx) (int, error) {
|
2024-01-08 16:50:19 +01:00
|
|
|
stmt, err := tx.Prepare("update accounts set email=lower($1), salt=$2, password=$3, token=$4, limitless=$5 where id = $6")
|
2023-01-18 20:05:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing accounts update statement")
|
|
|
|
}
|
|
|
|
var id int
|
2023-01-23 18:50:24 +01:00
|
|
|
if _, err := stmt.Exec(a.Email, a.Salt, a.Password, a.Token, a.Limitless, a.Id); err != nil {
|
2023-01-18 20:05:10 +01:00
|
|
|
return 0, errors.Wrap(err, "error executing accounts update statement")
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|