Skip to content

Commit a233590

Browse files
sontdhustmetalwhale
authored andcommitted
Log the article ids that were ignored when inserting news.
1 parent f7a5b57 commit a233590

File tree

1 file changed

+16
-5
lines changed
  • chloria-backend/chloria-job/src/infrastructure/repository

1 file changed

+16
-5
lines changed

chloria-backend/chloria-job/src/infrastructure/repository/postgresql.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use diesel::{
66
r2d2::{ConnectionManager, Pool},
77
PgConnection,
88
};
9+
use log::info;
910

1011
use crate::{
1112
execution::ports::repository::{InsertNewsInput, Repository},
12-
schema::news,
13+
schema::news::{self, article_id},
1314
};
1415

1516
pub(crate) struct PostgresqlClient {
@@ -41,6 +42,7 @@ struct InsertNewsValue {
4142
#[async_trait]
4243
impl Repository for PostgresqlClient {
4344
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();
4446
let values: Vec<InsertNewsValue> = inputs
4547
.into_iter()
4648
.map(|input| InsertNewsValue {
@@ -54,11 +56,20 @@ impl Repository for PostgresqlClient {
5456
published_time: input.published_time,
5557
})
5658
.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)
5860
.values(&values)
5961
.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)
6374
}
6475
}

0 commit comments

Comments
 (0)