11use askama:: Template ;
22use askama_web:: WebTemplate ;
33use axum:: Form ;
4- use axum:: extract:: Path ;
4+ use axum:: extract:: { Path , Query } ;
5+ use axum:: response:: { IntoResponse , Response } ;
56use axum_extra:: extract:: CookieJar ;
67use serde:: { Deserialize , Serialize } ;
8+ use snafu:: prelude:: * ;
79
810use crate :: database:: category;
911use crate :: database:: content_folder:: PathBreadcrumb ;
1012use crate :: database:: torrent;
1113use crate :: extractors:: category_request:: { CategoriesRequest , CategoryRequest } ;
14+ use crate :: extractors:: moving:: MovingQuery ;
1215use crate :: filesystem:: FileSystemEntry ;
1316use crate :: routes:: content_folder:: ContentFolderForm ;
1417use crate :: routes:: index:: IndexTemplate ;
15- use crate :: state:: AppStateContext ;
16- use crate :: state:: flash_message:: {
17- FallibleTemplate , FlashRedirect , FlashTemplate , OperationStatus , StatusCookie ,
18- } ;
18+ use crate :: state:: flash_message:: { FallibleTemplate , FlashRedirect , OperationStatus , StatusCookie } ;
19+ use crate :: state:: { AppStateContext , error:: * } ;
1920
2021#[ derive( Clone , Debug , Deserialize , Serialize ) ]
2122pub struct CategoryForm {
@@ -95,10 +96,16 @@ pub struct CategoryShowTemplate {
9596 pub breadcrumbs : Vec < PathBreadcrumb > ,
9697 /// Torrents in this category
9798 pub torrents : Vec < torrent:: Model > ,
99+ /// If any, the current torrent being moved in the folder.
100+ pub current_torrent : Option < torrent:: Model > ,
98101}
99102
100103impl CategoryShowTemplate {
101- fn new ( context : AppStateContext , category : CategoryRequest ) -> Self {
104+ fn new (
105+ context : AppStateContext ,
106+ category : CategoryRequest ,
107+ current_torrent : Option < torrent:: Model > ,
108+ ) -> Self {
102109 let CategoryRequest {
103110 breadcrumbs,
104111 category,
@@ -113,6 +120,7 @@ impl CategoryShowTemplate {
113120 flash : None ,
114121 state : context,
115122 torrents,
123+ current_torrent,
116124 }
117125 }
118126}
@@ -127,8 +135,41 @@ pub async fn show(
127135 context : AppStateContext ,
128136 category : CategoryRequest ,
129137 status : StatusCookie ,
130- ) -> FlashTemplate < CategoryShowTemplate > {
131- status. with_template ( CategoryShowTemplate :: new ( context, category) )
138+ Query ( moving) : Query < MovingQuery > ,
139+ ) -> Result < Response , AppStateError > {
140+ if let Some ( id) = moving. id {
141+ // We are currently moving a torrent between folders
142+ let torrent: torrent:: Model = context
143+ . db
144+ . torrent ( )
145+ . get ( id)
146+ . await
147+ . context ( TorrentUploadSnafu ) ?;
148+ if moving. validate {
149+ // Save to DB the new location of the torrent
150+ let _torrent = context
151+ . db
152+ . torrent ( )
153+ . update_category_content_folder ( torrent. clone ( ) , category. category . clone ( ) , None )
154+ . await
155+ . context ( TorrentUploadSnafu ) ?;
156+ // Now we produce a redirection (to the same page) in order to refresh the list of torrents
157+ // in this folder, which was already computed.
158+ Ok ( status
159+ . with_success ( "Torrent successfully saved to this folder" . to_string ( ) )
160+ // Need to remove the query params
161+ . redirect ( "?" )
162+ . into_response ( ) )
163+ } else {
164+ Ok ( status
165+ . with_template ( CategoryShowTemplate :: new ( context, category, Some ( torrent) ) )
166+ . into_response ( ) )
167+ }
168+ } else {
169+ Ok ( status
170+ . with_template ( CategoryShowTemplate :: new ( context, category, None ) )
171+ . into_response ( ) )
172+ }
132173}
133174
134175pub async fn create_folder (
@@ -152,7 +193,7 @@ pub async fn create_folder(
152193 }
153194 Err ( error) => {
154195 let status = OperationStatus :: error ( error) ;
155- Err ( status. with_template ( CategoryShowTemplate :: new ( context, category) ) )
196+ Err ( status. with_template ( CategoryShowTemplate :: new ( context, category, None ) ) )
156197 }
157198 }
158199}
0 commit comments