coryd.dev/queries/functions/get_feed_data.psql

27 lines
1.2 KiB
Text

CREATE OR REPLACE FUNCTION get_feed_data(feed_key TEXT)
RETURNS JSON AS $$
DECLARE
result JSON;
sql_query TEXT;
BEGIN
CASE feed_key
WHEN 'movies' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_movies WHERE feed IS NOT NULL';
WHEN 'books' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_books WHERE feed IS NOT NULL';
WHEN 'posts' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_posts WHERE feed IS NOT NULL';
WHEN 'links' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_links WHERE feed IS NOT NULL';
WHEN 'allActivity' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_all_activity WHERE feed IS NOT NULL';
WHEN 'syndication' THEN
sql_query := 'SELECT json_agg(feed ORDER BY (feed->>''date'')::timestamp DESC) FROM optimized_syndication WHERE feed IS NOT NULL';
ELSE
RETURN NULL;
END CASE;
EXECUTE sql_query INTO result;
RETURN result;
END;
$$ LANGUAGE plpgsql;