Skip to content

Commit 16c72c9

Browse files
committed
feat: Add IndexTemplate constructor and remove unused fields
1 parent 762a9da commit 16c72c9

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

src/routes/index.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
use askama::Template;
22
use askama_web::WebTemplate;
33
use axum::extract::State;
4-
use axum::response::{IntoResponse, Response};
54
use snafu::prelude::*;
65

76
// TUTORIAL: https://github.com/SeaQL/sea-orm/blob/master/examples/axum_example/
87
use crate::database::category::CategoryOperator;
98
use crate::extractors::user::User;
109
use crate::state::{AppState, AppStateContext, error::*};
1110

12-
use std::collections::HashMap;
13-
1411
#[derive(Template, WebTemplate)]
1512
#[template(path = "index.html")]
1613
pub struct IndexTemplate {
1714
/// Global application state (errors/warnings)
1815
pub state: AppStateContext,
19-
/// TODO: Submitted values in a POST request
20-
///
21-
/// This happens when the request was rejected by the handler, but still
22-
/// wants to repopulate form data from submitted values.
23-
pub post: HashMap<String, String>,
2416
/// Logged-in user.
2517
pub user: Option<User>,
2618
/// Categories
2719
pub categories: Vec<String>,
2820
}
2921

22+
impl IndexTemplate {
23+
pub async fn new(app_state: AppState, user: Option<User>) -> Result<Self, AppStateError> {
24+
let categories: Vec<String> = CategoryOperator::new(app_state.clone(), user.clone())
25+
.list()
26+
.await
27+
.context(CategorySnafu)?
28+
.into_iter()
29+
.map(|x| x.name)
30+
.collect();
31+
32+
Ok(IndexTemplate {
33+
state: app_state.context().await?,
34+
user,
35+
categories,
36+
})
37+
}
38+
}
39+
3040
pub async fn index(
3141
State(app_state): State<AppState>,
3242
user: Option<User>,
33-
) -> Result<Response, AppStateError> {
34-
let app_state_context = app_state.context().await?;
35-
36-
let categories: Vec<String> = CategoryOperator::new(app_state.clone(), user.clone())
37-
.list()
38-
.await
39-
.context(CategorySnafu)?
40-
.into_iter()
41-
.map(|x| x.name)
42-
.collect();
43-
44-
Ok(IndexTemplate {
45-
state: app_state_context,
46-
post: HashMap::new(),
47-
user,
48-
categories,
49-
}
50-
.into_response())
43+
) -> Result<IndexTemplate, AppStateError> {
44+
IndexTemplate::new(app_state, user).await
5145
}

0 commit comments

Comments
 (0)