feat: add feature based structure
This commit is contained in:
26
internal/author/controller.go
Normal file
26
internal/author/controller.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"tutorial.sqlc.dev/app/internal/author/gen"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
queries gen.Queries
|
||||
}
|
||||
|
||||
func NewController(conn gen.DBTX) Controller {
|
||||
return Controller{
|
||||
queries: *gen.New(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Controller) ListAuthors(ctx *gin.Context) {
|
||||
authors, err := u.queries.ListAuthors(context.Background())
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
ctx.JSON(200, authors)
|
||||
}
|
||||
32
internal/author/gen/db.go
Normal file
32
internal/author/gen/db.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
|
||||
package gen
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
15
internal/author/gen/models.go
Normal file
15
internal/author/gen/models.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
}
|
||||
99
internal/author/gen/query.sql.go
Normal file
99
internal/author/gen/query.sql.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.29.0
|
||||
// source: query.sql
|
||||
|
||||
package gen
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createAuthor = `-- name: CreateAuthor :one
|
||||
INSERT INTO authors (
|
||||
name, bio
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING id, name, bio
|
||||
`
|
||||
|
||||
type CreateAuthorParams struct {
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAuthor(ctx context.Context, arg CreateAuthorParams) (Author, error) {
|
||||
row := q.db.QueryRow(ctx, createAuthor, arg.Name, arg.Bio)
|
||||
var i Author
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Bio)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteAuthor = `-- name: DeleteAuthor :exec
|
||||
DELETE FROM authors
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteAuthor(ctx context.Context, id int64) error {
|
||||
_, err := q.db.Exec(ctx, deleteAuthor, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAuthor = `-- name: GetAuthor :one
|
||||
SELECT id, name, bio FROM authors
|
||||
WHERE id = $1 LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) {
|
||||
row := q.db.QueryRow(ctx, getAuthor, id)
|
||||
var i Author
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Bio)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAuthors = `-- name: ListAuthors :many
|
||||
SELECT id, name, bio FROM authors
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListAuthors(ctx context.Context) ([]Author, error) {
|
||||
rows, err := q.db.Query(ctx, listAuthors)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Author
|
||||
for rows.Next() {
|
||||
var i Author
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateAuthor = `-- name: UpdateAuthor :exec
|
||||
UPDATE authors
|
||||
set name = $2,
|
||||
bio = $3
|
||||
WHERE id = $1
|
||||
RETURNING id, name, bio
|
||||
`
|
||||
|
||||
type UpdateAuthorParams struct {
|
||||
ID int64
|
||||
Name string
|
||||
Bio pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateAuthor(ctx context.Context, arg UpdateAuthorParams) error {
|
||||
_, err := q.db.Exec(ctx, updateAuthor, arg.ID, arg.Name, arg.Bio)
|
||||
return err
|
||||
}
|
||||
26
internal/author/query.sql
Normal file
26
internal/author/query.sql
Normal file
@@ -0,0 +1,26 @@
|
||||
-- name: GetAuthor :one
|
||||
SELECT * FROM authors
|
||||
WHERE id = $1 LIMIT 1;
|
||||
|
||||
-- name: ListAuthors :many
|
||||
SELECT * FROM authors
|
||||
ORDER BY name;
|
||||
|
||||
-- name: CreateAuthor :one
|
||||
INSERT INTO authors (
|
||||
name, bio
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateAuthor :exec
|
||||
UPDATE authors
|
||||
set name = $2,
|
||||
bio = $3
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteAuthor :exec
|
||||
DELETE FROM authors
|
||||
WHERE id = $1;
|
||||
8
internal/author/router.go
Normal file
8
internal/author/router.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package user
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Register(r gin.IRouter, ctl *Controller) {
|
||||
g := r.Group("/authors")
|
||||
g.GET("", ctl.ListAuthors)
|
||||
}
|
||||
5
internal/author/schema.sql
Normal file
5
internal/author/schema.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE authors (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
bio text
|
||||
);
|
||||
Reference in New Issue
Block a user