start stubbing out endpoints
This commit is contained in:
@@ -131,35 +131,41 @@ class Topics(Model):
|
||||
order_clause = 'ORDER BY threads.is_stickied DESC, latest_post_created_at DESC'
|
||||
|
||||
q = """
|
||||
SELECT
|
||||
threads.id, threads.title, threads.slug, threads.created_at, threads.is_locked, threads.is_stickied,
|
||||
users.username AS started_by,
|
||||
users.display_name AS started_by_display_name,
|
||||
u.username AS latest_post_username,
|
||||
u.display_name AS latest_post_display_name,
|
||||
ph.content AS latest_post_content,
|
||||
posts.created_at AS latest_post_created_at,
|
||||
posts.id AS latest_post_id
|
||||
FROM
|
||||
threads
|
||||
JOIN users ON users.id = threads.user_id
|
||||
JOIN (
|
||||
WITH latest_posts AS (
|
||||
SELECT
|
||||
posts.thread_id,
|
||||
posts.id,
|
||||
posts.user_id,
|
||||
posts.created_at,
|
||||
posts.current_revision_id,
|
||||
ROW_NUMBER() OVER (PARTITION BY posts.thread_id ORDER BY posts.created_at DESC) AS rn
|
||||
FROM
|
||||
posts
|
||||
) posts ON posts.thread_id = threads.id AND posts.rn = 1
|
||||
JOIN
|
||||
post_history ph ON ph.id = posts.current_revision_id
|
||||
JOIN
|
||||
users u ON u.id = posts.user_id
|
||||
WHERE
|
||||
threads.topic_id = ?
|
||||
thread_id,
|
||||
id AS latest_post_id,
|
||||
user_id AS latest_post_user_id,
|
||||
created_at AS latest_post_created_at,
|
||||
ROW_NUMBER() OVER (PARTITION BY thread_id ORDER BY created_at DESC) AS rn
|
||||
FROM posts
|
||||
),
|
||||
post_counts AS (
|
||||
SELECT
|
||||
thread_id,
|
||||
COUNT(*) AS posts_count
|
||||
FROM posts
|
||||
GROUP BY thread_id
|
||||
)
|
||||
SELECT
|
||||
threads.title,
|
||||
threads.slug,
|
||||
threads.created_at,
|
||||
threads.is_locked,
|
||||
threads.is_stickied,
|
||||
starter.username AS started_by,
|
||||
starter.display_name AS started_by_display_name,
|
||||
latest_poster.username AS latest_post_username,
|
||||
latest_poster.display_name AS latest_post_display_name,
|
||||
latest_posts.latest_post_created_at,
|
||||
latest_posts.latest_post_id,
|
||||
COALESCE(post_counts.posts_count, 0) AS posts_count
|
||||
FROM threads
|
||||
JOIN users AS starter ON starter.id = threads.user_id
|
||||
LEFT JOIN latest_posts ON latest_posts.thread_id = threads.id AND latest_posts.rn = 1
|
||||
LEFT JOIN users AS latest_poster ON latest_poster.id = latest_posts.latest_post_user_id
|
||||
LEFT JOIN post_counts ON post_counts.thread_id = threads.id
|
||||
WHERE threads.topic_id = ?
|
||||
""" + order_clause + ' LIMIT ? OFFSET ?'
|
||||
|
||||
return db.query(q, self.id, per_page, (page - 1) * per_page)
|
||||
|
||||
Reference in New Issue
Block a user