refactor(posts): remove orphan row_to_post_list and use row_to_post_full for get_post_by_id

This commit is contained in:
xfy 2026-06-17 16:24:38 +08:00
parent 1eedab8f21
commit 9f41ea7354
2 changed files with 5 additions and 51 deletions

View File

@ -14,53 +14,6 @@ use crate::utils::text::count_words;
#[cfg(feature = "server")]
pub(super) use crate::api::auth::get_current_admin_user;
/// 将数据库行转换为文章列表项。
///
/// 用于列表接口,包含标签聚合、字数与阅读时长估算,
/// 不包含上下篇导航与目录。
#[cfg(feature = "server")]
pub(super) async fn row_to_post_list(
_client: &tokio_postgres::Client,
row: &tokio_postgres::Row,
) -> Post {
let id: i32 = row.get("id");
let role_str: String = row.get("status");
let status = PostStatus::from_str(&role_str).unwrap_or(PostStatus::Draft);
// 聚合标签并过滤空字符串。
let tags: Vec<String> = row
.try_get::<_, Vec<String>>("tags")
.unwrap_or_default()
.into_iter()
.filter(|t| !t.is_empty())
.collect();
let content_md: String = row.get("content_md");
let word_count = count_words(&content_md);
Post {
id,
author_id: row.get("author_id"),
title: row.get("title"),
slug: row.get("slug"),
summary: row.get("summary"),
content_md,
content_html: row.get("content_html"),
status,
published_at: row.get("published_at"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
deleted_at: row.try_get("deleted_at").ok(),
tags,
cover_image: row.get("cover_image"),
reading_time: (word_count / 200).max(1),
word_count,
toc_html: None,
prev_post: None,
next_post: None,
}
}
/// 将数据库行转换为轻量列表项 DTO。
///
/// 不包含 `content_md`/`content_html`;字数与阅读时长直接读取已持久化的列。

View File

@ -8,7 +8,7 @@
use dioxus::prelude::*;
#[cfg(feature = "server")]
use super::helpers::{get_current_admin_user, row_to_post_full, row_to_post_list};
use super::helpers::{get_current_admin_user, row_to_post_full};
use super::types::SinglePostResponse;
#[cfg(feature = "server")]
use crate::api::error::AppError;
@ -28,9 +28,10 @@ pub async fn get_post_by_id(post_id: i32) -> Result<SinglePostResponse, ServerFn
let row = client
.query_opt(
"SELECT
p.id, p.author_id, p.title, p.slug, p.summary, p.content_md, p.content_html,
"SELECT
p.id, p.author_id, p.title, p.slug, p.summary, p.content_md, p.content_html, p.toc_html,
p.status, p.published_at, p.created_at, p.updated_at, p.cover_image,
p.word_count, p.reading_time,
COALESCE(array_agg(t.name) FILTER (WHERE t.name IS NOT NULL), '{}') as tags
FROM posts p
LEFT JOIN post_tags pt ON p.id = pt.post_id
@ -43,7 +44,7 @@ pub async fn get_post_by_id(post_id: i32) -> Result<SinglePostResponse, ServerFn
.map_err(AppError::query)?;
let post = match row {
Some(row) => Some(row_to_post_list(&client, &row).await),
Some(row) => Some(row_to_post_full(&client, &row).await),
None => None,
};