chore: initial commit

This commit is contained in:
2026-07-22 17:50:33 +08:00
parent 82936a8987
commit ee9f50b859
107 changed files with 8346 additions and 0 deletions

View File

@@ -0,0 +1,186 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: category.sql
package db
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)
VALUES ($1, $2)
`
type CreateCategoryParams struct {
Name string `json:"name"`
Code string `json:"code"`
}
func (q *Queries) CreateCategory(ctx context.Context, arg CreateCategoryParams) error {
_, err := q.db.Exec(ctx, createCategory, arg.Name, arg.Code)
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 deletePostCategory = `-- name: DeletePostCategory :exec
DELETE
FROM post_category
WHERE post_id = $1
`
func (q *Queries) DeletePostCategory(ctx context.Context, postID int32) error {
_, err := q.db.Exec(ctx, deletePostCategory, postID)
return err
}
const getCategoryById = `-- name: GetCategoryById :one
SELECT id, name, code, created_at, updated_at
FROM categories
WHERE id = $1
`
func (q *Queries) GetCategoryById(ctx context.Context, id int32) (Category, error) {
row := q.db.QueryRow(ctx, getCategoryById, id)
var i Category
err := row.Scan(
&i.ID,
&i.Name,
&i.Code,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const listAllCategories = `-- name: ListAllCategories :many
SELECT id, name, code, created_at, updated_at
FROM categories
`
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.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, created_at, updated_at
FROM categories
ORDER BY 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.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)
WHERE id = $3
`
type UpdateCategoryParams struct {
Name *string `json:"name"`
Code *string `json:"code"`
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.ID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}