@@ -13,7 +13,9 @@ use crate::models::category::CategoryId;
1313use crate :: models:: info_hash:: InfoHash ;
1414use crate :: models:: response:: TorrentsResponse ;
1515use crate :: models:: torrent:: { Metadata , TorrentListing } ;
16- use crate :: models:: torrent_file:: { DbTorrent , DbTorrentAnnounceUrl , DbTorrentFile , DbTorrentHttpSeedUrl , Torrent , TorrentFile } ;
16+ use crate :: models:: torrent_file:: {
17+ DbTorrent , DbTorrentAnnounceUrl , DbTorrentFile , DbTorrentHttpSeedUrl , DbTorrentNode , Torrent , TorrentFile ,
18+ } ;
1719use crate :: models:: torrent_tag:: { TagId , TorrentTag } ;
1820use crate :: models:: tracker_key:: TrackerKey ;
1921use crate :: models:: user:: { User , UserAuthentication , UserCompact , UserId , UserProfile } ;
@@ -600,6 +602,31 @@ impl Database for Sqlite {
600602 return Err ( e) ;
601603 }
602604
605+ // add nodes
606+
607+ let insert_torrent_nodes_result: Result < ( ) , database:: Error > = if let Some ( nodes) = & torrent. nodes {
608+ for node in nodes {
609+ let ( ) = query ( "INSERT INTO torrust_torrent_nodes (torrent_id, node_ip, node_port) VALUES (?, ?, ?)" )
610+ . bind ( torrent_id)
611+ . bind ( node. 0 . clone ( ) )
612+ . bind ( node. 1 )
613+ . execute ( & mut * tx)
614+ . await
615+ . map ( |_| ( ) )
616+ . map_err ( |_| database:: Error :: Error ) ?;
617+ }
618+
619+ Ok ( ( ) )
620+ } else {
621+ Ok ( ( ) )
622+ } ;
623+
624+ // rollback transaction on error
625+ if let Err ( e) = insert_torrent_nodes_result {
626+ drop ( tx. rollback ( ) . await ) ;
627+ return Err ( e) ;
628+ }
629+
603630 // add tags
604631
605632 for tag_id in & metadata. tags {
@@ -767,6 +794,15 @@ impl Database for Sqlite {
767794 . map_err ( |_| database:: Error :: TorrentNotFound )
768795 }
769796
797+ async fn get_torrent_nodes_from_id ( & self , torrent_id : i64 ) -> Result < Vec < ( String , i64 ) > , database:: Error > {
798+ query_as :: < _ , DbTorrentNode > ( "SELECT node_ip, node_port FROM torrust_torrent_nodes WHERE torrent_id = ?" )
799+ . bind ( torrent_id)
800+ . fetch_all ( & self . pool )
801+ . await
802+ . map ( |v| v. iter ( ) . map ( |a| ( a. node_ip . to_string ( ) , a. node_port ) ) . collect ( ) )
803+ . map_err ( |_| database:: Error :: TorrentNotFound )
804+ }
805+
770806 async fn get_torrent_listing_from_id ( & self , torrent_id : i64 ) -> Result < TorrentListing , database:: Error > {
771807 query_as :: < _ , TorrentListing > (
772808 "SELECT
0 commit comments