From 1297f48645f91a1f0c4551f97f4d154e38cb1c94 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 22:26:33 +0200 Subject: [PATCH] transaction edit --- pkg/controllers/subscriptions.go | 2 +- pkg/controllers/transactions.go | 32 ++++++++++++++++++++++ pkg/models/transactions.go | 9 ++++++ pkg/services/transactions.go | 47 +++++++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/subscriptions.go b/pkg/controllers/subscriptions.go index b4c6f1c..30c4b0c 100644 --- a/pkg/controllers/subscriptions.go +++ b/pkg/controllers/subscriptions.go @@ -19,8 +19,8 @@ func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGr s.PUT("/end/:id", wc.End) s.POST("", wc.New) s.PUT("/:id", wc.Edit) - s.GET("", wc.GetAll) s.GET("/:id", wc.Get) + s.GET("", wc.GetAll) return wc } diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go index b800691..540751d 100644 --- a/pkg/controllers/transactions.go +++ b/pkg/controllers/transactions.go @@ -18,6 +18,8 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou s.POST("", wc.New) s.GET("", wc.GetAll) + s.PUT("/:id", wc.Edit) + s.GET("/:id", wc.Get) return wc } @@ -45,3 +47,33 @@ func (wc *TransactionController) GetAll(c *gin.Context) { c.JSON(200, fr) } + +func (wc *TransactionController) Edit(c *gin.Context) { + body := new(models.TransactionEdit) + if err := c.ShouldBind(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + id := c.Param("id") + + wm := wc.TransactionService.Edit(c, body, id) + c.JSON(200, wm) +} + +func (wc *TransactionController) Get(c *gin.Context) { + body := new(models.Auth) + params := new(models.Params) + + auth := c.MustGet("auth") + body.Id = auth.(*models.Auth).Id + + id := c.Param("id") + + embed, _ := c.GetQuery("embed") + params.Embed = embed + + fr := wc.TransactionService.Get(c, body, id, params) + + c.JSON(200, fr) +} diff --git a/pkg/models/transactions.go b/pkg/models/transactions.go index 6dd9de9..2076a3c 100644 --- a/pkg/models/transactions.go +++ b/pkg/models/transactions.go @@ -26,3 +26,12 @@ type NewTransactionBody struct { Description string `json:"description" form:"description"` Amount json.Number `json:"amount" form:"amount"` } + +type TransactionEdit struct { + Id string `json:"id" form:"id"` + WalletID string `json:"walletId" form:"walletId"` + TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` + TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` + Description string `json:"description" form:"description"` + Amount json.Number `json:"amount" form:"amount"` +} diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index a7406af..0e04e9d 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -5,6 +5,7 @@ import ( "math" "time" "wallet-api/pkg/models" + "wallet-api/pkg/utl/common" "github.com/go-pg/pg/v10" ) @@ -19,6 +20,9 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti tm := new(models.Transaction) + tx, _ := db.Begin() + defer tx.Rollback() + amount, _ := body.Amount.Float64() tm.Init() @@ -32,7 +36,8 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti tm.TransactionDate = time.Now() } - db.Model(tm).Insert() + tx.Model(tm).Insert() + tx.Commit() return tm } @@ -67,3 +72,43 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle tx.Commit() } + +func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) *models.Transaction { + db := as.Db.WithContext(ctx) + + amount, _ := body.Amount.Float64() + + tm := new(models.Transaction) + tm.Id = id + tm.Description = body.Description + tm.WalletID = body.WalletID + tm.TransactionTypeID = body.TransactionTypeID + tm.TransactionDate = body.TransactionDate + tm.Amount = float32(math.Round(amount*100) / 100) + + tx, _ := db.Begin() + defer tx.Rollback() + + tx.Model(tm).WherePK().UpdateNotZero() + + tx.Commit() + + return tm +} + +func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Transaction { + db := as.Db.WithContext(ctx) + + wm := new(models.Transaction) + wm.Id = id + + tx, _ := db.Begin() + defer tx.Rollback() + + qry := tx.Model(wm) + common.GenerateEmbed(qry, params.Embed).WherePK().Select() + + tx.Commit() + + return wm +}