From 149fbfa21199de3a0ead312884d6764a4b515479 Mon Sep 17 00:00:00 2001 From: Fran Jurmanovic Date: Fri, 30 Jul 2021 23:40:23 +0200 Subject: [PATCH] wallet edit --- pkg/controllers/wallets.go | 41 ++++++++++++++++++++++++++------------ pkg/models/wallets.go | 5 +++++ pkg/services/wallets.go | 30 ++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/pkg/controllers/wallets.go b/pkg/controllers/wallets.go index 62c266f..12e865b 100644 --- a/pkg/controllers/wallets.go +++ b/pkg/controllers/wallets.go @@ -18,6 +18,8 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle s.POST("", wc.New) s.GET("", wc.GetAll) + s.PUT("/:id", wc.Edit) + s.GET("/:id", wc.Get) return wc } @@ -37,18 +39,6 @@ func (wc *WalletsController) New(c *gin.Context) { c.JSON(200, wm) } -func (wc *WalletsController) Get(c *gin.Context) { - body := new(models.Auth) - - embed, _ := c.GetQuery("embed") - auth := c.MustGet("auth") - body.Id = auth.(*models.Auth).Id - - wm := wc.WalletService.Get(c, body, embed) - - c.JSON(200, wm) -} - func (wc *WalletsController) GetAll(c *gin.Context) { body := new(models.Auth) auth := c.MustGet("auth") @@ -59,5 +49,30 @@ func (wc *WalletsController) GetAll(c *gin.Context) { wc.WalletService.GetAll(c, body, fr) c.JSON(200, fr) - +} + +func (wc *WalletsController) Edit(c *gin.Context) { + body := new(models.WalletEdit) + if err := c.ShouldBind(body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + id := c.Param("id") + + wm := wc.WalletService.Edit(c, body, id) + c.JSON(200, wm) +} + +func (wc *WalletsController) Get(c *gin.Context) { + params := new(models.Params) + + id := c.Param("id") + + embed, _ := c.GetQuery("embed") + params.Embed = embed + + fr := wc.WalletService.Get(c, id, params) + + c.JSON(200, fr) } diff --git a/pkg/models/wallets.go b/pkg/models/wallets.go index b421d4f..c45ed24 100644 --- a/pkg/models/wallets.go +++ b/pkg/models/wallets.go @@ -28,3 +28,8 @@ type WalletTransactions struct { LastMonth float32 NextMonth float32 } + +type WalletEdit struct { + Id string `json:"id" form:"id"` + Name string `json:"name" form:"name"` +} diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index e5b55fb..6ab4e44 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -25,13 +25,36 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *mod return walletModel } -func (as *WalletService) Get(ctx context.Context, am *models.Auth, embed string) *models.Wallet { +func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) *models.Wallet { + db := as.Db.WithContext(ctx) + + tm := new(models.Wallet) + tm.Id = id + tm.Name = body.Name + + tx, _ := db.Begin() + defer tx.Rollback() + + tx.Model(tm).WherePK().UpdateNotZero() + + tx.Commit() + + return tm +} + +func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) *models.Wallet { db := as.Db.WithContext(ctx) wm := new(models.Wallet) + wm.Id = id - query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id) - common.GenerateEmbed(query, embed).Select() + tx, _ := db.Begin() + defer tx.Rollback() + + qry := tx.Model(wm) + common.GenerateEmbed(qry, params.Embed).WherePK().Select() + + tx.Commit() return wm } @@ -148,7 +171,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI wm.Currency = "USD" wm.WalletId = walletId - return wm }