本章将实现博客的分类文章列表功能。
模板
请参见代码仓库的templates/frontend/topic_list.html
视图类
请参见代码仓库的src/view/frontend/topic.rs
handler
// src/handler/frontend/topic.rs
pub async fn list(
Extension(state): Extension<Arc<AppState>>,
Path(id): Path<i32>,
Query(args): Query<Args>,
) -> Result<HtmlView> {
let page = args.page();
let handler_name = "frontend/topic/list";
let client = get_client(&state).await.map_err(log_error(handler_name))?;
let list = topic::list_by_cat(&client, page, id)
.await
.map_err(log_error(handler_name))?;
let cats = category::list(&client)
.await
.map_err(log_error(handler_name))?;
let archives = topic::archive_list(&client)
.await
.map_err(log_error(handler_name))?;
let cat = category::find(&client, id)
.await
.map_err(log_error(handler_name))?;
let tmpl = List {
list,
cats,
archives,
category_name: cat.name.clone(),
page,
};
render(tmpl).map_err(log_error(handler_name))
}
其中的 topic::list_by_cat()
方法,是通过分类ID来查询所有文章。详情见下文“数据库操作”部分。
数据库操作
以下代码位于
src/db/topic.rs
list_by_cat()
的定义如下:
pub async fn list_by_cat(client: &Client, page: u32, cat_id: i32) -> Result<Paginate<Vec<TopicList>>> {
list_by_condition(client, page, Some("category_id=$1"), Some(&[&cat_id])).await
}
它调用的是 list_by_condition()
:
async fn list_by_condition(client:&Client, page:u32, condition:Option<&str>, params:Option<&[&(dyn ToSql + Sync)]>) -> Result<Paginate<Vec<TopicList>>> {
let sql = "SELECT id,title,category_id,summary,hit,dateline,is_del,category_name FROM v_topic_cat_list WHERE is_del=false %CONDITION% ORDER BY id DESC ";
let condition = match condition {
Some(c) => format!(" AND {}", c),
None => "".to_string(),
};
let sql = sql.replace("%CONDITION%", &condition);
let sql=format!("{} LIMIT {} OFFSET {}",sql, DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE as u32 * page);
let count_sql = "SELECT COUNT(*) FROM v_topic_cat_list WHERE is_del=false %CONDITION%";
let count_sql = count_sql.replace("%CONDITION%", &condition);
let params = params.unwrap_or(&[]);
super::pagination(client, &sql, &count_sql, params, page).await
}
list_by_condition()
用于指定条件的查询,list_by_cat()
的条件是分类ID,而我们上一章使用的 list()
是无条件的,所以该函数也改用list_by_condition()
:
pub async fn list(client: &Client, page: u32) -> Result<Paginate<Vec<TopicList>>> {
list_by_condition(client, page, None, None).await
}
路由
请参见源码仓库的src/handler/frontend/mod.rs。
标签:axum,list,博客,列表,client,let,cat,page,condition From: https://www.cnblogs.com/pythonClub/p/17779463.html