feat: add getAuthorByID
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tutorial.sqlc.dev/app/internal/author/gen"
|
||||
@@ -17,10 +17,27 @@ func NewController(conn gen.DBTX) Controller {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Controller) ListAuthors(ctx *gin.Context) {
|
||||
authors, err := u.queries.ListAuthors(context.Background())
|
||||
func (u *Controller) ListAuthors(c *gin.Context) {
|
||||
authors, err := u.queries.ListAuthors(c.Request.Context())
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, authors)
|
||||
c.JSON(200, authors)
|
||||
}
|
||||
|
||||
func (u *Controller) GetAuthorByID(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
c.AbortWithError(400, err)
|
||||
return
|
||||
}
|
||||
author, err := u.queries.GetAuthor(c.Request.Context(), int64(id))
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"message": "Author not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, author)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user