From fded9418f05ada5d91c6b5ed705f1002d0252905 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Wed, 14 Aug 2024 22:58:32 +0530 Subject: [PATCH] setting api server and createAccount func --- api/account.go | 15 +++++++++++++++ api/server.go | 11 +++++++---- db/sqlc/store.go | 2 ++ utils/test.go | 8 ++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/api/account.go b/api/account.go index a2e8854..5b69383 100644 --- a/api/account.go +++ b/api/account.go @@ -1,6 +1,7 @@ package api import ( + db "example/laxmi_chit_fund/db/sqlc" "net/http" "github.com/gin-gonic/gin" @@ -22,4 +23,18 @@ func (server *Server) createAccount(ctx *gin.Context) { // Create the account after validating the input + 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) + } diff --git a/api/server.go b/api/server.go index 538d1ca..b42fb87 100644 --- a/api/server.go +++ b/api/server.go @@ -2,20 +2,23 @@ package api import ( db "example/laxmi_chit_fund/db/sqlc" - "example/laxmi_chit_fund/utils" "github.com/gin-gonic/gin" ) type Server struct { - config utils.Config store db.Store router *gin.Engine } -// func NewServer(store *db.Store) *Server{ +func NewServer(store *db.Store) *Server { + server := &Server{store: store} + router := gin.Default(), -// } + router.POST("/accounts", server.createAccount) + server.router = router + return server +} // Setup Gin Router to handle all the incoming API routes diff --git a/db/sqlc/store.go b/db/sqlc/store.go index b6cdb30..6f3cc9a 100644 --- a/db/sqlc/store.go +++ b/db/sqlc/store.go @@ -12,6 +12,8 @@ type Store interface { TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error) + CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) // Add this line + } // SQLStore provides all functions to execute SQL queries and transactions diff --git a/utils/test.go b/utils/test.go index d4b585b..2c974ee 100644 --- a/utils/test.go +++ b/utils/test.go @@ -1 +1,9 @@ package utils + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, world!") +}