axum-with-seaorm/src/handler/article.rs

40 lines
1.3 KiB
Rust

use axum::{Form, Router};
use axum::extract::Query;
use axum::routing::get;
use crate::common::{response::HtmlResponse, Result};
use crate::common::response::{redirect, RedirectResponse};
use crate::common::template::render;
use crate::form::ArticleForm;
use crate::param::ArticleParams;
use crate::service::article::ArticleService;
pub fn init_router() -> Router {
Router::new()
.route("/", get(index))
.route("/add", get(add_ui).post(add))
.route("/tags", get(list_with_tags))
}
pub async fn index(Query(params): Query<ArticleParams>) -> Result<HtmlResponse> {
let handler_name = "article/index";
let tpl = ArticleService::index(handler_name, params).await?;
render(tpl, handler_name)
}
pub async fn add_ui() -> Result<HtmlResponse> {
let handler_name = "article/add_ui";
let tpl = ArticleService::add_ui(handler_name).await?;
render(tpl, handler_name)
}
pub async fn add(Form(frm): Form<ArticleForm>) -> Result<RedirectResponse> {
let handler_name = "article/add";
ArticleService::add(handler_name, frm).await?;
redirect("/article?msg=文章添加成功")
}
pub async fn list_with_tags() -> Result<String> {
let handler_name = "article/list_with_tags";
ArticleService::list_with_tags(handler_name).await
}