-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: GitHub <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"net/http" | ||
|
||
db "github.com/1shubham7/bank/db/sqlc" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type transferRequest struct { | ||
FromAccountID int64 `json:"from_account_id" binding:"required,min=1"` | ||
ToAccountID int64 `json:"to_account_id" binding:"required,min=1"` | ||
Amount int64 `json:"amount" binding:"required,gt=0"` | ||
Currency string `json:"currency" binding:"required,oneof=USD INR EUR CAD YUAN"` | ||
} | ||
|
||
func (server *Server) createTransfer(ctx *gin.Context) { | ||
var req transferRequest | ||
|
||
err := ctx.ShouldBindJSON(&req) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
if !server.validAccount(ctx, req.FromAccountID, req.Currency){ | ||
return | ||
} | ||
|
||
if !server.validAccount(ctx, req.ToAccountID, req.Currency){ | ||
return | ||
} | ||
|
||
arg := db.TransferTransectionParams{ | ||
FromAccountID: req.FromAccountID, | ||
ToAccountID: req.ToAccountID, | ||
Amount: req.Amount, | ||
} | ||
|
||
transferTransectionResult, err := server.store.TransferTransection(ctx, arg) | ||
|
||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, transferTransectionResult) | ||
} | ||
|
||
func (server *Server) validAccount(ctx *gin.Context, accountID int64, currency string) (bool) { | ||
account, err := server.store.GetAccount(ctx, accountID) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
ctx.JSON(http.StatusNotFound, errorResponse(err)) | ||
return false | ||
} | ||
|
||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return false | ||
} | ||
|
||
if account.Currency != currency { | ||
err := fmt.Errorf("account [%d] currency mismatch: %s vs %s", account.ID, account.Currency, currency) | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return false | ||
} | ||
|
||
return true | ||
} |