chore: overhaul onboarding steps
This commit is contained in:
parent
acb7f511f9
commit
d413193366
12 changed files with 358 additions and 186 deletions
59
internal/repository/account.go
Normal file
59
internal/repository/account.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"git.juancwu.dev/juancwu/budgit/internal/model"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
var ErrAccountNotFound = errors.New("account not found")
|
||||
|
||||
type AccountRepository interface {
|
||||
Create(account *model.Account) error
|
||||
ByID(id string) (*model.Account, error)
|
||||
BySpaceID(spaceID string) ([]*model.Account, error)
|
||||
Delete(id string) error
|
||||
}
|
||||
|
||||
type accountRepository struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewAccountRepository(db *sqlx.DB) AccountRepository {
|
||||
return &accountRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *accountRepository) Create(account *model.Account) error {
|
||||
query := `INSERT INTO accounts (id, name, space_id, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5);`
|
||||
_, err := r.db.Exec(query, account.ID, account.Name, account.SpaceID, account.CreatedAt, account.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *accountRepository) ByID(id string) (*model.Account, error) {
|
||||
account := &model.Account{}
|
||||
query := `SELECT * FROM accounts WHERE id = $1;`
|
||||
err := r.db.Get(account, query, id)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrAccountNotFound
|
||||
}
|
||||
return account, err
|
||||
}
|
||||
|
||||
func (r *accountRepository) BySpaceID(spaceID string) ([]*model.Account, error) {
|
||||
var accounts []*model.Account
|
||||
query := `SELECT * FROM accounts WHERE space_id = $1 ORDER BY created_at ASC;`
|
||||
err := r.db.Select(&accounts, query, spaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) Delete(id string) error {
|
||||
query := `DELETE FROM accounts WHERE id = $1;`
|
||||
_, err := r.db.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
77
internal/repository/account_test.go
Normal file
77
internal/repository/account_test.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.juancwu.dev/juancwu/budgit/internal/model"
|
||||
"git.juancwu.dev/juancwu/budgit/internal/testutil"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAccountRepository_CreateAndRead(t *testing.T) {
|
||||
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
|
||||
repo := NewAccountRepository(dbi.DB)
|
||||
|
||||
user := testutil.CreateTestUser(t, dbi.DB, "account-create@example.com", nil)
|
||||
space := testutil.CreateTestSpace(t, dbi.DB, user.ID, "Space With Account")
|
||||
|
||||
now := time.Now()
|
||||
account := &model.Account{
|
||||
ID: uuid.NewString(),
|
||||
Name: "Money Account",
|
||||
SpaceID: space.ID,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
err := repo.Create(account)
|
||||
require.NoError(t, err)
|
||||
|
||||
fetched, err := repo.ByID(account.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Money Account", fetched.Name)
|
||||
assert.Equal(t, space.ID, fetched.SpaceID)
|
||||
|
||||
accounts, err := repo.BySpaceID(space.ID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, accounts, 1)
|
||||
assert.Equal(t, account.ID, accounts[0].ID)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountRepository_ByID_NotFound(t *testing.T) {
|
||||
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
|
||||
repo := NewAccountRepository(dbi.DB)
|
||||
|
||||
_, err := repo.ByID(uuid.NewString())
|
||||
assert.ErrorIs(t, err, ErrAccountNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountRepository_Delete(t *testing.T) {
|
||||
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
|
||||
repo := NewAccountRepository(dbi.DB)
|
||||
|
||||
user := testutil.CreateTestUser(t, dbi.DB, "account-delete@example.com", nil)
|
||||
space := testutil.CreateTestSpace(t, dbi.DB, user.ID, "Delete Space")
|
||||
|
||||
now := time.Now()
|
||||
account := &model.Account{
|
||||
ID: uuid.NewString(),
|
||||
Name: "To Delete",
|
||||
SpaceID: space.ID,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
require.NoError(t, repo.Create(account))
|
||||
|
||||
err := repo.Delete(account.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = repo.ByID(account.ID)
|
||||
assert.ErrorIs(t, err, ErrAccountNotFound)
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue