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
53 changes: 53 additions & 0 deletions src/extractors/category_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use axum::extract::{FromRequestParts, Path};
use axum::http::request::Parts;
use snafu::prelude::*;

use crate::database::category::{self, CategoryOperator};
use crate::database::content_folder::PathBreadcrumb;
use crate::filesystem::FileSystemEntry;
use crate::state::{AppState, error::*};

#[derive(Clone, Debug)]
pub struct CategoryRequest {
pub category: category::Model,
pub breadcrumbs: Vec<PathBreadcrumb>,
pub children: Vec<FileSystemEntry>,
}

impl FromRequestParts<AppState> for CategoryRequest {
type Rejection = AppStateError;

async fn from_request_parts(
parts: &mut Parts,
app_state: &AppState,
) -> Result<Self, Self::Rejection> {
let Path(category_name) =
<Path<String> as FromRequestParts<AppState>>::from_request_parts(parts, app_state)
.await
.unwrap();

// Read-only operators: no need to extract the current user
let categories = CategoryOperator::new(app_state.clone(), None);

let category = categories
.find_by_name(category_name.to_string())
.await
.context(CategorySnafu)?;

// get all content folders in this category
let content_folders = categories
.list_folders(category.id)
.await
.context(CategorySnafu)?;

let children = FileSystemEntry::from_content_folders(&category, &content_folders);

let breadcrumbs = PathBreadcrumb::for_filesystem_path(category.name.as_str());

Ok(Self {
category,
children,
breadcrumbs,
})
}
}
1 change: 1 addition & 0 deletions src/extractors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod category_request;
pub mod folder_request;
pub mod normalized_path;
pub mod torrent_list;
Expand Down
51 changes: 23 additions & 28 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use axum::Form;
use axum::extract::Path;
use axum_extra::extract::CookieJar;
use serde::{Deserialize, Serialize};
use snafu::prelude::*;

use crate::database::category;
use crate::database::content_folder::PathBreadcrumb;
use crate::extractors::category_request::CategoryRequest;
use crate::extractors::normalized_path::*;
use crate::filesystem::FileSystemEntry;
use crate::state::AppStateContext;
use crate::state::flash_message::{
FallibleTemplate, FlashRedirect, FlashTemplate, OperationStatus, StatusCookie,
};
use crate::state::{AppStateContext, error::*};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CategoryForm {
Expand Down Expand Up @@ -87,6 +87,24 @@ pub struct CategoryShowTemplate {
pub breadcrumbs: Vec<PathBreadcrumb>,
}

impl CategoryShowTemplate {
fn new(context: AppStateContext, category: CategoryRequest) -> Self {
let CategoryRequest {
breadcrumbs,
category,
children,
} = category;

Self {
breadcrumbs,
category,
children,
flash: None,
state: context,
}
}
}

impl FallibleTemplate for CategoryShowTemplate {
fn with_optional_flash(&mut self, flash: Option<OperationStatus>) {
self.flash = flash;
Expand All @@ -95,31 +113,8 @@ impl FallibleTemplate for CategoryShowTemplate {

pub async fn show(
context: AppStateContext,
Path(category_name): Path<String>,
category: CategoryRequest,
status: StatusCookie,
) -> Result<FlashTemplate<CategoryShowTemplate>, AppStateError> {
let categories = context.db.category();

let category = categories
.find_by_name(category_name.to_string())
.await
.context(CategorySnafu)?;

// get all content folders in this category
let content_folders = categories
.list_folders(category.id)
.await
.context(CategorySnafu)?;

let children = FileSystemEntry::from_content_folders(&category, &content_folders);

let breadcrumbs = PathBreadcrumb::for_filesystem_path(category.name.as_str());

Ok(status.with_template(CategoryShowTemplate {
category,
children,
state: context,
flash: None,
breadcrumbs,
}))
) -> FlashTemplate<CategoryShowTemplate> {
status.with_template(CategoryShowTemplate::new(context, category))
}
31 changes: 22 additions & 9 deletions src/routes/content_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct ContentFolderShowTemplate {
/// Global application state
pub state: AppStateContext,
/// current folder
pub current_content_folder: content_folder::Model,
pub folder: content_folder::Model,
/// Folders with parent_id set to current folder
pub children: Vec<FileSystemEntry>,
/// Category
Expand All @@ -41,6 +41,26 @@ pub struct ContentFolderShowTemplate {
pub flash: Option<OperationStatus>,
}

impl ContentFolderShowTemplate {
fn new(context: AppStateContext, folder: FolderRequest) -> Self {
let FolderRequest {
breadcrumbs,
category,
children,
folder,
} = folder;

Self {
breadcrumbs,
category,
children,
flash: None,
folder,
state: context,
}
}
}

impl FallibleTemplate for ContentFolderShowTemplate {
fn with_optional_flash(&mut self, flash: Option<OperationStatus>) {
self.flash = flash;
Expand All @@ -52,14 +72,7 @@ pub async fn show(
folder: FolderRequest,
status: StatusCookie,
) -> FlashTemplate<ContentFolderShowTemplate> {
status.with_template(ContentFolderShowTemplate {
breadcrumbs: folder.breadcrumbs,
children: folder.children,
current_content_folder: folder.folder,
category: folder.category,
state: context,
flash: None,
})
status.with_template(ContentFolderShowTemplate::new(context, folder))
}

pub async fn create(
Expand Down
4 changes: 2 additions & 2 deletions templates/content_folders/show.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "layouts/file_system_base.html" %}

{% block folder_title %}
{{ current_content_folder.name }}
{{ folder.name }}
{% endblock%}

{% block actions_buttons %}
Expand All @@ -22,7 +22,7 @@
</div>

<input type="hidden" id="category_id" name="category_id" value="{{ category.id }}" />
<input type="hidden" id="parent_id" name="parent_id" value="{{ current_content_folder.id }}" />
<input type="hidden" id="parent_id" name="parent_id" value="{{ folder.id }}" />
<input type="hidden" id="path" name="path" value="fake_path" />

<div class="text-center mt-4">
Expand Down