234 lines
5.2 KiB
Go
234 lines
5.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: posts_queries.sql
|
|
|
|
package db_repo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createPost = `-- name: CreatePost :one
|
|
INSERT INTO public.posts
|
|
(post_id, blog_id, user_id, title, "content", created_at, updated_at)
|
|
VALUES($1, $2, $3, $4, $5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
RETURNING post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
`
|
|
|
|
type CreatePostParams struct {
|
|
PostID int64 `json:"post_id"`
|
|
BlogID pgtype.Int8 `json:"blog_id"`
|
|
UserID int64 `json:"user_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
Content pgtype.Text `json:"content"`
|
|
}
|
|
|
|
func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) (Post, error) {
|
|
row := q.db.QueryRow(ctx, createPost,
|
|
arg.PostID,
|
|
arg.BlogID,
|
|
arg.UserID,
|
|
arg.Title,
|
|
arg.Content,
|
|
)
|
|
var i Post
|
|
err := row.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.Content,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deletePostByPostId = `-- name: DeletePostByPostId :exec
|
|
DELETE FROM public.posts
|
|
WHERE post_id=$1
|
|
`
|
|
|
|
func (q *Queries) DeletePostByPostId(ctx context.Context, postID int64) error {
|
|
_, err := q.db.Exec(ctx, deletePostByPostId, postID)
|
|
return err
|
|
}
|
|
|
|
const getPostsByBlogId = `-- name: GetPostsByBlogId :many
|
|
SELECT post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
FROM public.posts posts
|
|
where posts.blog_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPostsByBlogId(ctx context.Context, blogID pgtype.Int8) ([]Post, error) {
|
|
rows, err := q.db.Query(ctx, getPostsByBlogId, blogID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Post
|
|
for rows.Next() {
|
|
var i Post
|
|
if err := rows.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.Content,
|
|
&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 getPostsByPostId = `-- name: GetPostsByPostId :one
|
|
SELECT post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
FROM public.posts posts
|
|
where posts.post_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPostsByPostId(ctx context.Context, postID int64) (Post, error) {
|
|
row := q.db.QueryRow(ctx, getPostsByPostId, postID)
|
|
var i Post
|
|
err := row.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.Content,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPostsByUserId = `-- name: GetPostsByUserId :many
|
|
SELECT post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
FROM public.posts posts
|
|
where posts.user_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPostsByUserId(ctx context.Context, userID int64) ([]Post, error) {
|
|
rows, err := q.db.Query(ctx, getPostsByUserId, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Post
|
|
for rows.Next() {
|
|
var i Post
|
|
if err := rows.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.Content,
|
|
&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 getRandomPosts = `-- name: GetRandomPosts :many
|
|
SELECT post_id, blog_id, user_id, title, created_at
|
|
FROM public.posts
|
|
ORDER BY RANDOM()
|
|
LIMIT $1
|
|
`
|
|
|
|
type GetRandomPostsRow struct {
|
|
PostID int64 `json:"post_id"`
|
|
BlogID pgtype.Int8 `json:"blog_id"`
|
|
UserID int64 `json:"user_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedAt pgtype.Timestamp `json:"created_at"`
|
|
}
|
|
|
|
func (q *Queries) GetRandomPosts(ctx context.Context, limit int32) ([]GetRandomPostsRow, error) {
|
|
rows, err := q.db.Query(ctx, getRandomPosts, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetRandomPostsRow
|
|
for rows.Next() {
|
|
var i GetRandomPostsRow
|
|
if err := rows.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updatePostBlogId = `-- name: UpdatePostBlogId :exec
|
|
UPDATE public.posts
|
|
SET blog_id=$2, updated_at=CURRENT_TIMESTAMP
|
|
WHERE post_id = $1
|
|
RETURNING post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
`
|
|
|
|
type UpdatePostBlogIdParams struct {
|
|
PostID int64 `json:"post_id"`
|
|
BlogID pgtype.Int8 `json:"blog_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdatePostBlogId(ctx context.Context, arg UpdatePostBlogIdParams) error {
|
|
_, err := q.db.Exec(ctx, updatePostBlogId, arg.PostID, arg.BlogID)
|
|
return err
|
|
}
|
|
|
|
const updatePostByPostId = `-- name: UpdatePostByPostId :one
|
|
UPDATE public.posts
|
|
SET title=$1, "content"=$2, updated_at=CURRENT_TIMESTAMP
|
|
WHERE post_id = $3
|
|
RETURNING post_id, blog_id, user_id, title, content, created_at, updated_at
|
|
`
|
|
|
|
type UpdatePostByPostIdParams struct {
|
|
Title pgtype.Text `json:"title"`
|
|
Content pgtype.Text `json:"content"`
|
|
PostID int64 `json:"post_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdatePostByPostId(ctx context.Context, arg UpdatePostByPostIdParams) (Post, error) {
|
|
row := q.db.QueryRow(ctx, updatePostByPostId, arg.Title, arg.Content, arg.PostID)
|
|
var i Post
|
|
err := row.Scan(
|
|
&i.PostID,
|
|
&i.BlogID,
|
|
&i.UserID,
|
|
&i.Title,
|
|
&i.Content,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|