add in-memory DB for accounts, with tests!

This commit is contained in:
ari melody 2026-07-31 04:20:39 +01:00
parent 49e14b5bc5
commit 5a540184c9
Signed by: ari
GPG key ID: CF99829C92678188
9 changed files with 534 additions and 45 deletions

View file

@ -20,8 +20,8 @@ func NewAccountRepositoryPostgres(db *sqlx.DB) *AccountRepositoryPostgres {
return &AccountRepositoryPostgres{ db: db }
}
func (repo *AccountRepositoryPostgres) GetAll() ([]model.Account, error) {
var accounts = []model.Account{}
func (repo *AccountRepositoryPostgres) GetAll() ([]*model.Account, error) {
var accounts = []*model.Account{}
err := repo.db.Select(&accounts, "SELECT * FROM account ORDER BY created_at ASC")
if err != nil {
@ -79,22 +79,6 @@ func (repo *AccountRepositoryPostgres) GetByEmail(email string) (*model.Account,
return &account, nil
}
func (repo *AccountRepositoryPostgres) GetBySession(sessionToken string) (*model.Account, error) {
if sessionToken == "" { return nil, nil }
account := model.Account{}
err := repo.db.Get(&account, "SELECT account.* FROM account JOIN token ON id=account WHERE token=$1", sessionToken)
if err != nil {
if strings.Contains(err.Error(), "no rows") {
return nil, nil
}
return nil, err
}
return &account, nil
}
func (repo *AccountRepositoryPostgres) Create(
username string,
password string,
@ -175,25 +159,30 @@ func (repo *AccountRepositoryPostgres) RemoveAvatar(id string) error {
return err
}
func (repo *AccountRepositoryPostgres) Delete(accountID string) error {
_, err := repo.db.Exec("DELETE FROM account WHERE id=$1", accountID)
func (repo *AccountRepositoryPostgres) Delete(id string) error {
_, err := repo.db.Exec("DELETE FROM account WHERE id=$1", id)
return err
}
// Increment the number of account login failure attempts,
// returning the current fail count.
func (repo *AccountRepositoryPostgres) IncrementFails(accountID string) (int, error) {
func (repo *AccountRepositoryPostgres) IncrementFails(id string) (int, error) {
failAttempts := 0
err := repo.db.Get(&failAttempts, "UPDATE account SET fail_attempts = fail_attempts + 1 WHERE id=$1 RETURNING fail_attempts", accountID)
err := repo.db.Get(&failAttempts, "UPDATE account SET fail_attempts = fail_attempts + 1 WHERE id=$1 RETURNING fail_attempts", id)
return failAttempts, err
}
func (repo *AccountRepositoryPostgres) Lock(accountID string) error {
_, err := repo.db.Exec("UPDATE account SET locked = true WHERE id=$1", accountID)
func (repo *AccountRepositoryPostgres) ResetFails(id string) error {
_, err := repo.db.Exec("UPDATE account SET fail_attempts = 0 WHERE id=$1", id)
return err
}
func (repo *AccountRepositoryPostgres) Unlock(accountID string) error {
_, err := repo.db.Exec("UPDATE account SET locked = false, fail_attempts = 0 WHERE id=$1", accountID)
func (repo *AccountRepositoryPostgres) Lock(id string) error {
_, err := repo.db.Exec("UPDATE account SET locked = true WHERE id=$1", id)
return err
}
func (repo *AccountRepositoryPostgres) Unlock(id string) error {
_, err := repo.db.Exec("UPDATE account SET locked = false, fail_attempts = 0 WHERE id=$1", id)
return err
}