Skip to content

Commit

Permalink
added getAccount handler
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
1Shubham7 authored Sep 16, 2024
1 parent 155c398 commit 05623f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions api/accounts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"database/sql"
"net/http"

db "github.com/1shubham7/bank/db/sqlc"
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -25,4 +26,4 @@ func errorResponse(err error) gin.H {

func (server *Server) Start(address string) error{
return server.router.Run(address)
}
}

0 comments on commit 05623f9

Please sign in to comment.