-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transfer api with test and custom validation
- Loading branch information
1 parent
f9d2b20
commit bae48bd
Showing
11 changed files
with
445 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,69 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
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,min=1"` | ||
Currency string `json:"currency" binding:"required,currency"` | ||
} | ||
|
||
func (server *Server) createTransfer(ctx *gin.Context) { | ||
var req transferRequest | ||
|
||
if err := ctx.ShouldBindJSON(&req); 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.TransferTxParams{ | ||
FromAccountID: req.FromAccountID, | ||
ToAccountID: req.ToAccountID, | ||
Amount: req.Amount, | ||
} | ||
|
||
account, err := server.store.TransferTxAccount(ctx, arg) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusCreated, account) | ||
} | ||
|
||
func (server *Server) validAccount(ctx *gin.Context, accountID int64, currency string) bool { | ||
account, err := server.store.GetAccount(ctx, accountID) | ||
if err != nil { | ||
if errors.Is(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] mismatch: %s vs %s ", accountID, account.Currency, currency) | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return false | ||
} | ||
|
||
return true | ||
} |
Oops, something went wrong.