分类文章列表

543567
2022/03/27 00:03:58

模板

请参见代码仓库的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()

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