完成文章的展示以及新增
This commit is contained in:
parent
46dedb4306
commit
adf1d94c29
|
@ -1,11 +1,14 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::extract::{Query, State};
|
use axum::extract::{Query, State};
|
||||||
use sea_orm::{ColumnTrait, Condition, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect};
|
use axum::Form;
|
||||||
|
use sea_orm::{ActiveModelTrait, ColumnTrait, Condition, EntityTrait, NotSet, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect};
|
||||||
|
use sea_orm::ActiveValue::Set;
|
||||||
|
|
||||||
use crate::entity::{article, category};
|
use crate::entity::{article, category};
|
||||||
use crate::err::AppError;
|
use crate::err::AppError;
|
||||||
use crate::handler::{get_conn, HtmlResponse, log_error, render};
|
use crate::form::ArticleForm;
|
||||||
|
use crate::handler::{get_conn, HtmlResponse, log_error, redirect, RedirectResponse, render};
|
||||||
use crate::param::ArticleParams;
|
use crate::param::ArticleParams;
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
@ -19,26 +22,31 @@ pub async fn index(
|
||||||
let conn = get_conn(&state);
|
let conn = get_conn(&state);
|
||||||
|
|
||||||
// 构建条件: 所有未被删除的文章
|
// 构建条件: 所有未被删除的文章
|
||||||
let condition = Condition::all().add(article::Column::IsDel.eq(false));
|
let condition = Condition::all()
|
||||||
let select = article::Entity::find().filter(condition);
|
.add(article::Column::IsDel.eq(false))
|
||||||
let record_total = select
|
.add_option(
|
||||||
.clone()
|
params.keyword_opt().map(|n| article::Column::Title.contains(&n))
|
||||||
.count(conn)
|
)
|
||||||
.await
|
.add_option(params.is_del_opt().map(|n| article::Column::IsDel.eq(n)));
|
||||||
.map_err(AppError::from)
|
|
||||||
.map_err(log_error(handler_name))?;
|
|
||||||
|
|
||||||
let page_size = 15u64;
|
let mut select = article::Entity::find()
|
||||||
let page = 0u64;
|
.filter(condition);
|
||||||
|
|
||||||
let page_total = f64::ceil(record_total as f64 / page_size as f64) as u64;
|
let page = params.page(); // 当前页码
|
||||||
let offset = page_size * page;
|
let page_size = params.page_size(); // 每页条数,默认15
|
||||||
|
|
||||||
let list = select.find_also_related(category::Entity)
|
if let Some(ord) = params.order() {
|
||||||
.order_by_desc(article::Column::Id)
|
select = select.order_by(category::Column::Id, ord);
|
||||||
.limit(page_size)
|
}
|
||||||
.offset(offset)
|
|
||||||
.all(conn)
|
let paginator = select
|
||||||
|
.find_also_related(category::Entity)
|
||||||
|
.paginate(conn, page_size);
|
||||||
|
|
||||||
|
let page_total = paginator.num_pages().await.map_err(AppError::from)?;
|
||||||
|
|
||||||
|
let list: Vec<(article::Model, Option<category::Model>)> = paginator
|
||||||
|
.fetch_page(page)
|
||||||
.await
|
.await
|
||||||
.map_err(AppError::from)
|
.map_err(AppError::from)
|
||||||
.map_err(log_error(handler_name))?;
|
.map_err(log_error(handler_name))?;
|
||||||
|
@ -49,4 +57,42 @@ pub async fn index(
|
||||||
params,
|
params,
|
||||||
};
|
};
|
||||||
render(tpl, handler_name)
|
render(tpl, handler_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn add_ui(State(state): State<Arc<AppState>>) -> Result<HtmlResponse> {
|
||||||
|
let handler_name = "article/add_ui";
|
||||||
|
let conn = get_conn(&state);
|
||||||
|
|
||||||
|
let categies = category::Entity::find()
|
||||||
|
.filter(category::Column::IsDel.eq(false))
|
||||||
|
.limit(100)
|
||||||
|
.order_by_asc(category::Column::Id)
|
||||||
|
.all(conn)
|
||||||
|
.await
|
||||||
|
.map_err(AppError::from)
|
||||||
|
.map_err(log_error(handler_name))?;
|
||||||
|
|
||||||
|
let tpl = view::ArticleAddTemplate { categies };
|
||||||
|
render(tpl, handler_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn add(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
Form(frm): Form<ArticleForm>,
|
||||||
|
) -> Result<RedirectResponse> {
|
||||||
|
let handler_name = "article/add";
|
||||||
|
let conn = get_conn(&state);
|
||||||
|
|
||||||
|
article::ActiveModel {
|
||||||
|
id: NotSet,
|
||||||
|
title: Set(frm.title),
|
||||||
|
category_id: Set(frm.category_id),
|
||||||
|
content: Set(frm.content),
|
||||||
|
..Default::default()
|
||||||
|
}.save(conn)
|
||||||
|
.await
|
||||||
|
.map_err(AppError::from)
|
||||||
|
.map_err(log_error(handler_name))?;
|
||||||
|
|
||||||
|
redirect("/article?msg=文章添加成功")
|
||||||
}
|
}
|
|
@ -4,19 +4,26 @@ use crate::handler;
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
pub fn init() -> axum::Router<Arc<AppState>> {
|
pub fn init() -> axum::Router<Arc<AppState>> {
|
||||||
axum::Router::new()
|
let category_router = axum::Router::new()
|
||||||
.route("/", get(handler::index))
|
.route("/", get(handler::category::index))
|
||||||
.route("/category", get(handler::category::index))
|
|
||||||
.route(
|
.route(
|
||||||
"/category/add",
|
"/add",
|
||||||
get(handler::category::add_ui).post(handler::category::add),
|
get(handler::category::add_ui).post(handler::category::add),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/category/edit/:id",
|
"/edit/:id",
|
||||||
get(handler::category::edit_ui).post(handler::category::edit),
|
get(handler::category::edit_ui).post(handler::category::edit),
|
||||||
)
|
)
|
||||||
.route("/category/del/:id", get(handler::category::del))
|
.route("/del/:id", get(handler::category::del))
|
||||||
.route("/category/del/:id/:real", get(handler::category::del))
|
.route("/del/:id/:real", get(handler::category::del))
|
||||||
.route("/category/articles/:id", get(handler::category::articles))
|
.route("/articles/:id", get(handler::category::articles));
|
||||||
.route("/article", get(handler::article::index))
|
|
||||||
}
|
let article_router = axum::Router::new()
|
||||||
|
.route("/", get(handler::article::index))
|
||||||
|
.route("/add", get(handler::article::add_ui).post(handler::article::add));
|
||||||
|
|
||||||
|
axum::Router::new()
|
||||||
|
.route("/", get(handler::index))
|
||||||
|
.nest("/category", category_router)
|
||||||
|
.nest("/article", article_router)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue