Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions src/routes/index.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
use askama::Template;
use askama_web::WebTemplate;
use axum::extract::State;
use axum::response::{IntoResponse, Response};
use snafu::prelude::*;

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

use std::collections::HashMap;

#[derive(Template, WebTemplate)]
#[template(path = "index.html")]
pub struct IndexTemplate {
/// Global application state (errors/warnings)
pub state: AppStateContext,
/// TODO: Submitted values in a POST request
///
/// This happens when the request was rejected by the handler, but still
/// wants to repopulate form data from submitted values.
pub post: HashMap<String, String>,
/// Logged-in user.
pub user: Option<User>,
/// Categories
pub categories: Vec<String>,
/// Category Form Data
pub category_form: Option<CategoryForm>,
}

impl IndexTemplate {
pub async fn new(app_state: AppState, user: Option<User>) -> Result<Self, AppStateError> {
let categories: Vec<String> = CategoryOperator::new(app_state.clone(), user.clone())
.list()
.await
.context(CategorySnafu)?
.into_iter()
.map(|x| x.name)
.collect();

Ok(IndexTemplate {
state: app_state.context().await?,
user,
categories,
})
}
}

pub async fn index(
State(app_state): State<AppState>,
user: Option<User>,
) -> Result<Response, AppStateError> {
let app_state_context = app_state.context().await?;

let categories: Vec<String> = CategoryOperator::new(app_state.clone(), user.clone())
.list()
.await
.context(CategorySnafu)?
.into_iter()
.map(|x| x.name)
.collect();

Ok(IndexTemplate {
state: app_state_context,
post: HashMap::new(),
user,
categories,
category_form: None,
}
.into_response())
) -> Result<IndexTemplate, AppStateError> {
IndexTemplate::new(app_state, user).await
}
5 changes: 5 additions & 0 deletions templates/categories/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
placeholder="Ex: Movies"
name="name"
id="name"
{# This is not defined in the homepage modal, when no categories have been created yet #}
{% if category_form is defined %}
{% if let Some(category_form) = category_form %} value="{{ category_form.name }}" {% endif %}
{% endif %}
/>
</div>

Expand All @@ -19,7 +22,9 @@
placeholder="Ex: /mnt/MyBigHdd/DATA"
name="path"
id="path"
{% if category_form is defined %}
{% if let Some(category_form) = category_form %} value="{{ category_form.path}}" {% endif %}
{% endif %}
/>
</div>

Expand Down