-- name: SelectStory :one
SELECT * FROM story WHERE id = $1 LIMIT 1;

-- name: SelectStories :many
SELECT * FROM story
WHERE (@filter_id::bool = false OR id = ANY(@ids::text[]))
  AND (@filter_author::bool = false OR name = @author::text)
  AND (@filter_earlist_fictional_date::bool = false OR fictional_date >= @earliest_fictional_date::timestamp)
  AND (@filter_lastest_fictional_date::bool = false OR fictional_date <= @latest_fictional_date::timestamp)
  AND (@filter_category::bool = false OR category = @category::text)
  AND (@filter_open::bool = false OR open = @open::bool)
  AND (@filter_unlisted::bool = false OR unlisted = @unlisted::bool)
ORDER BY updated_date
LIMIT NULLIF(@limit_size::INT, 0);

-- name: InsertStory :exec
INSERT INTO story (id, author, name, category, open, listed, sort_by_fictional_date, created_date, fictional_date, updated_date)
VALUES (
    @id::text, @author::text, @name::text, @category::text,
    @open::boolean, @listed::boolean, @sort_by_fictional_date::boolean,
    @created_date::timestamp, @fictional_date::timestamp, @updated_date::timestamp
);

-- name: UpdateStory :exec
UPDATE story
SET name = @name,
    category = @category,
    author = @author,
    open = @open,
    listed = @listed,
    fictional_date = @fictional_date,
    updated_date = @updated_date,
    sort_by_fictional_date = @sort_by_fictional_date
WHERE id = @id;

-- name: DeleteStory :exec
DELETE FROM story WHERE id=$1;