diff --git a/api/accounts.go b/api/accounts.go index 79fb6a5..b4dec13 100644 --- a/api/accounts.go +++ b/api/accounts.go @@ -1,6 +1,7 @@ package api import ( + "database/sql" "net/http" db "github.com/1shubham7/bank/db/sqlc" @@ -35,5 +36,32 @@ func (server *Server) createAccount(ctx *gin.Context){ 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 + + err := ctx.ShouldBindJSON(&req) + if 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)) + } + + ctx.JSON(http.StatusInternalServerError, errorResponse(err)) + return + } + ctx.JSON(http.StatusOK, account) } \ No newline at end of file diff --git a/api/server.go b/api/server.go index db9ec1c..b26e936 100644 --- a/api/server.go +++ b/api/server.go @@ -15,6 +15,7 @@ func NewServer(store db.Store) *Server{ router := gin.Default() router.POST("/accounts", server.createAccount) + router.GET("/accounts:id", server.getAccount) server.router = router return server } @@ -25,4 +26,4 @@ func errorResponse(err error) gin.H { func (server *Server) Start(address string) error{ return server.router.Run(address) -} \ No newline at end of file +}