Skip to content

Commit ae8d6de

Browse files
committed
feat: Allow typed/sourced errors in content folder creation form
1 parent 44b5d5a commit ae8d6de

6 files changed

Lines changed: 47 additions & 30 deletions

File tree

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ pub fn router(state: state::AppState) -> Router {
2525
.route("/categories", post(routes::category::create))
2626
.route("/categories/new", get(routes::category::new))
2727
.route("/categories/{id}/delete", get(routes::category::delete))
28-
.route("/folders/{category_id}", get(routes::category::show))
28+
.route("/folders/{category}", get(routes::category::show))
29+
.route("/folders/{category}", post(routes::category::create_folder))
2930
.route(
3031
"/folders/{category_name}/{*folder_path}",
3132
get(routes::content_folder::show),
3233
)
34+
.route(
35+
"/folders/{category_name}/{*folder_path}",
36+
post(routes::content_folder::create_subfolder),
37+
)
3338
.route("/folders", get(routes::index::index))
34-
.route("/folders", post(routes::content_folder::create))
3539
.route("/logs", get(routes::logs::index))
3640
// Register static assets routes
3741
.nest("/assets", static_router())

src/routes/category.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::database::category;
99
use crate::database::content_folder::PathBreadcrumb;
1010
use crate::extractors::category_request::CategoryRequest;
1111
use crate::filesystem::FileSystemEntry;
12+
use crate::routes::content_folder::ContentFolderForm;
1213
use crate::state::AppStateContext;
1314
use crate::state::flash_message::{
1415
FallibleTemplate, FlashRedirect, FlashTemplate, OperationStatus, StatusCookie,
@@ -117,3 +118,29 @@ pub async fn show(
117118
) -> FlashTemplate<CategoryShowTemplate> {
118119
status.with_template(CategoryShowTemplate::new(context, category))
119120
}
121+
122+
pub async fn create_folder(
123+
context: AppStateContext,
124+
jar: CookieJar,
125+
category: CategoryRequest,
126+
Form(form): Form<ContentFolderForm>,
127+
) -> Result<FlashRedirect, CategoryShowTemplate> {
128+
match context.db.content_folder().create(&form).await {
129+
Ok(created) => {
130+
let status = StatusCookie::success(
131+
jar,
132+
format!(
133+
"The folder {} has been successfully created (ID: {})",
134+
created.name, created.id
135+
),
136+
);
137+
138+
let uri = format!("/folders/{}{}", category.category.name, created.path);
139+
Ok(status.redirect(&uri))
140+
}
141+
Err(error) => {
142+
let status = OperationStatus::error(error);
143+
Err(status.with_template(CategoryShowTemplate::new(context, category)))
144+
}
145+
}
146+
}

src/routes/content_folder.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ use askama_web::WebTemplate;
33
use axum::Form;
44
use axum_extra::extract::CookieJar;
55
use serde::{Deserialize, Serialize};
6-
use snafu::prelude::*;
76

87
use crate::database::content_folder::PathBreadcrumb;
98
use crate::database::{category, content_folder};
109
use crate::extractors::folder_request::FolderRequest;
1110
use crate::filesystem::FileSystemEntry;
11+
use crate::state::AppStateContext;
1212
use crate::state::flash_message::{
1313
FallibleTemplate, FlashRedirect, FlashTemplate, OperationStatus, StatusCookie,
1414
};
15-
use crate::state::{AppStateContext, error::*};
1615

1716
#[derive(Clone, Debug, Deserialize, Serialize)]
1817
pub struct ContentFolderForm {
@@ -72,19 +71,12 @@ pub async fn show(
7271
status.with_template(ContentFolderShowTemplate::new(context, folder))
7372
}
7473

75-
// TODO: currently an error takes us back to /
76-
pub async fn create(
74+
pub async fn create_subfolder(
7775
context: AppStateContext,
7876
jar: CookieJar,
77+
folder: FolderRequest,
7978
Form(form): Form<ContentFolderForm>,
80-
) -> Result<FlashRedirect, AppStateError> {
81-
let category = context
82-
.db
83-
.category()
84-
.find_by_id(form.category_id)
85-
.await
86-
.context(CategorySnafu)?;
87-
79+
) -> Result<FlashRedirect, ContentFolderShowTemplate> {
8880
match context.db.content_folder().create(&form).await {
8981
Ok(created) => {
9082
let status = StatusCookie::success(
@@ -95,23 +87,12 @@ pub async fn create(
9587
),
9688
);
9789

98-
let uri = format!("/folders/{}{}", category.name, created.path);
90+
let uri = format!("/folders/{}{}", folder.category.name, created.path);
9991
Ok(status.redirect(&uri))
10092
}
10193
Err(error) => {
102-
let status = StatusCookie::error(jar, error.to_string());
103-
let uri = if let Some(parent_id) = form.parent_id {
104-
let parent = context
105-
.db
106-
.content_folder()
107-
.find_by_id(parent_id)
108-
.await
109-
.context(ContentFolderSnafu)?;
110-
format!("/folders/{}{}", category.name, parent.path)
111-
} else {
112-
format!("/folders{}", category.name)
113-
};
114-
Ok(status.redirect(&uri))
94+
let status = OperationStatus::error(error);
95+
Err(status.with_template(ContentFolderShowTemplate::new(context, folder)))
11596
}
11697
}
11798
}

src/state/flash_message.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ impl OperationStatus {
7676
message: MessageOrError::Message(message),
7777
}
7878
}
79+
80+
pub fn with_template<T: FallibleTemplate>(self, mut template: T) -> T {
81+
template.with_optional_flash(Some(self));
82+
template
83+
}
7984
}
8085

8186
/// An operation status passed as a cookie.

templates/categories/show.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{% endblock %}
1010

1111
{% block content_folder_form %}
12-
<form method="POST" action="/folders" accept-charset="utf-8">
12+
<form method="POST" accept-charset="utf-8">
1313
<div class="form-group">
1414
<label for="name" class="form-label">Name</label>
1515
<input

templates/content_folders/show.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{% endblock %}
1010

1111
{% block content_folder_form %}
12-
<form method="POST" action="/folders" accept-charset="utf-8">
12+
<form method="POST" accept-charset="utf-8">
1313
<div class="form-group">
1414
<label for="name" class="form-label">Name</label>
1515
<input

0 commit comments

Comments
 (0)