From b4321395d90aeb4a4287ecab14b1540d3a25b2cf Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Sun, 27 Feb 2022 20:02:23 +0000 Subject: [PATCH] Implement backend for reconcilation --- server/http.go | 5 +++++ server/reconcile.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 server/reconcile.go diff --git a/server/http.go b/server/http.go index 80e9bd4..e3fcfc9 100644 --- a/server/http.go +++ b/server/http.go @@ -36,6 +36,10 @@ type ErrorResponse struct { Message string } +type SuccessResponse struct { + Message string +} + // LoadRoutes initializes all the routes. func (h *Handler) LoadRoutes(router *gin.Engine) { router.Use(enableCachingForStaticFiles()) @@ -52,6 +56,7 @@ func (h *Handler) LoadRoutes(router *gin.Engine) { authenticated.Use(h.verifyLoginWithForbidden) authenticated.GET("/dashboard", h.dashboard) authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount) + authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions) authenticated.POST("/account/:accountid", h.editAccount) authenticated.GET("/admin/clear-database", h.clearDatabase) diff --git a/server/reconcile.go b/server/reconcile.go new file mode 100644 index 0000000..4941e53 --- /dev/null +++ b/server/reconcile.go @@ -0,0 +1,40 @@ +package server + +import ( + "database/sql" + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +type ReconcileTransactionsRequest struct { + TransactionIDs []uuid.UUID +} + +func (h *Handler) reconcileTransactions(c *gin.Context) { + var request ReconcileTransactionsRequest + err := c.BindJSON(&request) + if err != nil { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf("parse request: %w", err)) + return + } + + tx, err := h.Service.BeginTx(c.Request.Context(), &sql.TxOptions{}) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err)) + return + } + db := h.Service.WithTx(tx) + for _, transactionID := range request.TransactionIDs { + err := db.SetTransactionReconciled(c.Request.Context(), transactionID) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err)) + return + } + } + tx.Commit() + + c.JSON(http.StatusOK, SuccessResponse{fmt.Sprintf("Set status for %d transactions", len(request.TransactionIDs))}) +}