@@ -7,7 +7,7 @@ use serde_derive::{Deserialize, Serialize};
77use super :: category:: DbCategoryRepository ;
88use super :: user:: DbUserRepository ;
99use crate :: config:: Configuration ;
10- use crate :: databases:: database:: { Category , Database , Error , Sorting } ;
10+ use crate :: databases:: database:: { Database , Error , Sorting } ;
1111use crate :: errors:: ServiceError ;
1212use crate :: models:: category:: CategoryId ;
1313use crate :: models:: info_hash:: InfoHash ;
@@ -38,7 +38,7 @@ pub struct Index {
3838pub struct AddTorrentRequest {
3939 pub title : String ,
4040 pub description : String ,
41- pub category : String ,
41+ pub category_name : String ,
4242 pub tags : Vec < TagId > ,
4343 pub torrent_buffer : Vec < u8 > ,
4444}
@@ -130,23 +130,9 @@ impl Index {
130130 user_id : UserId ,
131131 ) -> Result < AddTorrentResponse , ServiceError > {
132132 // Authorization: only authenticated users ere allowed to upload torrents
133-
134133 let _user = self . user_repository . get_compact ( & user_id) . await ?;
135134
136- // Validate and build metadata
137-
138- let metadata = Metadata :: new (
139- & add_torrent_req. title ,
140- & add_torrent_req. description ,
141- & add_torrent_req. category ,
142- & add_torrent_req. tags ,
143- ) ?;
144-
145- let category = self
146- . category_repository
147- . get_by_name ( & metadata. category )
148- . await
149- . map_err ( |_| ServiceError :: InvalidCategory ) ?;
135+ let metadata = self . validate_and_build_metadata ( & add_torrent_req) . await ?;
150136
151137 // Validate and build torrent file
152138
@@ -198,7 +184,7 @@ impl Index {
198184
199185 let torrent_id = self
200186 . torrent_repository
201- . add ( & original_info_hash, & torrent, & metadata, user_id, category )
187+ . add ( & original_info_hash, & torrent, & metadata, user_id, metadata . category_id )
202188 . await ?;
203189
204190 self . torrent_tag_repository
@@ -236,6 +222,27 @@ impl Index {
236222 } )
237223 }
238224
225+ async fn validate_and_build_metadata ( & self , add_torrent_req : & AddTorrentRequest ) -> Result < Metadata , ServiceError > {
226+ if add_torrent_req. category_name . is_empty ( ) {
227+ return Err ( ServiceError :: MissingMandatoryMetadataFields ) ;
228+ }
229+
230+ let category = self
231+ . category_repository
232+ . get_by_name ( & add_torrent_req. category_name )
233+ . await
234+ . map_err ( |_| ServiceError :: InvalidCategory ) ?;
235+
236+ let metadata = Metadata :: new (
237+ & add_torrent_req. title ,
238+ & add_torrent_req. description ,
239+ category. category_id ,
240+ & add_torrent_req. tags ,
241+ ) ?;
242+
243+ Ok ( metadata)
244+ }
245+
239246 /// Gets a torrent from the Index.
240247 ///
241248 /// # Errors
@@ -533,14 +540,14 @@ impl DbTorrentRepository {
533540 torrent : & Torrent ,
534541 metadata : & Metadata ,
535542 user_id : UserId ,
536- category : Category ,
543+ category_id : CategoryId ,
537544 ) -> Result < TorrentId , Error > {
538545 self . database
539546 . insert_torrent_and_get_id (
540547 original_info_hash,
541548 torrent,
542549 user_id,
543- category . category_id ,
550+ category_id,
544551 & metadata. title ,
545552 & metadata. description ,
546553 )
0 commit comments