11use askama:: Template ;
22use 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 } ;
55use serde:: { Deserialize , Serialize } ;
66use snafu:: prelude:: * ;
77
8- use crate :: database:: category;
9- use crate :: database:: magnet;
8+ use crate :: database:: { category, magnet} ;
109use 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-
4719pub 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 ) ]
0 commit comments