-- name: CountPostsOfSite :one
SELECT COUNT(*) FROM posts
WHERE site_id = sqlc.arg(site_id) AND (
    CASE CAST (sqlc.arg(post_filter) AS TEXT)
        WHEN 'deleted' THEN deleted_at > 0
        ELSE deleted_at = 0
    END
);

-- name: SelectPostsOfSite :many
SELECT *
FROM posts
WHERE site_id = sqlc.arg(site_id) AND (
    CASE CAST (sqlc.arg(post_filter) AS TEXT)
        WHEN 'deleted' THEN deleted_at > 0
        ELSE deleted_at = 0
    END
) ORDER BY created_at DESC LIMIT sqlc.arg(limit) OFFSET sqlc.arg(offset);

-- name: SelectPost :one
SELECT * FROM posts WHERE id = ? LIMIT 1;

-- name: SelectPostByGUID :one
SELECT * FROM posts WHERE guid = ? LIMIT 1;

-- name: InsertPost :one
INSERT INTO posts (
    site_id,
    state,
    guid,
    title,
    body,
    slug,
    created_at,
    updated_at,
    published_at,
    deleted_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id;

-- name: UpdatePost :exec
UPDATE posts SET
    title = ?,
    state = ?,
    body = ?,
    slug = ?,
    updated_at = ?,
    published_at = ?,
    deleted_at = ?
WHERE id = ?;

-- name: SoftDeletePost :exec
UPDATE posts SET deleted_at = ? WHERE id = ?;

-- name: RestorePost :exec
UPDATE posts SET deleted_at = 0 WHERE id = ?;

-- name: HardDeletePost :exec
DELETE FROM posts WHERE id = ?;