From dad7e399e2cd16c83a05252d08b4e6e08a286550 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 28 May 2023 21:10:18 +0200 Subject: [PATCH] made sure that room data is saved to the database --- server/calendar/src/models.rs | 2 +- .../src/scrape_task/main_api_connector.rs | 44 ++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/server/calendar/src/models.rs b/server/calendar/src/models.rs index bb38ec48d..4646d613e 100644 --- a/server/calendar/src/models.rs +++ b/server/calendar/src/models.rs @@ -28,7 +28,7 @@ pub struct XMLEvent { pub last_scrape: NaiveDateTime, } -#[derive(Insertable, Queryable, AsChangeset)] +#[derive(Insertable, Queryable, AsChangeset, Clone)] #[diesel(table_name = crate::schema::rooms)] pub struct Room { pub key: String, diff --git a/server/calendar/src/scrape_task/main_api_connector.rs b/server/calendar/src/scrape_task/main_api_connector.rs index 80fab735f..639957911 100644 --- a/server/calendar/src/scrape_task/main_api_connector.rs +++ b/server/calendar/src/scrape_task/main_api_connector.rs @@ -1,3 +1,5 @@ +use chrono::{NaiveDateTime, Utc}; +use diesel::PgConnection; use log::error; use regex::Regex; use serde::Deserialize; @@ -56,8 +58,46 @@ pub async fn get_all_ids() -> Vec { return vec![]; } }; - match rooms { + let rooms: Vec = match rooms { Ok(rooms) => rooms.into_iter().flat_map(Room::from).collect(), Err(e) => panic!("Failed to parse main-api response: {e:#?}"), - } + }; + let start_time = Utc::now().naive_utc(); + let conn = &mut crate::utils::establish_connection(); + store_in_db(conn, &rooms, &start_time); + delete_stale_results(conn, start_time); + rooms +} + +fn store_in_db(conn: &mut PgConnection, rooms_to_store: &[Room], start_time: &NaiveDateTime) { + use crate::schema::rooms::dsl::*; + use diesel::prelude::*; + rooms_to_store + .iter() + .map(|room| crate::models::Room { + key: room.sap_id.clone(), + tumonline_org_id: room.tumonline_org_id, + tumonline_calendar_id: room.tumonline_calendar_id, + tumonline_room_id: room.tumonline_room_id, + last_scrape: *start_time, + }) + .for_each(|room| { + let res = diesel::insert_into(rooms) + .values(&room) + .on_conflict(key) + .do_update() + .set(&room) + .execute(conn); + if let Err(e) = res { + error!("Error inserting into database: {e:?}"); + } + }); +} +fn delete_stale_results(conn: &mut PgConnection, start_time: NaiveDateTime) { + use crate::schema::rooms::dsl::*; + use diesel::prelude::*; + diesel::delete(rooms) + .filter(last_scrape.le(start_time)) + .execute(conn) + .expect("Failed to delete stale rooms"); }