feat: deposit funds

This commit is contained in:
juancwu 2026-05-03 18:06:04 +00:00
commit 4298890d68
8 changed files with 353 additions and 1 deletions

View file

@ -302,6 +302,119 @@ func (h *spaceHandler) SpaceCreateBillPage(w http.ResponseWriter, r *http.Reques
}))
}
func (h *spaceHandler) SpaceCreateDepositPage(w http.ResponseWriter, r *http.Request) {
spaceID := r.PathValue("spaceID")
accountID := r.PathValue("accountID")
account, err := h.accountService.GetAccount(accountID)
if err != nil {
slog.Error("failed to load account", "error", err, "account_id", accountID)
ui.Render(w, r, pages.NotFound())
return
}
if account.SpaceID != spaceID {
ui.Render(w, r, pages.NotFound())
return
}
ui.Render(w, r, pages.SpaceCreateDepositPage(pages.SpaceCreateDepositPageProps{
SpaceID: spaceID,
AccountID: accountID,
AccountName: account.Name,
Form: forms.CreateDepositProps{
SpaceID: spaceID,
AccountID: accountID,
Date: time.Now().Format("2006-01-02"),
},
}))
}
func (h *spaceHandler) HandleCreateDeposit(w http.ResponseWriter, r *http.Request) {
spaceID := r.PathValue("spaceID")
accountID := r.PathValue("accountID")
titleInput := strings.TrimSpace(r.FormValue("title"))
amountInput := strings.TrimSpace(r.FormValue("amount"))
dateInput := strings.TrimSpace(r.FormValue("date"))
descriptionInput := strings.TrimSpace(r.FormValue("description"))
formProps := forms.CreateDepositProps{
SpaceID: spaceID,
AccountID: accountID,
Title: titleInput,
Amount: amountInput,
Date: dateInput,
Description: descriptionInput,
}
hasErr := false
if titleInput == "" {
formProps.TitleErr = "Title is required."
hasErr = true
}
var amount decimal.Decimal
if amountInput == "" {
formProps.AmountErr = "Amount is required."
hasErr = true
} else {
amt, err := decimal.NewFromString(amountInput)
if err != nil {
formProps.AmountErr = "Enter a valid amount (e.g. 12.34)."
hasErr = true
} else if !amt.IsPositive() {
formProps.AmountErr = "Amount must be greater than zero."
hasErr = true
} else if amt.Exponent() < -2 {
formProps.AmountErr = "Amount can have at most 2 decimal places."
hasErr = true
} else {
amount = amt
}
}
var occurredAt time.Time
if dateInput == "" {
formProps.DateErr = "Date is required."
hasErr = true
} else {
parsed, err := time.Parse("2006-01-02", dateInput)
if err != nil {
formProps.DateErr = "Enter a valid date."
hasErr = true
} else {
occurredAt = parsed
}
}
if hasErr {
ui.Render(w, r, forms.CreateDeposit(formProps))
return
}
_, err := h.transactionService.Deposit(service.DepositInput{
AccountID: accountID,
Title: titleInput,
Amount: amount,
OccurredAt: occurredAt,
Description: descriptionInput,
})
if err != nil {
slog.Error("failed to create deposit", "error", err, "account_id", accountID)
formProps.GeneralErr = "Something went wrong. Please try again."
ui.Render(w, r, forms.CreateDeposit(formProps))
return
}
redirectTo := routeurl.URL(
"page.app.spaces.space.accounts.account.overview",
"spaceID", spaceID,
"accountID", accountID,
)
w.Header().Set("HX-Redirect", redirectTo)
w.WriteHeader(http.StatusOK)
}
func (h *spaceHandler) HandleCreateBill(w http.ResponseWriter, r *http.Request) {
spaceID := r.PathValue("spaceID")
accountID := r.PathValue("accountID")

View file

@ -10,6 +10,7 @@ import (
type TransactionRepository interface {
CreateBillAtomic(t *model.Transaction, newBalance decimal.Decimal, categoryID *string) error
CreateDepositAtomic(t *model.Transaction, newBalance decimal.Decimal) error
ListByAccount(accountID string, limit, offset int) ([]*model.Transaction, error)
CountByAccount(accountID string) (int, error)
}
@ -54,6 +55,30 @@ func (r *transactionRepository) CreateBillAtomic(t *model.Transaction, newBalanc
})
}
func (r *transactionRepository) CreateDepositAtomic(t *model.Transaction, newBalance decimal.Decimal) error {
return WithTx(r.db, func(tx *sqlx.Tx) error {
insertTxn := `
INSERT INTO transactions
(id, value, type, account_id, title, description, occurred_at, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9);
`
if _, err := tx.Exec(
insertTxn,
t.ID, t.Value, t.Type, t.AccountID, t.Title, t.Description,
t.OccurredAt, t.CreatedAt, t.UpdatedAt,
); err != nil {
return err
}
updateBalance := `UPDATE accounts SET balance = $1, updated_at = $2 WHERE id = $3;`
if _, err := tx.Exec(updateBalance, newBalance, time.Now(), t.AccountID); err != nil {
return err
}
return nil
})
}
func (r *transactionRepository) ListByAccount(accountID string, limit, offset int) ([]*model.Transaction, error) {
query := `
SELECT id, value, type, account_id, title, description, occurred_at, created_at, updated_at

View file

@ -98,6 +98,8 @@ func SetupRoutes(a *app.App) http.Handler {
g.Get("/transactions", spaceH.SpaceAccountTransactionsPage).Name("page.app.spaces.space.accounts.account.transactions")
g.Get("/bills/create", spaceH.SpaceCreateBillPage).Name("page.app.spaces.space.accounts.account.bills.create")
g.Post("/bills/create", spaceH.HandleCreateBill).Name("action.app.spaces.space.accounts.account.bills.create")
g.Get("/deposits/create", spaceH.SpaceCreateDepositPage).Name("page.app.spaces.space.accounts.account.deposits.create")
g.Post("/deposits/create", spaceH.HandleCreateDeposit).Name("action.app.spaces.space.accounts.account.deposits.create")
})
})
})

View file

@ -89,6 +89,61 @@ func (s *TransactionService) PayBill(input PayBillInput) (*model.Transaction, er
return txn, nil
}
type DepositInput struct {
AccountID string
Title string
Amount decimal.Decimal
OccurredAt time.Time
Description string
}
func (s *TransactionService) Deposit(input DepositInput) (*model.Transaction, error) {
title := strings.TrimSpace(input.Title)
if title == "" {
return nil, fmt.Errorf("title is required")
}
if input.AccountID == "" {
return nil, fmt.Errorf("account id is required")
}
if !input.Amount.IsPositive() {
return nil, fmt.Errorf("amount must be greater than zero")
}
if input.OccurredAt.IsZero() {
return nil, fmt.Errorf("date is required")
}
account, err := s.accountService.GetAccount(input.AccountID)
if err != nil {
return nil, fmt.Errorf("failed to load account: %w", err)
}
newBalance := account.Balance.Add(input.Amount)
now := time.Now()
var description *string
if d := strings.TrimSpace(input.Description); d != "" {
description = &d
}
txn := &model.Transaction{
ID: uuid.NewString(),
Value: input.Amount,
Type: model.TransactionTypeDeposit,
AccountID: input.AccountID,
Title: title,
Description: description,
OccurredAt: input.OccurredAt,
CreatedAt: now,
UpdatedAt: now,
}
if err := s.transactionRepo.CreateDepositAtomic(txn, newBalance); err != nil {
return nil, fmt.Errorf("failed to create deposit transaction: %w", err)
}
return txn, nil
}
func (s *TransactionService) ListByAccount(accountID string, limit, offset int) ([]*model.Transaction, error) {
if limit <= 0 {
limit = 25

View file

@ -0,0 +1,134 @@
package forms
import "git.juancwu.dev/juancwu/budgit/internal/routeurl"
import "git.juancwu.dev/juancwu/budgit/internal/ui/components/button"
import "git.juancwu.dev/juancwu/budgit/internal/ui/components/card"
import "git.juancwu.dev/juancwu/budgit/internal/ui/components/form"
import "git.juancwu.dev/juancwu/budgit/internal/ui/components/input"
import "git.juancwu.dev/juancwu/budgit/internal/ui/components/textarea"
type CreateDepositProps struct {
SpaceID string
AccountID string
Title string
Amount string
Date string
Description string
TitleErr string
AmountErr string
DateErr string
GeneralErr string
}
templ CreateDeposit(props CreateDepositProps) {
<form hx-post={ routeurl.URL("action.app.spaces.space.accounts.account.deposits.create", "spaceID", props.SpaceID, "accountID", props.AccountID) }>
@card.Card(card.Props{Class: "rounded-sm"}) {
@card.Content(card.ContentProps{Class: "p-4 space-y-4"}) {
if props.GeneralErr != "" {
@form.Message(form.MessageProps{Variant: form.MessageVariantError}) {
{ props.GeneralErr }
}
}
@form.Item() {
@form.Label(form.LabelProps{For: "title"}) {
Title
}
@input.Input(input.Props{
ID: "title",
Name: "title",
Type: input.TypeText,
Placeholder: "e.g. Paycheck",
Class: "rounded-sm",
Value: props.Title,
HasError: props.TitleErr != "",
Required: true,
Attributes: templ.Attributes{
"autocomplete": "off",
"autofocus": "",
},
})
if props.TitleErr != "" {
@form.Message(form.MessageProps{Variant: form.MessageVariantError}) {
{ props.TitleErr }
}
}
}
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
@form.Item() {
@form.Label(form.LabelProps{For: "amount"}) {
Amount
}
@input.Input(input.Props{
ID: "amount",
Name: "amount",
Type: input.TypeNumber,
Placeholder: "0.00",
Class: "rounded-sm",
Value: props.Amount,
HasError: props.AmountErr != "",
Required: true,
Attributes: templ.Attributes{
"step": "0.01",
"min": "0",
"inputmode": "decimal",
"autocomplete": "off",
},
})
if props.AmountErr != "" {
@form.Message(form.MessageProps{Variant: form.MessageVariantError}) {
{ props.AmountErr }
}
}
}
@form.Item() {
@form.Label(form.LabelProps{For: "date"}) {
Date
}
@input.Input(input.Props{
ID: "date",
Name: "date",
Type: input.TypeDate,
Class: "rounded-sm",
Value: props.Date,
HasError: props.DateErr != "",
Required: true,
})
if props.DateErr != "" {
@form.Message(form.MessageProps{Variant: form.MessageVariantError}) {
{ props.DateErr }
}
}
}
</div>
@form.Item() {
@form.Label(form.LabelProps{For: "description"}) {
Description
}
@textarea.Textarea(textarea.Props{
ID: "description",
Name: "description",
Placeholder: "Anything extra worth remembering",
Rows: 3,
Value: props.Description,
})
@form.Description() {
Optional.
}
}
}
@card.Footer(card.FooterProps{Class: "flex justify-end gap-2"}) {
@button.Button(button.Props{
Variant: button.VariantGhost,
Href: routeurl.URL("page.app.spaces.space.accounts.account.overview", "spaceID", props.SpaceID, "accountID", props.AccountID),
}) {
Cancel
}
@button.Button(button.Props{Type: button.TypeSubmit}) {
Deposit
}
}
}
</form>
}

View file

@ -61,6 +61,7 @@ templ SpaceAccountPage(props SpaceAccountPageProps) {
@button.Button(button.Props{
Class: "w-full flex gap-2 md:gap-4 items-center",
Variant: button.VariantSecondary,
Href: routeurl.URL("page.app.spaces.space.accounts.account.deposits.create", "spaceID", props.SpaceID, "accountID", props.AccountID),
}) {
Deposit Funds
@icon.BanknoteArrowDown()

View file

@ -42,6 +42,7 @@ templ SpaceAccountTransactionsPage(props SpaceAccountTransactionsPageProps) {
}
@button.Button(button.Props{
Variant: button.VariantSecondary,
Href: routeurl.URL("page.app.spaces.space.accounts.account.deposits.create", "spaceID", props.SpaceID, "accountID", props.AccountID),
Class: "flex gap-2 items-center",
}) {
@icon.BanknoteArrowDown()

View file

@ -1,4 +1,25 @@
package pages
templ SpaceCreateDeposit() {
import "git.juancwu.dev/juancwu/budgit/internal/ui/forms"
import "git.juancwu.dev/juancwu/budgit/internal/ui/layouts"
type SpaceCreateDepositPageProps struct {
SpaceID string
AccountID string
AccountName string
Form forms.CreateDepositProps
}
templ SpaceCreateDepositPage(props SpaceCreateDepositPageProps) {
@layouts.App("Deposit Funds", spaceOverviewSidebarContent(), spaceSpecificSidebarContent(props.SpaceID)) {
<div class="container max-w-3xl px-6 py-8 mx-auto space-y-8">
<div>
<h1 class="text-3xl font-bold">Deposit Funds</h1>
<p class="text-muted-foreground mt-2">
Record a deposit into { props.AccountName }.
</p>
</div>
@forms.CreateDeposit(props.Form)
</div>
}
}