diff --git a/http/http.go b/http/http.go index f4ef2b6..47212da 100644 --- a/http/http.go +++ b/http/http.go @@ -66,6 +66,7 @@ func (h *Handler) Serve() { withBudget.GET("/budget/:budgetid/settings", h.settings) withBudget.GET("/budget/:budgetid/settings/clear", h.clearBudget) withBudget.GET("/budget/:budgetid/settings/clean-negative", h.cleanNegativeBudget) + withBudget.GET("/budget/:budgetid/transaction/:transactionid", h.transaction) api := router.Group("/api/v1") diff --git a/http/transaction-edit.go b/http/transaction-edit.go new file mode 100644 index 0000000..65a76a7 --- /dev/null +++ b/http/transaction-edit.go @@ -0,0 +1,54 @@ +package http + +import ( + "net/http" + + "git.javil.eu/jacob1123/budgeteer/postgres" + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +type TransactionData struct { + AlwaysNeededData + Transaction *postgres.Transaction + Account *postgres.Account + Categories []postgres.GetCategoriesRow +} + +func (h *Handler) transaction(c *gin.Context) { + data := c.MustGet("data").(AlwaysNeededData) + + transactionID := c.Param("transactionid") + transactionUUID, err := uuid.Parse(transactionID) + if err != nil { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + transaction, err := h.Service.GetTransaction(c.Request.Context(), transactionUUID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + account, err := h.Service.GetAccount(c.Request.Context(), transaction.AccountID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + categories, err := h.Service.GetCategories(c.Request.Context(), data.Budget.ID) + if err != nil { + c.AbortWithError(http.StatusNotFound, err) + return + } + + d := TransactionData{ + data, + &transaction, + &account, + categories, + } + + c.HTML(http.StatusOK, "transaction.html", d) +} diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index b8c7f92..9e67259 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -1,3 +1,7 @@ +-- name: GetTransaction :one +SELECT * FROM transactions +WHERE id = $1; + -- name: CreateTransaction :one INSERT INTO transactions (date, memo, amount, account_id, payee_id, category_id) diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index a79cdbf..c3aacc3 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -63,6 +63,26 @@ func (q *Queries) DeleteAllTransactions(ctx context.Context, budgetID uuid.UUID) return result.RowsAffected() } +const getTransaction = `-- name: GetTransaction :one +SELECT id, date, memo, amount, account_id, category_id, payee_id FROM transactions +WHERE id = $1 +` + +func (q *Queries) GetTransaction(ctx context.Context, id uuid.UUID) (Transaction, error) { + row := q.db.QueryRowContext(ctx, getTransaction, id) + var i Transaction + err := row.Scan( + &i.ID, + &i.Date, + &i.Memo, + &i.Amount, + &i.AccountID, + &i.CategoryID, + &i.PayeeID, + ) + return i, err +} + const getTransactionsByMonthAndCategory = `-- name: GetTransactionsByMonthAndCategory :many SELECT date, category_id, budget_id, amount FROM transactions_by_month diff --git a/web/account.html b/web/account.html index bf055f5..71dceeb 100644 --- a/web/account.html +++ b/web/account.html @@ -27,7 +27,7 @@ {{end}}