diff --git a/app b/app deleted file mode 100755 index 4a0a09c..0000000 Binary files a/app and /dev/null differ diff --git a/cmd/api/main.go b/cmd/api/main.go index f56f25e..e2ef6ee 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -8,6 +8,7 @@ import ( "github.com/jackc/pgx/v5" "tutorial.sqlc.dev/app/internal/author" + "tutorial.sqlc.dev/app/internal/db" ) func run() error { @@ -19,8 +20,9 @@ func run() error { } defer conn.Close(ctx) + querier := db.New(conn) r := gin.Default() - authorCtl := author.NewController(conn) + authorCtl := author.NewController(querier) author.Register(r, &authorCtl) r.Run(":8000") diff --git a/internal/author/query.sql b/db/query.sql similarity index 100% rename from internal/author/query.sql rename to db/query.sql diff --git a/internal/author/schema.sql b/db/schema.sql similarity index 100% rename from internal/author/schema.sql rename to db/schema.sql diff --git a/internal/author/controller.go b/internal/author/controller.go index 0333f9a..86b12f8 100644 --- a/internal/author/controller.go +++ b/internal/author/controller.go @@ -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) } diff --git a/internal/author/gen/models.go b/internal/author/gen/models.go deleted file mode 100644 index b8bcbb3..0000000 --- a/internal/author/gen/models.go +++ /dev/null @@ -1,15 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.29.0 - -package gen - -import ( - "github.com/jackc/pgx/v5/pgtype" -) - -type Author struct { - ID int64 - Name string - Bio pgtype.Text -} diff --git a/internal/author/mapper.go b/internal/author/mapper.go index 83c7fc4..9a99912 100644 --- a/internal/author/mapper.go +++ b/internal/author/mapper.go @@ -1,10 +1,12 @@ package author -import "tutorial.sqlc.dev/app/internal/author/gen" +import ( + "tutorial.sqlc.dev/app/internal/db" +) -func toAuthor(a gen.Author) Author { +func toAuthor(a db.Author) Author { return Author{ - a.ID, a.Name, a.Bio.String, + a.ID, a.Name, a.Bio, } } diff --git a/internal/author/model.go b/internal/author/model.go index d579ede..6e8f4ca 100644 --- a/internal/author/model.go +++ b/internal/author/model.go @@ -1,7 +1,7 @@ package author type Author struct { - ID int64 `json:"id"` - Name string `json:"name"` - Bio string `json:"bio"` + ID int64 `json:"id"` + Name string `json:"name"` + Bio *string `json:"bio"` } diff --git a/internal/author/gen/db.go b/internal/db/db.go similarity index 97% rename from internal/author/gen/db.go rename to internal/db/db.go index 5017afe..0e4c3f4 100644 --- a/internal/author/gen/db.go +++ b/internal/db/db.go @@ -2,7 +2,7 @@ // versions: // sqlc v1.29.0 -package gen +package db import ( "context" diff --git a/internal/db/models.go b/internal/db/models.go new file mode 100644 index 0000000..c90a57f --- /dev/null +++ b/internal/db/models.go @@ -0,0 +1,11 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.29.0 + +package db + +type Author struct { + ID int64 `json:"id"` + Name string `json:"name"` + Bio *string `json:"bio"` +} diff --git a/internal/db/querier.go b/internal/db/querier.go new file mode 100644 index 0000000..b569be0 --- /dev/null +++ b/internal/db/querier.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.29.0 + +package db + +import ( + "context" +) + +type Querier interface { + CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) + DeleteAuthor(ctx context.Context, id int64) error + GetAuthor(ctx context.Context, id int64) (Author, error) + ListAuthors(ctx context.Context) ([]Author, error) + UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error +} + +var _ Querier = (*Queries)(nil) diff --git a/internal/author/gen/query.sql.go b/internal/db/query.sql.go similarity index 92% rename from internal/author/gen/query.sql.go rename to internal/db/query.sql.go index 5d04ce3..0221067 100644 --- a/internal/author/gen/query.sql.go +++ b/internal/db/query.sql.go @@ -3,12 +3,10 @@ // sqlc v1.29.0 // source: query.sql -package gen +package db import ( "context" - - "github.com/jackc/pgx/v5/pgtype" ) const createAuthor = `-- name: CreateAuthor :one @@ -21,8 +19,8 @@ RETURNING id, name, bio ` type CreateAuthorParams struct { - Name string - Bio pgtype.Text + Name string `json:"name"` + Bio *string `json:"bio"` } func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) { @@ -88,9 +86,9 @@ RETURNING id, name, bio ` type UpdateAuthorParams struct { - ID int64 - Name string - Bio pgtype.Text + ID int64 `json:"id"` + Name string `json:"name"` + Bio *string `json:"bio"` } func (q *Queries) UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error { diff --git a/sqlc.yaml b/sqlc.yaml index cc12fba..5c35d2e 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -1,10 +1,13 @@ version: "2" sql: - engine: "postgresql" - queries: "internal/author/query.sql" - schema: "internal/author/schema.sql" + queries: "db/query.sql" + schema: "db/schema.sql" gen: go: - package: "gen" - out: "internal/author/gen" + package: "db" + out: "internal/db" sql_package: "pgx/v5" + emit_json_tags: true + emit_interface: true + emit_pointers_for_null_types: true diff --git a/tutorial/db.go b/tutorial/db.go deleted file mode 100644 index 5f69f0f..0000000 --- a/tutorial/db.go +++ /dev/null @@ -1,32 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.29.0 - -package tutorial - -import ( - "context" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgconn" -) - -type DBTX interface { - Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) - Query(context.Context, string, ...interface{}) (pgx.Rows, error) - QueryRow(context.Context, string, ...interface{}) pgx.Row -} - -func New(db DBTX) *Queries { - return &Queries{db: db} -} - -type Queries struct { - db DBTX -} - -func (q *Queries) WithTx(tx pgx.Tx) *Queries { - return &Queries{ - db: tx, - } -} diff --git a/tutorial/models.go b/tutorial/models.go deleted file mode 100644 index bac90c3..0000000 --- a/tutorial/models.go +++ /dev/null @@ -1,15 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.29.0 - -package tutorial - -import ( - "github.com/jackc/pgx/v5/pgtype" -) - -type Author struct { - ID int64 - Name string - Bio pgtype.Text -}