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
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub fn router(state: state::AppState) -> Router {
.route("/", get(routes::index::index))
.route("/upload", get(routes::index::upload))
.route("/progress/{view_request}", get(routes::progress::progress))
.route("/categories", get(routes::category::index))
.route("/categories", post(routes::category::create))
.route("/categories/new", get(routes::category::new))
.route("/categories/{id}/delete", get(routes::category::delete))
Expand Down
78 changes: 26 additions & 52 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ pub struct CategoryForm {
pub path: NormalizedPathAbsolute,
}

#[derive(Template, WebTemplate)]
#[template(path = "categories/index.html")]
pub struct CategoriesTemplate {
/// Global application state
pub state: AppStateContext,
/// Categories found in database
pub categories: Vec<category::Model>,
/// Logged-in user.
pub user: Option<User>,
/// Operation status for UI confirmation
pub flash: Option<OperationStatus>,
}

#[derive(Template, WebTemplate)]
#[template(path = "categories/new.html")]
pub struct NewCategoryTemplate {
Expand Down Expand Up @@ -94,7 +81,6 @@ pub async fn create(
jar: CookieJar,
Form(form): Form<CategoryForm>,
) -> Result<impl axum::response::IntoResponse, AppStateError> {
let app_state_context = app_state.context().await?;
let categories = CategoryOperator::new(app_state.clone(), user.clone());

let created = categories.create(&form, user.clone()).await;
Expand All @@ -111,40 +97,19 @@ pub async fn create(

let jar = operation_status.set_cookie(jar);

Ok((jar, Redirect::to("/categories").into_response()))
Ok((jar, Redirect::to("/").into_response()))
}
Err(error) => Ok((
jar,
NewCategoryTemplate {
state: app_state_context,
user,
category_form: Some(form),
error: Some(error),
}
.into_response(),
)),
}
}

pub async fn index(
State(app_state): State<AppState>,
user: Option<User>,
jar: CookieJar,
) -> Result<(CookieJar, CategoriesTemplate), AppStateError> {
let app_state_context = app_state.context().await?;
let categories = CategoryOperator::new(app_state.clone(), user.clone());
Err(error) => {
let operation_status = OperationStatus {
success: false,
message: format!("{}", error),
};

let (jar, operation_status) = get_cookie(jar);
let jar = operation_status.set_cookie(jar);

Ok((
jar,
CategoriesTemplate {
categories: categories.list().await.context(CategorySnafu)?,
state: app_state_context,
user,
flash: operation_status,
},
))
Ok((jar, Redirect::to("/").into_response()))
}
}
}

#[derive(Template, WebTemplate)]
Expand All @@ -158,13 +123,16 @@ pub struct CategoryShowTemplate {
pub user: Option<User>,
/// Category
category: category::Model,
/// Operation status for UI confirmation (Cookie)
pub flash: Option<OperationStatus>,
}

pub async fn show(
State(app_state): State<AppState>,
user: Option<User>,
Path(category_name): Path<String>,
) -> Result<CategoryShowTemplate, AppStateError> {
jar: CookieJar,
) -> Result<impl IntoResponse, AppStateError> {
let app_state_context = app_state.context().await?;

let category: category::Model = CategoryOperator::new(app_state.clone(), user.clone())
Expand All @@ -179,10 +147,16 @@ pub async fn show(
.await
.context(CategorySnafu)?;

Ok(CategoryShowTemplate {
content_folders,
category,
state: app_state_context,
user,
})
let (jar, operation_status) = get_cookie(jar);

Ok((
jar,
CategoryShowTemplate {
content_folders,
category,
state: app_state_context,
user,
flash: operation_status,
},
))
}
31 changes: 23 additions & 8 deletions src/routes/index.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use askama::Template;
use askama_web::WebTemplate;
use axum::extract::State;
use axum_extra::extract::CookieJar;
use snafu::prelude::*;

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

#[derive(Template, WebTemplate)]
Expand All @@ -17,6 +19,8 @@ pub struct IndexTemplate {
pub user: Option<User>,
/// Categories
pub categories: Vec<category::Model>,
/// Operation status for UI confirmation
pub flash: Option<OperationStatus>,
}

#[derive(Template, WebTemplate)]
Expand All @@ -31,19 +35,29 @@ pub struct UploadTemplate {
}

impl IndexTemplate {
pub async fn new(app_state: AppState, user: Option<User>) -> Result<Self, AppStateError> {
pub async fn new(
app_state: AppState,
user: Option<User>,
jar: CookieJar,
) -> Result<(CookieJar, Self), AppStateError> {
let app_state_context = app_state.context().await?;

let categories = CategoryOperator::new(app_state.clone(), user.clone())
.list()
.await
.context(CategorySnafu)?;

Ok(IndexTemplate {
state: app_state_context,
user,
categories,
})
let (jar, operation_status) = get_cookie(jar);

Ok((
jar,
IndexTemplate {
state: app_state_context,
user,
categories,
flash: operation_status,
},
))
}
}

Expand All @@ -68,8 +82,9 @@ impl UploadTemplate {
pub async fn index(
State(app_state): State<AppState>,
user: Option<User>,
) -> Result<IndexTemplate, AppStateError> {
IndexTemplate::new(app_state, user).await
jar: CookieJar,
) -> Result<(CookieJar, IndexTemplate), AppStateError> {
IndexTemplate::new(app_state, user, jar).await
}

pub async fn upload(
Expand Down
2 changes: 1 addition & 1 deletion templates/categories/dropdown_actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
Actions
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/categories/new">Create category</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#createCategory">Create category</a></li>
</ul>
</div>
4 changes: 2 additions & 2 deletions templates/categories/form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form method="POST" action="/categories" accept-charset="utf-8">
<div class="form-group">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input
type="text"
Expand All @@ -14,7 +14,7 @@
/>
</div>

<div class="form-group mt-3">
<div class="mb-3">
<label for="path" class="form-label">Path</label>
<input
type="text"
Expand Down
52 changes: 0 additions & 52 deletions templates/categories/index.html

This file was deleted.

4 changes: 4 additions & 0 deletions templates/categories/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ <h5 class="mb-0">
{% include "content_folders/dropdown_actions.html" %}
{% endblock %}

{% block alert_message %}
{% include "shared/alert_operation_status.html" %}
{% endblock %}

{% block content_folder_form %}
<form method="POST" action="/folders" accept-charset="utf-8">
<div class="form-group">
Expand Down
6 changes: 3 additions & 3 deletions templates/content_folders/dropdown_actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Actions
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#createSubFolder">Créer un sous-dossier</a></li>
<li><a class="dropdown-item" href="#">Importer un Magnet</a></li>
<li><a class="dropdown-item" href="#">Importer un Torrent</a></li>
<li><a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#createSubFolder">Create a subfolder</a></li>
<li><a class="dropdown-item" href="#">Import magnet link</a></li>
<li><a class="dropdown-item" href="#">Import torrent file</a></li>
</ul>
</div>
19 changes: 17 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
All categories
{% endblock%}

{% block alert_message %}
{% include "shared/alert_operation_status.html" %}
{% endblock %}

{% block actions_buttons %}
{% include "categories/dropdown_actions.html" %}
{% endblock actions_buttons %}
Expand All @@ -38,6 +42,17 @@ <h5 class="mb-0">
</a>
</li>
{% endfor %}
{% endblock %}


<div class="modal fade" id="createCategory" tabindex="-1" aria-labelledby="createCategoryLabel" aria-hidden="false">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="staticBackdropLabel">Create category</h1>
</div>
<div class="modal-body">
{% include "categories/form.html" %}
</div>
</div>
</div>
</div>
{% endblock %}
15 changes: 0 additions & 15 deletions templates/menus/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,9 @@
<li class="nav-item">
<a class="nav-link" href="/progress/everything">Torrents list</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Categories
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/categories">List</a></li>
<li><a class="dropdown-item" href="/categories/new">new</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/logs">Logs</a>
</li>
<!-- <li class="nav-item"> -->
<!-- <a class="nav-link" href="/collection">Collections</a> -->
<!-- </li> -->
<!-- <li class="nav-item"> -->
<!-- <a class="nav-link" href="/chores">Chores</a> -->
<!-- </li> -->
</ul>
<span class="navbar-text">
<a class="btn btn-success text-white" href="/upload">
Expand Down