Skip to content

Commit 64ded3a

Browse files
Gabatxo1312amateurforger
authored andcommitted
Add Waiting to import button for unimported resolved magnet
1 parent 1aa9bd4 commit 64ded3a

12 files changed

Lines changed: 161 additions & 90 deletions

File tree

src/database/magnet.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ impl MagnetOperator {
6161
.context(DBSnafu)
6262
}
6363

64+
/// List unresolved magnet
65+
///
66+
/// Should not fail, unless SQLite was corrupted for some reason.
67+
pub async fn resolved_list_unimported(&self) -> Result<Vec<Model>, MagnetError> {
68+
Entity::find()
69+
.filter(Column::Resolved.eq(true))
70+
.all(&self.state.database)
71+
.await
72+
.context(DBSnafu)
73+
}
74+
6475
pub async fn get(&self, id: i32) -> Result<Model, MagnetError> {
6576
let db = &self.state.database;
6677

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub fn router(state: state::AppState) -> Router {
3434
.route("/magnet/upload", post(routes::magnet::upload))
3535
.route("/magnet/upload", get(routes::magnet::get_upload))
3636
.route("/magnet", get(routes::magnet::list))
37-
.route("/magnet/{id}", get(routes::magnet::show))
3837
// Register static assets routes
3938
.nest("/assets", static_router())
4039
// Insert request timing

src/routes/category.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use axum_extra::extract::CookieJar;
77
use serde::{Deserialize, Serialize};
88
use snafu::prelude::*;
99

10-
use crate::database::category;
1110
use crate::database::category::CategoryError;
12-
use crate::database::content_folder;
11+
use crate::database::{category, content_folder, magnet};
1312
use crate::extractors::normalized_path::*;
1413
use crate::state::flash_message::{OperationStatus, get_cookie};
1514
use crate::state::{AppStateContext, error::*};
@@ -29,13 +28,21 @@ pub struct NewCategoryTemplate {
2928
pub error: Option<CategoryError>,
3029
/// Default form with value
3130
pub category_form: Option<CategoryForm>,
31+
/// all unimported and resolved Magnets
32+
pub resolved_list_unimported: Vec<magnet::Model>,
3233
}
3334

34-
pub async fn new(
35-
app_state_context: AppStateContext,
36-
) -> Result<impl axum::response::IntoResponse, AppStateError> {
35+
pub async fn new(context: AppStateContext) -> Result<NewCategoryTemplate, AppStateError> {
36+
let resolved_list_unimported = context
37+
.db
38+
.magnet()
39+
.resolved_list_unimported()
40+
.await
41+
.context(MagnetUploadSnafu)?;
42+
3743
Ok(NewCategoryTemplate {
38-
state: app_state_context,
44+
state: context,
45+
resolved_list_unimported,
3946
category_form: None,
4047
error: None,
4148
})
@@ -105,6 +112,8 @@ pub struct CategoryShowTemplate {
105112
category: category::Model,
106113
/// Operation status for UI confirmation (Cookie)
107114
pub flash: Option<OperationStatus>,
115+
/// all unimported and resolved Magnets
116+
pub resolved_list_unimported: Vec<magnet::Model>,
108117
}
109118

110119
pub async fn show(
@@ -114,6 +123,13 @@ pub async fn show(
114123
) -> Result<impl IntoResponse, AppStateError> {
115124
let categories = context.db.category();
116125

126+
let resolved_list_unimported = context
127+
.db
128+
.magnet()
129+
.resolved_list_unimported()
130+
.await
131+
.context(MagnetUploadSnafu)?;
132+
117133
let category = categories
118134
.find_by_name(category_name.to_string())
119135
.await
@@ -130,6 +146,7 @@ pub async fn show(
130146
Ok((
131147
jar,
132148
CategoryShowTemplate {
149+
resolved_list_unimported,
133150
content_folders,
134151
category,
135152
state: context,

src/routes/content_folder.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use snafu::prelude::*;
99

1010
use crate::database::content_folder::PathBreadcrumb;
11-
use crate::database::{category, content_folder};
11+
use crate::database::{category, content_folder, magnet};
1212
use crate::extractors::folder_request::FolderRequest;
1313
use crate::state::flash_message::{OperationStatus, get_cookie};
1414
use crate::state::{AppStateContext, error::*};
@@ -38,13 +38,22 @@ pub struct ContentFolderShowTemplate {
3838
pub parent_folder: Option<content_folder::Model>,
3939
/// Operation status for UI confirmation (Cookie)
4040
pub flash: Option<OperationStatus>,
41+
/// all unimported and resolved Magnets
42+
pub resolved_list_unimported: Vec<magnet::Model>,
4143
}
4244

4345
pub async fn show(
4446
context: AppStateContext,
4547
folder: FolderRequest,
4648
jar: CookieJar,
4749
) -> Result<(CookieJar, ContentFolderShowTemplate), AppStateError> {
50+
let resolved_list_unimported = context
51+
.db
52+
.magnet()
53+
.resolved_list_unimported()
54+
.await
55+
.context(MagnetUploadSnafu)?;
56+
4857
let (jar, operation_status) = get_cookie(jar);
4958

5059
Ok((
@@ -56,6 +65,7 @@ pub async fn show(
5665
current_content_folder: folder.folder,
5766
category: folder.category,
5867
state: context,
68+
resolved_list_unimported,
5969
flash: operation_status,
6070
},
6171
))

src/routes/index.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use axum_extra::extract::CookieJar;
44
use snafu::prelude::*;
55

66
// TUTORIAL: https://github.com/SeaQL/sea-orm/blob/master/examples/axum_example/
7-
use crate::database::category;
7+
use crate::database::{category, magnet};
88
use crate::routes::magnet::MagnetForm;
99
use crate::state::flash_message::{OperationStatus, get_cookie};
1010
use crate::state::{AppStateContext, error::*};
@@ -18,6 +18,8 @@ pub struct IndexTemplate {
1818
pub categories: Vec<category::Model>,
1919
/// Operation status for UI confirmation
2020
pub flash: Option<OperationStatus>,
21+
/// all unimported and resolved Magnets
22+
pub resolved_list_unimported: Vec<magnet::Model>,
2123
}
2224

2325
#[derive(Template, WebTemplate)]
@@ -32,6 +34,8 @@ pub struct UploadTemplate {
3234
pub post: Option<MagnetForm>,
3335
/// Error with submitted magnet
3436
pub post_error: Option<AppStateError>,
37+
/// all unimported and resolved Magnets
38+
pub resolved_list_unimported: Vec<magnet::Model>,
3539
}
3640

3741
impl IndexTemplate {
@@ -41,12 +45,22 @@ impl IndexTemplate {
4145
) -> Result<(CookieJar, Self), AppStateError> {
4246
let categories = context.db.category().list().await.context(CategorySnafu)?;
4347

48+
let resolved_list_unimported = context
49+
.db
50+
.magnet()
51+
.resolved_list_unimported()
52+
.await
53+
// TODO: this should probably not be a MagnetUploadSnafu because
54+
// that's not the context in which we're performing the operation
55+
.context(MagnetUploadSnafu)?;
56+
4457
let (jar, operation_status) = get_cookie(jar);
4558

4659
Ok((
4760
jar,
4861
IndexTemplate {
4962
state: context,
63+
resolved_list_unimported,
5064
categories,
5165
flash: operation_status,
5266
},
@@ -66,11 +80,19 @@ impl UploadTemplate {
6680
.map(|x| x.name.to_string())
6781
.collect();
6882

83+
let resolved_list_unimported = context
84+
.db
85+
.magnet()
86+
.resolved_list_unimported()
87+
.await
88+
.context(MagnetUploadSnafu)?;
89+
6990
Ok(UploadTemplate {
7091
state: context,
7192
categories,
7293
post: None,
7394
post_error: None,
95+
resolved_list_unimported,
7496
})
7597
}
7698
}

src/routes/logs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use askama::Template;
22
use askama_web::WebTemplate;
33
use snafu::prelude::*;
44

5+
use crate::database::magnet;
56
use crate::database::operation::OperationLog;
67
use crate::database::operation::OperationType;
78
use crate::state::{AppStateContext, error::*};
@@ -11,13 +12,22 @@ use crate::state::{AppStateContext, error::*};
1112
pub struct LogTemplate {
1213
pub state: AppStateContext,
1314
pub logs: Vec<OperationLog>,
15+
/// all unimported and resolved Magnets
16+
pub resolved_list_unimported: Vec<magnet::Model>,
1417
}
1518

1619
pub async fn index(context: AppStateContext) -> Result<LogTemplate, AppStateError> {
1720
let logs = context.state.logger.read().await.context(LoggerSnafu)?;
21+
let resolved_list_unimported = context
22+
.db
23+
.magnet()
24+
.resolved_list_unimported()
25+
.await
26+
.context(MagnetUploadSnafu)?;
1827

1928
Ok(LogTemplate {
2029
state: context,
30+
resolved_list_unimported,
2131
logs,
2232
})
2333
}

src/routes/magnet.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use askama::Template;
22
use askama_web::WebTemplate;
3-
use axum::extract::{Form, Path};
4-
use axum::response::{IntoResponse, Response};
3+
use axum::extract::Form;
4+
use axum::response::{IntoResponse, Redirect, Response};
55
use serde::{Deserialize, Serialize};
66
use snafu::prelude::*;
77

8-
use crate::database::category;
9-
use crate::database::magnet;
8+
use crate::database::{category, magnet};
109
use crate::state::{AppStateContext, error::*};
1110

1211
/// Multipart form submitted to /magnet/upload:
@@ -17,55 +16,25 @@ pub struct MagnetForm {
1716
pub magnet: String,
1817
}
1918

20-
#[derive(Template, WebTemplate)]
21-
#[template(path = "magnet/show.html")]
22-
pub struct MagnetTemplate {
23-
/// Global application state (errors/warnings)
24-
pub state: AppStateContext,
25-
/// Parsed magnet from form
26-
pub magnet: magnet::Model,
27-
}
28-
29-
pub async fn show(
30-
context: AppStateContext,
31-
Path(id): Path<i32>,
32-
) -> Result<impl IntoResponse, AppStateError> {
33-
let magnet = context
34-
.db
35-
.magnet()
36-
.get(id)
37-
.await
38-
.boxed()
39-
.context(OtherSnafu)?;
40-
41-
Ok(MagnetTemplate {
42-
state: context,
43-
magnet,
44-
})
45-
}
46-
4719
pub async fn upload(
4820
context: AppStateContext,
4921
Form(form): Form<MagnetForm>,
5022
) -> Result<Response, AppStateError> {
51-
// Parse magnet
52-
match context
23+
// TODO: proper error type
24+
if let Err(e) = context
5325
.db
5426
.magnet()
5527
.create(&form)
5628
.await
5729
.context(MagnetUploadSnafu)
5830
{
59-
Ok(magnet_model) => Ok(MagnetTemplate {
60-
state: context,
61-
magnet: magnet_model,
62-
}
63-
.into_response()),
64-
Err(e) => Ok(UploadMagnetTemplate::new(context)
31+
return Ok(UploadMagnetTemplate::new(context)
6532
.await?
6633
.with_errored_form(form, e)
67-
.into_response()),
34+
.into_response());
6835
}
36+
37+
Ok(Redirect::to("/magnet").into_response())
6938
}
7039

7140
#[derive(Template, WebTemplate)]
@@ -75,6 +44,8 @@ pub struct MagnetListTemplate {
7544
pub state: AppStateContext,
7645
/// Magnets stored in database
7746
pub magnets: Vec<magnet::Model>,
47+
/// all unimported and resolved Magnets
48+
pub resolved_list_unimported: Vec<magnet::Model>,
7849
}
7950

8051
pub async fn list(context: AppStateContext) -> Result<impl IntoResponse, AppStateError> {
@@ -86,8 +57,16 @@ pub async fn list(context: AppStateContext) -> Result<impl IntoResponse, AppStat
8657
.boxed()
8758
.context(OtherSnafu)?;
8859

60+
let resolved_list_unimported = context
61+
.db
62+
.magnet()
63+
.resolved_list_unimported()
64+
.await
65+
.context(MagnetUploadSnafu)?;
66+
8967
Ok(MagnetListTemplate {
9068
state: context,
69+
resolved_list_unimported,
9170
magnets,
9271
})
9372
}
@@ -102,6 +81,8 @@ pub struct UploadMagnetTemplate {
10281
/// Error with submitted magnet
10382
pub post_error: Option<AppStateError>,
10483
pub categories: Vec<category::Model>,
84+
/// all unimported and resolved Magnets
85+
pub resolved_list_unimported: Vec<magnet::Model>,
10586
}
10687

10788
pub async fn get_upload(context: AppStateContext) -> Result<UploadMagnetTemplate, AppStateError> {
@@ -111,9 +92,16 @@ pub async fn get_upload(context: AppStateContext) -> Result<UploadMagnetTemplate
11192
impl UploadMagnetTemplate {
11293
pub async fn new(context: AppStateContext) -> Result<Self, AppStateError> {
11394
let categories = context.db.category().list().await.context(CategorySnafu)?;
95+
let resolved_list_unimported = context
96+
.db
97+
.magnet()
98+
.resolved_list_unimported()
99+
.await
100+
.context(MagnetUploadSnafu)?;
114101

115102
Ok(UploadMagnetTemplate {
116103
state: context,
104+
resolved_list_unimported,
117105
categories,
118106
post: None,
119107
post_error: None,

0 commit comments

Comments
 (0)