-
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
1 parent
902c605
commit 4a90b74
Showing
9 changed files
with
266 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Makefile | ||
sqlc.yaml |
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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
# make ${target} DB_CONTAINER=${your_db_container} DB_USERNAME=${your_db_username} DB_NAME=${your_db_name} | ||
|
||
postgres: | ||
docker run --name postgres-udemy -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:alpine | ||
docker run --name postgres-udemy -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=123456789 -p 5432:5432 -d postgres:alpine | ||
|
||
createdb: | ||
docker exec -it postgres-udemy createdb --username=root --owner=root simple_bank | ||
docker exec -it postgresql createdb --username=postgres --owner=postgres simple_bank | ||
|
||
dropdb: | ||
docker exec -it postgres-udemy dropdb simple_bank | ||
docker exec -it postgresql dropdb --username=postgres simple_bank | ||
|
||
# Path: Makefile | ||
# Path: Mafkefile | ||
migrateup: | ||
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up | ||
migrate -path db/migration -database "postgresql://postgres:[email protected]/simple_bank?sslmode=disable" -verbose up | ||
|
||
migratedown: | ||
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down | ||
migrate -path db/migration -database "postgresql://postgres:[email protected]:5432/simple_bank?sslmode=disable" -verbose down | ||
|
||
sqlc: | ||
sqlc generate | ||
|
||
test: | ||
go test -v -cover ./... | ||
|
||
.PHONY: createdb dropdb postgres migrateup migratedown sqlc test | ||
server: | ||
go run main.go | ||
|
||
.PHONY: createdb dropdb postgres migrateup migratedown sqlc test server |
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,86 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
db "go-udemy.sqlc.dev/app/db/sqlc" | ||
) | ||
|
||
type CreateAccountRequest struct { | ||
Owner string `json:"owner" binding:"required"` | ||
Currency string `json:"currency" binding:"required,oneof=USD EUR"` | ||
} | ||
|
||
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, | ||
Balance: 0, | ||
Currency: req.Currency, | ||
} | ||
|
||
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 == sql.ErrNoRows { | ||
ctx.JSON(http.StatusNotFound, errorResponse(err)) | ||
return | ||
} | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, account) | ||
} | ||
|
||
type ListAccountRequest struct { | ||
PageID int64 `form:"page_id" binding:"required,min=1"` | ||
PageSize int64 `form:"page_size" binding:"required,min=5,max=10"` | ||
} | ||
|
||
func (server *Server) listAccounts(ctx *gin.Context) { | ||
var req ListAccountRequest | ||
if err := ctx.ShouldBindQuery(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
arg := db.ListAccountsParams{ | ||
Limit: req.PageSize, | ||
Offset: (req.PageID - 1) * req.PageSize, | ||
} | ||
|
||
accounts, err := server.store.ListAccounts(ctx, arg) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, accounts) | ||
} |
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,35 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
db "go-udemy.sqlc.dev/app/db/sqlc" | ||
) | ||
|
||
// Server is the API server | ||
type Server struct { | ||
store *db.Store | ||
router *gin.Engine | ||
} | ||
|
||
// NewServer creates a new HTTP server and set up routing | ||
func NewServer(store *db.Store) *Server { | ||
server := &Server{store: store} | ||
router := gin.Default() | ||
|
||
router.POST("/accounts", server.createAccount) | ||
router.GET("/accounts/:id", server.getAccount) | ||
router.GET("/accounts", server.listAccounts) | ||
// router.POST("/transfers", server.createTransfer) | ||
|
||
server.router = router | ||
return server | ||
} | ||
|
||
// Start runs the HTTP server on a specific address | ||
func (server *Server) Start(address string) error { | ||
return server.router.Run(address) | ||
} | ||
|
||
func errorResponse(err error) gin.H { | ||
return gin.H{"error": err.Error()} | ||
} |
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ import ( | |
|
||
const ( | ||
dbDriver = "postgres" | ||
dbSource = "postgres://root:secret@localhost:5432/simple_bank?sslmode=disable" | ||
dbSource = "postgresql://postgres:[email protected]/simple_bank?sslmode=disable" | ||
) | ||
|
||
var testQueries *Queries | ||
|
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"go-udemy.sqlc.dev/app/api" | ||
db "go-udemy.sqlc.dev/app/db/sqlc" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
const ( | ||
dbDriver = "postgres" | ||
dbSource = "postgresql://postgres:[email protected]/simple_bank?sslmode=disable" | ||
serverAddress = "0.0.0.0:8080" | ||
) | ||
|
||
func main() { | ||
conn, err := sql.Open(dbDriver, dbSource) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
store := db.NewStore(conn) | ||
server := api.NewServer(store) | ||
|
||
err = server.Start(serverAddress) | ||
if err != nil { | ||
panic(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 |
---|---|---|
|
@@ -4,7 +4,7 @@ sql: | |
queries: "./db/query/" | ||
schema: "./db/migration/" | ||
database: | ||
uri: "postgres://root:secret@localhost:5432/simple_bank?sslmode=disable" | ||
uri: "postgresql://postgres:[email protected]/simple_bank?sslmode=disable" | ||
gen: | ||
go: | ||
package: "db" | ||
|