11use askama:: Template ;
22use askama_web:: WebTemplate ;
3- use axum:: extract:: Form ;
4- use axum:: response:: { IntoResponse , Redirect , Response } ;
53use serde:: { Deserialize , Serialize } ;
64use snafu:: prelude:: * ;
75
8- use crate :: database:: { category, magnet} ;
6+ use sea_orm:: LoaderTrait ;
7+
8+ use crate :: database:: { content_folder, magnet} ;
99use crate :: state:: { AppStateContext , error:: * } ;
1010
1111/// Multipart form submitted to /magnet/upload:
1212///
1313/// - magnet: the magnet link to upload
1414#[ derive( Clone , Debug , Serialize , Deserialize ) ]
1515pub struct MagnetForm {
16+ pub content_folder_id : String ,
1617 pub magnet : String ,
1718}
1819
19- pub async fn upload (
20- context : AppStateContext ,
21- Form ( form) : Form < MagnetForm > ,
22- ) -> Result < Response , AppStateError > {
23- // TODO: proper error type
24- if let Err ( e) = context
25- . db
26- . magnet ( )
27- . create ( & form)
28- . await
29- . context ( MagnetUploadSnafu )
30- {
31- return Ok ( UploadMagnetTemplate :: new ( context)
32- . await ?
33- . with_errored_form ( form, e)
34- . into_response ( ) ) ;
35- }
36-
37- Ok ( Redirect :: to ( "/magnet" ) . into_response ( ) )
38- }
39-
4020#[ derive( Template , WebTemplate ) ]
4121#[ template( path = "magnet/list.html" ) ]
4222pub struct MagnetListTemplate {
4323 /// Global application state (errors/warnings)
4424 pub state : AppStateContext ,
4525 /// Magnets stored in database
46- pub magnets : Vec < magnet:: Model > ,
26+ pub magnets : Vec < ( magnet:: Model , content_folder :: Model ) > ,
4727}
4828
49- pub async fn list ( context : AppStateContext ) -> Result < impl IntoResponse , AppStateError > {
29+ pub async fn list ( context : AppStateContext ) -> Result < MagnetListTemplate , AppStateError > {
5030 let magnets = context
5131 . db
5232 . magnet ( )
@@ -55,43 +35,22 @@ pub async fn list(context: AppStateContext) -> Result<impl IntoResponse, AppStat
5535 . boxed ( )
5636 . context ( OtherSnafu ) ?;
5737
38+ // In the creation form we guarantee to set the content_folder so we can unwrap
39+ let content_folders: Vec < content_folder:: Model > = magnets
40+ . load_one ( content_folder:: Entity , & context. state . database )
41+ . await
42+ . context ( SqliteSnafu ) ?
43+ . into_iter ( )
44+ . map ( |x| x. unwrap ( ) )
45+ . collect ( ) ;
46+
47+ let magnets = magnets
48+ . into_iter ( )
49+ . zip ( content_folders. into_iter ( ) )
50+ . collect ( ) ;
51+
5852 Ok ( MagnetListTemplate {
5953 state : context,
6054 magnets,
6155 } )
6256}
63-
64- #[ derive( Template , WebTemplate ) ]
65- #[ template( path = "magnet/upload.html" ) ]
66- pub struct UploadMagnetTemplate {
67- /// Global application state (errors/warnings)
68- pub state : AppStateContext ,
69- /// Magnet upload form
70- pub post : Option < MagnetForm > ,
71- /// Error with submitted magnet
72- pub post_error : Option < AppStateError > ,
73- pub categories : Vec < category:: Model > ,
74- }
75-
76- pub async fn get_upload ( context : AppStateContext ) -> Result < UploadMagnetTemplate , AppStateError > {
77- UploadMagnetTemplate :: new ( context) . await
78- }
79-
80- impl UploadMagnetTemplate {
81- pub async fn new ( context : AppStateContext ) -> Result < Self , AppStateError > {
82- let categories = context. db . category ( ) . list ( ) . await . context ( CategorySnafu ) ?;
83-
84- Ok ( UploadMagnetTemplate {
85- state : context,
86- categories,
87- post : None ,
88- post_error : None ,
89- } )
90- }
91-
92- pub fn with_errored_form ( mut self , form : MagnetForm , error : AppStateError ) -> Self {
93- self . post = Some ( form) ;
94- self . post_error = Some ( error) ;
95- self
96- }
97- }
0 commit comments