feat: move db gen out, emit json field

This commit is contained in:
Justin Lin
2025-08-26 20:58:39 +10:00
parent e770b31402
commit 7ced3985b1
15 changed files with 63 additions and 90 deletions

View File

@@ -4,26 +4,26 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"tutorial.sqlc.dev/app/internal/author/gen"
"tutorial.sqlc.dev/app/internal/db"
)
type Controller struct {
queries gen.Queries
q db.Querier
}
func NewController(conn gen.DBTX) Controller {
func NewController(queries db.Querier) Controller {
return Controller{
queries: *gen.New(conn),
q: queries,
}
}
func (u *Controller) ListAuthors(c *gin.Context) {
authors, err := u.queries.ListAuthors(c.Request.Context())
authors, err := u.q.ListAuthors(c.Request.Context())
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, toAuthorSlice(authors))
c.JSON(200, authors)
}
func (u *Controller) GetAuthorByID(c *gin.Context) {
@@ -33,11 +33,11 @@ func (u *Controller) GetAuthorByID(c *gin.Context) {
c.AbortWithError(400, err)
return
}
author, err := u.queries.GetAuthor(c.Request.Context(), int64(id))
author, err := u.q.GetAuthor(c.Request.Context(), int64(id))
if err != nil {
c.JSON(400, gin.H{"message": "Author not found"})
return
}
c.JSON(200, toAuthor(author))
c.JSON(200, author)
}