-
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.
- Loading branch information
Showing
10 changed files
with
268 additions
and
12 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,87 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/jackc/pgx/v5" | ||
db "github.com/song0180/simple-bank/db/sqlc" | ||
) | ||
|
||
type createAccountRequest struct { | ||
Owner string `json:"owner" binding:"required"` | ||
Currency string `json:"currency" binding:"required,oneof=USD CNY"` | ||
} | ||
|
||
func (server *Server) createAccount(ctx *gin.Context) { | ||
var req createAccountRequest | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
arg := db.CreateAccountParams{ | ||
Owner: req.Owner, | ||
Currency: req.Currency, | ||
Balance: 0, | ||
} | ||
|
||
account, err := server.store.CreateAccount(ctx, arg) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, account) | ||
} | ||
|
||
type getAccountRequest struct { | ||
ID int64 `uri:"id" binding:"required,min=1"` | ||
} | ||
|
||
func (server *Server) getAccount(ctx *gin.Context) { | ||
var req getAccountRequest | ||
if err := ctx.ShouldBindUri(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
account, err := server.store.GetAccount(ctx, req.ID) | ||
if err != nil { | ||
if err == pgx.ErrNoRows { | ||
ctx.JSON(http.StatusNotFound, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, account) | ||
} | ||
|
||
type getAccountListRequest struct { | ||
Page int32 `form:"page" binding:"required,min=1"` | ||
Limit int32 `form:"limit" binding:"required,min=5,max=10"` | ||
} | ||
|
||
func (server *Server) getAccountList(ctx *gin.Context) { | ||
var req getAccountListRequest | ||
if err := ctx.ShouldBindQuery(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
arg := db.ListAccountParams{ | ||
Limit: req.Limit, | ||
Offset: (req.Page - 1) * req.Limit, | ||
} | ||
|
||
account, err := server.store.ListAccount(ctx, arg) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, account) | ||
} |
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,34 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
db "github.com/song0180/simple-bank/db/sqlc" | ||
) | ||
|
||
type Server struct { | ||
store *db.Store | ||
router *gin.Engine | ||
} | ||
|
||
func NewServer(store *db.Store) *Server { | ||
server := &Server{ | ||
store: store, | ||
} | ||
router := gin.Default() | ||
|
||
// routes | ||
router.POST("/accounts", server.createAccount) | ||
router.GET("/accounts/:id", server.getAccount) | ||
router.GET("/accounts", server.getAccountList) | ||
|
||
server.router = router | ||
return server | ||
} | ||
|
||
func (server *Server) Start(address string) error { | ||
return server.router.Run(address) | ||
} | ||
|
||
func errorResponse(err error) gin.H { | ||
return gin.H{"error": err.Error()} | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
_ "github.com/lib/pq" | ||
"github.com/song0180/simple-bank/api" | ||
db "github.com/song0180/simple-bank/db/sqlc" | ||
) | ||
|
||
const ( | ||
dbDriver = "postgres" | ||
dbSource = "postgresql://root:secret@localhost:5432/simple-bank?sslmode=disable" | ||
serverAddress = "127.0.0.1:8080" | ||
) | ||
|
||
func main() { | ||
var err error | ||
testConnPool, err := pgxpool.New(context.Background(), dbSource) | ||
|
||
if err != nil { | ||
log.Fatal("Unable to connect to the DB", err) | ||
} | ||
|
||
store := db.NewStore(testConnPool) | ||
server := api.NewServer(store) | ||
|
||
err = server.Start(serverAddress) | ||
if err != nil { | ||
log.Fatal("Unable to start the server:", err) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ sql: | |
out: './db/sqlc' | ||
sql_package: 'pgx/v5' | ||
emit_json_tags: true | ||
emit_empty_slices: true |