190 lines
4.1 KiB
Go
190 lines
4.1 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: categories.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
const countCategories = `-- name: CountCategories :one
|
|
SELECT COUNT(*)
|
|
FROM categories
|
|
`
|
|
|
|
func (q *Queries) CountCategories(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countCategories)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createCategory = `-- name: CreateCategory :exec
|
|
INSERT INTO categories(name, code, sort)
|
|
VALUES ($1, $2, $3)
|
|
`
|
|
|
|
type CreateCategoryParams struct {
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
Sort *int32 `json:"sort"`
|
|
}
|
|
|
|
func (q *Queries) CreateCategory(ctx context.Context, arg CreateCategoryParams) error {
|
|
_, err := q.db.Exec(ctx, createCategory, arg.Name, arg.Code, arg.Sort)
|
|
return err
|
|
}
|
|
|
|
const createPostCategory = `-- name: CreatePostCategory :exec
|
|
INSERT INTO post_category(post_id, category_id)
|
|
VALUES ($1, $2)
|
|
`
|
|
|
|
type CreatePostCategoryParams struct {
|
|
PostID int32 `json:"post_id"`
|
|
CategoryID int32 `json:"category_id"`
|
|
}
|
|
|
|
func (q *Queries) CreatePostCategory(ctx context.Context, arg CreatePostCategoryParams) error {
|
|
_, err := q.db.Exec(ctx, createPostCategory, arg.PostID, arg.CategoryID)
|
|
return err
|
|
}
|
|
|
|
const deleteCategory = `-- name: DeleteCategory :execrows
|
|
DELETE
|
|
FROM categories
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteCategory(ctx context.Context, id int32) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteCategory, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deletePostCategoryByCategoryID = `-- name: DeletePostCategoryByCategoryID :exec
|
|
DELETE
|
|
FROM post_category
|
|
WHERE category_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeletePostCategoryByCategoryID(ctx context.Context, categoryID int32) error {
|
|
_, err := q.db.Exec(ctx, deletePostCategoryByCategoryID, categoryID)
|
|
return err
|
|
}
|
|
|
|
const deletePostCategoryByPostID = `-- name: DeletePostCategoryByPostID :exec
|
|
DELETE
|
|
FROM post_category
|
|
WHERE post_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeletePostCategoryByPostID(ctx context.Context, postID int32) error {
|
|
_, err := q.db.Exec(ctx, deletePostCategoryByPostID, postID)
|
|
return err
|
|
}
|
|
|
|
const listAllCategories = `-- name: ListAllCategories :many
|
|
SELECT id, name, code, sort, created_at, updated_at
|
|
FROM categories
|
|
ORDER BY sort DESC, id
|
|
`
|
|
|
|
func (q *Queries) ListAllCategories(ctx context.Context) ([]Category, error) {
|
|
rows, err := q.db.Query(ctx, listAllCategories)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Category{}
|
|
for rows.Next() {
|
|
var i Category
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Code,
|
|
&i.Sort,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listCategories = `-- name: ListCategories :many
|
|
SELECT id, name, code, sort, created_at, updated_at
|
|
FROM categories
|
|
ORDER BY sort DESC, id
|
|
LIMIT $1 OFFSET $2
|
|
`
|
|
|
|
type ListCategoriesParams struct {
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
|
|
func (q *Queries) ListCategories(ctx context.Context, arg ListCategoriesParams) ([]Category, error) {
|
|
rows, err := q.db.Query(ctx, listCategories, arg.Limit, arg.Offset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Category{}
|
|
for rows.Next() {
|
|
var i Category
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Code,
|
|
&i.Sort,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateCategory = `-- name: UpdateCategory :execrows
|
|
UPDATE categories
|
|
SET name = coalesce($1, name),
|
|
code = coalesce($2, code),
|
|
sort = coalesce($3, sort)
|
|
WHERE id = $4
|
|
`
|
|
|
|
type UpdateCategoryParams struct {
|
|
Name *string `json:"name"`
|
|
Code *string `json:"code"`
|
|
Sort *int32 `json:"sort"`
|
|
ID int32 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateCategory(ctx context.Context, arg UpdateCategoryParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateCategory,
|
|
arg.Name,
|
|
arg.Code,
|
|
arg.Sort,
|
|
arg.ID,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|