259 lines
5.9 KiB
Go
259 lines
5.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.28.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 :one
|
|
WITH all_posts AS (
|
|
SELECT
|
|
COUNT(*) AS post_count
|
|
FROM
|
|
public.posts
|
|
),
|
|
filtered_posts AS (
|
|
SELECT
|
|
ARRAY(
|
|
SELECT
|
|
json_build_object(
|
|
'post_id', post_id::text, 'blog_id', blog_id::text,
|
|
'user_id', user_id::text, 'title', title,
|
|
'created_at', created_at, 'content', SUBSTRING(content FROM 1 FOR 300)
|
|
)
|
|
FROM
|
|
public.posts
|
|
ORDER BY
|
|
created_at DESC
|
|
LIMIT $1 OFFSET $3
|
|
) as selected_posts
|
|
)
|
|
SELECT
|
|
fp.selected_posts,
|
|
(ap.post_count - ($2 + 1) * $1)::int > 0 as has_next_page,
|
|
case
|
|
when (ap.post_count - ($2 + 1) * $1)::int > 0
|
|
then $2 + 1
|
|
else -1
|
|
end as next_page_index,
|
|
case
|
|
when (ap.post_count - ( $2 + 1 ) * $1 + 1 * $1)::int <= ap.post_count
|
|
then $2 - 1
|
|
else -1
|
|
end as prev_page_index
|
|
FROM
|
|
filtered_posts fp,
|
|
all_posts ap
|
|
`
|
|
|
|
type GetRandomPostsParams struct {
|
|
Column1 interface{} `json:"column_1"`
|
|
Column2 interface{} `json:"column_2"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
|
|
type GetRandomPostsRow struct {
|
|
SelectedPosts interface{} `json:"selected_posts"`
|
|
HasNextPage bool `json:"has_next_page"`
|
|
NextPageIndex int32 `json:"next_page_index"`
|
|
PrevPageIndex int32 `json:"prev_page_index"`
|
|
}
|
|
|
|
func (q *Queries) GetRandomPosts(ctx context.Context, arg GetRandomPostsParams) (GetRandomPostsRow, error) {
|
|
row := q.db.QueryRow(ctx, getRandomPosts, arg.Column1, arg.Column2, arg.Offset)
|
|
var i GetRandomPostsRow
|
|
err := row.Scan(
|
|
&i.SelectedPosts,
|
|
&i.HasNextPage,
|
|
&i.NextPageIndex,
|
|
&i.PrevPageIndex,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
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
|
|
}
|