feat: add mapping functions
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
|
|
||||||
user "tutorial.sqlc.dev/app/internal/author"
|
"tutorial.sqlc.dev/app/internal/author"
|
||||||
)
|
)
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
@@ -20,8 +20,8 @@ func run() error {
|
|||||||
defer conn.Close(ctx)
|
defer conn.Close(ctx)
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
userCtl := user.NewController(conn)
|
authorCtl := author.NewController(conn)
|
||||||
user.Register(r, &userCtl)
|
author.Register(r, &authorCtl)
|
||||||
|
|
||||||
r.Run(":8000")
|
r.Run(":8000")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package user
|
package author
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -23,7 +23,7 @@ func (u *Controller) ListAuthors(c *gin.Context) {
|
|||||||
c.AbortWithError(500, err)
|
c.AbortWithError(500, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, authors)
|
c.JSON(200, toAuthorSlice(authors))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Controller) GetAuthorByID(c *gin.Context) {
|
func (u *Controller) GetAuthorByID(c *gin.Context) {
|
||||||
@@ -39,5 +39,5 @@ func (u *Controller) GetAuthorByID(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, author)
|
c.JSON(200, toAuthor(author))
|
||||||
}
|
}
|
||||||
|
|||||||
21
internal/author/mapper.go
Normal file
21
internal/author/mapper.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package author
|
||||||
|
|
||||||
|
import "tutorial.sqlc.dev/app/internal/author/gen"
|
||||||
|
|
||||||
|
func toAuthor(a gen.Author) Author {
|
||||||
|
return Author{
|
||||||
|
a.ID, a.Name, a.Bio.String,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapSlice[K any, V any](conv func(K) V) func([]K) []V {
|
||||||
|
return func(inputs []K) []V {
|
||||||
|
output := make([]V, len(inputs))
|
||||||
|
for i, input := range inputs {
|
||||||
|
output[i] = conv(input)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var toAuthorSlice = mapSlice(toAuthor)
|
||||||
7
internal/author/model.go
Normal file
7
internal/author/model.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package author
|
||||||
|
|
||||||
|
type Author struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Bio string `json:"bio"`
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package user
|
package author
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user