mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
edit transactions in bulk
This commit is contained in:
@@ -31,6 +31,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou
|
|||||||
s.PUT("/:id", wc.Edit)
|
s.PUT("/:id", wc.Edit)
|
||||||
s.GET("/:id", wc.Get)
|
s.GET("/:id", wc.Get)
|
||||||
s.GET("check", wc.Check)
|
s.GET("check", wc.Check)
|
||||||
|
s.PUT("/bulk", wc.BulkEdit)
|
||||||
|
|
||||||
return wc
|
return wc
|
||||||
}
|
}
|
||||||
@@ -111,6 +112,23 @@ func (wc *TransactionController) Edit(c *gin.Context) {
|
|||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
BulkEdit
|
||||||
|
Args:
|
||||||
|
*gin.Context: Gin Application Context
|
||||||
|
*/
|
||||||
|
// ROUTE (PUT /transactions/:id)
|
||||||
|
func (wc *TransactionController) BulkEdit(c *gin.Context) {
|
||||||
|
body := new([]models.TransactionEdit)
|
||||||
|
if err := c.ShouldBind(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wm := wc.TransactionService.BulkEdit(c, body)
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Get
|
Get
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -173,6 +173,45 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
|
|||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Bulk Edit
|
||||||
|
|
||||||
|
Updates row in transaction table by id.
|
||||||
|
Args:
|
||||||
|
context.Context: Application context
|
||||||
|
?[]models.Transaction Bulk Edit: Object to edit
|
||||||
|
string: id to search
|
||||||
|
Returns:
|
||||||
|
*models.Transaction: Transaction object from database.
|
||||||
|
*/
|
||||||
|
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) *[]models.Transaction {
|
||||||
|
db := as.Db.WithContext(ctx)
|
||||||
|
tx, _ := db.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
transactions := new([]models.Transaction)
|
||||||
|
|
||||||
|
for _, transaction := range *body {
|
||||||
|
|
||||||
|
amount, _ := transaction.Amount.Float64()
|
||||||
|
|
||||||
|
tm := new(models.Transaction)
|
||||||
|
tm.Id = transaction.Id
|
||||||
|
tm.Description = transaction.Description
|
||||||
|
tm.WalletID = transaction.WalletID
|
||||||
|
tm.TransactionTypeID = transaction.TransactionTypeID
|
||||||
|
tm.TransactionDate = transaction.TransactionDate
|
||||||
|
tm.Amount = float32(math.Round(amount*100) / 100)
|
||||||
|
|
||||||
|
tx.Model(tm).WherePK().UpdateNotZero()
|
||||||
|
*transactions = append(*transactions, *tm)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return transactions
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Get
|
Get
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user