-- name: CreateQuestion :exec INSERT INTO questions (id, author_id, title, body, city, hunt_date, hidden, created_at) VALUES ($1, $2, $3, $4, $5, $6, 0, $7); -- name: HideQuestion :exec UPDATE questions SET hidden = 1 WHERE id = $1; -- name: ListHunt :many SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, COALESCE(SUM(v.value), 0)::bigint AS score, CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered, COALESCE(( SELECT votes.value FROM votes WHERE votes.user_id = sqlc.arg(viewer_id) AND votes.question_id = q.id ), 0)::bigint AS user_vote FROM questions q JOIN users u ON u.id = q.author_id LEFT JOIN votes v ON v.question_id = q.id LEFT JOIN answers a ON a.question_id = q.id WHERE q.hunt_date = sqlc.arg(hunt_date) AND q.hidden = 0 GROUP BY q.id, q.author_id, u.name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, a.question_id ORDER BY score DESC, q.created_at ASC; -- name: GetQuestion :one SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score, CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered, COALESCE(( SELECT votes.value FROM votes WHERE votes.user_id = sqlc.arg(viewer_id) AND votes.question_id = q.id ), 0)::bigint AS user_vote FROM questions q JOIN users u ON u.id = q.author_id LEFT JOIN answers a ON a.question_id = q.id WHERE q.id = sqlc.arg(id); -- name: ListQuestionsByAuthor :many SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score, CASE WHEN a.question_id IS NULL THEN 0 ELSE 1 END::bigint AS answered, 0::bigint AS user_vote FROM questions q JOIN users u ON u.id = q.author_id LEFT JOIN answers a ON a.question_id = q.id WHERE q.author_id = sqlc.arg(author_id) AND q.hidden = 0 ORDER BY q.created_at DESC; -- name: ListQuestionsAnsweredBy :many SELECT q.id, q.author_id, u.name AS author_name, q.title, q.body, q.city, q.hunt_date, q.hidden, q.created_at, COALESCE((SELECT SUM(votes.value) FROM votes WHERE votes.question_id = q.id), 0)::bigint AS score, 1::bigint AS answered, 0::bigint AS user_vote FROM answers ans JOIN questions q ON q.id = ans.question_id JOIN users u ON u.id = q.author_id WHERE ans.author_id = sqlc.arg(admin_id) AND q.hidden = 0 ORDER BY ans.updated_at DESC;