Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active August 15, 2022 13:18
Show Gist options
  • Save dipeshhkc/675dc924d33be80a7cbe7f1e3a5bec83 to your computer and use it in GitHub Desktop.
Save dipeshhkc/675dc924d33be80a7cbe7f1e3a5bec83 to your computer and use it in GitHub Desktop.
func (u userController) TransferMoney(c *gin.Context) {
log.Print("[UserController]...get all Users")
txHandle := c.MustGet("db_trx").(*gorm.DB)
var moneyTransfer model.MoneyTransfer
if err := c.ShouldBindJSON(&moneyTransfer); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := u.userService.WithTrx(txHandle).IncrementMoney(moneyTransfer.Receiver, moneyTransfer.Amount); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Error while incrementing money"})
return
}
if err := u.userService.WithTrx(txHandle).DecrementMoney(moneyTransfer.Giver, moneyTransfer.Amount); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Error while decrementing money"})
return
}
c.JSON(http.StatusOK, gin.H{"msg": "Successfully Money Transferred"})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment