@@ -22,6 +22,18 @@ type Post struct {
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
type PostImage struct {
|
||||
ID string
|
||||
PostID string
|
||||
ObjectKey string
|
||||
PublicUrl string
|
||||
Description string
|
||||
Position int16
|
||||
Width int32
|
||||
Height int32
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type PostVote struct {
|
||||
UserID string
|
||||
PostID string
|
||||
|
||||
@@ -57,6 +57,60 @@ func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const createPostImage = `-- name: CreatePostImage :exec
|
||||
INSERT INTO post_images (
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9
|
||||
)
|
||||
`
|
||||
|
||||
type CreatePostImageParams struct {
|
||||
ID string
|
||||
PostID string
|
||||
ObjectKey string
|
||||
PublicUrl string
|
||||
Description string
|
||||
Position int16
|
||||
Width int32
|
||||
Height int32
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
func (q *Queries) CreatePostImage(ctx context.Context, arg CreatePostImageParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createPostImage,
|
||||
arg.ID,
|
||||
arg.PostID,
|
||||
arg.ObjectKey,
|
||||
arg.PublicUrl,
|
||||
arg.Description,
|
||||
arg.Position,
|
||||
arg.Width,
|
||||
arg.Height,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deletePostImages = `-- name: DeletePostImages :exec
|
||||
DELETE FROM post_images
|
||||
WHERE post_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePostImages(ctx context.Context, postID string) error {
|
||||
_, err := q.db.ExecContext(ctx, deletePostImages, postID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deletePostVote = `-- name: DeletePostVote :exec
|
||||
DELETE FROM post_votes
|
||||
WHERE user_id = $1
|
||||
@@ -145,6 +199,47 @@ func (q *Queries) GetRootPostVoteSummary(ctx context.Context, arg GetRootPostVot
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listPostImages = `-- name: ListPostImages :many
|
||||
SELECT
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
FROM post_images
|
||||
WHERE post_id = $1
|
||||
ORDER BY position
|
||||
`
|
||||
|
||||
func (q *Queries) ListPostImages(ctx context.Context, postID string) ([]PostImage, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listPostImages, postID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []PostImage{}
|
||||
for rows.Next() {
|
||||
var i PostImage
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PostID,
|
||||
&i.ObjectKey,
|
||||
&i.PublicUrl,
|
||||
&i.Description,
|
||||
&i.Position,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listPostThread = `-- name: ListPostThread :many
|
||||
WITH RECURSIVE thread AS (
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||
@@ -218,6 +313,59 @@ func (q *Queries) ListPostThread(ctx context.Context, rootID string) ([]ListPost
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listPostThreadImages = `-- name: ListPostThreadImages :many
|
||||
WITH RECURSIVE thread AS (
|
||||
SELECT p.id
|
||||
FROM posts p
|
||||
WHERE p.id = $1 AND p.parent_id IS NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT child.id
|
||||
FROM posts child
|
||||
JOIN thread parent ON child.parent_id = parent.id
|
||||
)
|
||||
SELECT
|
||||
images.id, images.post_id, images.object_key, images.public_url,
|
||||
images.description, images.position, images.width, images.height, images.created_at
|
||||
FROM post_images images
|
||||
JOIN thread ON thread.id = images.post_id
|
||||
ORDER BY images.post_id, images.position
|
||||
`
|
||||
|
||||
func (q *Queries) ListPostThreadImages(ctx context.Context, rootID string) ([]PostImage, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listPostThreadImages, rootID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []PostImage{}
|
||||
for rows.Next() {
|
||||
var i PostImage
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PostID,
|
||||
&i.ObjectKey,
|
||||
&i.PublicUrl,
|
||||
&i.Description,
|
||||
&i.Position,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listRootPosts = `-- name: ListRootPosts :many
|
||||
WITH RECURSIVE roots AS (
|
||||
SELECT p.id, p.parent_id, p.author_id, p.title, p.body, p.city, p.post_date, p.post_state, p.created_at, p.updated_at
|
||||
|
||||
Reference in New Issue
Block a user