@@ -6,10 +6,11 @@ use diesel::{
6
6
r2d2:: { ConnectionManager , Pool } ,
7
7
PgConnection ,
8
8
} ;
9
+ use log:: info;
9
10
10
11
use crate :: {
11
12
execution:: ports:: repository:: { InsertNewsInput , Repository } ,
12
- schema:: news,
13
+ schema:: news:: { self , article_id } ,
13
14
} ;
14
15
15
16
pub ( crate ) struct PostgresqlClient {
@@ -41,6 +42,7 @@ struct InsertNewsValue {
41
42
#[ async_trait]
42
43
impl Repository for PostgresqlClient {
43
44
async fn insert_news ( & self , inputs : Vec < InsertNewsInput > ) -> Result < Vec < i32 > > {
45
+ let total_article_ids: Vec < String > = inputs. iter ( ) . map ( |i| i. article_id . clone ( ) ) . collect ( ) ;
44
46
let values: Vec < InsertNewsValue > = inputs
45
47
. into_iter ( )
46
48
. map ( |input| InsertNewsValue {
@@ -54,11 +56,20 @@ impl Repository for PostgresqlClient {
54
56
published_time : input. published_time ,
55
57
} )
56
58
. collect ( ) ;
57
- let news_ids = diesel:: insert_into ( news:: table)
59
+ let ( inserted_news_ids , inserted_article_ids ) : ( Vec < i32 > , Vec < String > ) = diesel:: insert_into ( news:: table)
58
60
. values ( & values)
59
61
. on_conflict_do_nothing ( )
60
- . returning ( news:: id)
61
- . get_results ( & mut self . pool . get ( ) ?) ?;
62
- Ok ( news_ids)
62
+ . returning ( ( news:: id, article_id) )
63
+ . get_results :: < ( i32 , String ) > ( & mut self . pool . get ( ) ?) ?
64
+ . into_iter ( )
65
+ . unzip ( ) ;
66
+ let ignored_article_ids: Vec < String > = total_article_ids
67
+ . into_iter ( )
68
+ . filter ( |i| !inserted_article_ids. contains ( i) )
69
+ . collect ( ) ;
70
+ if ignored_article_ids. len ( ) > 0 {
71
+ info ! ( "ignored_article_ids={:?}" , ignored_article_ids) ;
72
+ }
73
+ Ok ( inserted_news_ids)
63
74
}
64
75
}
0 commit comments