From 6c4e8863ddd7a43f0f29e4c823fc487a228f44fb Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 6 May 2024 01:33:05 +0200 Subject: [PATCH] tmp --- ...430_fix-calendar-id-not-extracted.down.sql | 43 +++++++++++++++++++ ...24430_fix-calendar-id-not-extracted.up.sql | 20 +++++++++ server/main-api/src/calendar/mod.rs | 11 ++--- server/main-api/src/models.rs | 1 + 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.down.sql create mode 100644 server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.up.sql diff --git a/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.down.sql b/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.down.sql new file mode 100644 index 000000000..d5d5f29ff --- /dev/null +++ b/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.down.sql @@ -0,0 +1,43 @@ +-- Add down migration script here +-- was never used +create table rooms +( + key text primary key not null, + tumonline_org_id integer not null, + tumonline_calendar_id integer not null, + tumonline_room_id integer not null, + last_scrape timestamp without time zone not null +); + +-- migrating to +DROP TABLE en; +create table en +( + key text not null + primary key + references de, + name text not null, + tumonline_room_nr integer, + type text not null, + type_common_name text not null, + lat double precision not null, + lon double precision not null, + data text not null, + last_calendar_scrape_at timestamp with time zone +); +comment on column en.last_calendar_scrape_at is 'the last time the calendar was scraped for this room'; + +DROP TABLE de; +create table de +( + key text not null primary key, + name text not null, + tumonline_room_nr integer, + type text not null, + type_common_name text not null, + lat double precision not null, + lon double precision not null, + data text not null, + last_calendar_scrape_at timestamp with time zone +); +comment on column de.last_calendar_scrape_at is 'the last time the calendar was scraped for this room'; diff --git a/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.up.sql b/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.up.sql new file mode 100644 index 000000000..67ac800ff --- /dev/null +++ b/server/main-api/migrations/20240505224430_fix-calendar-id-not-extracted.up.sql @@ -0,0 +1,20 @@ +-- Add up migration script here +-- was never used +DROP TABLE rooms; + +-- migrating to using the json type instead of having elaborate insertion logic +alter table de alter column data type json using data::json; + +alter table de drop column lat; +alter table de add column lat FLOAT NOT NULL GENERATED ALWAYS AS (CAST (data->'coords'->>'lat' AS FLOAT)) STORED; +alter table de drop column lon; +alter table de add column lon FLOAT NOT NULL GENERATED ALWAYS AS (CAST (data->'coords'->>'lon' AS FLOAT)) STORED; +alter table de drop column name; +alter table de add column name TEXT NOT NULL GENERATED ALWAYS AS (CAST (data->>'name' AS TEXT)) STORED; +alter table de drop column type_common_name; +alter table de add column type_common_name TEXT NOT NULL GENERATED ALWAYS AS (CAST (data->>'type_common_name' AS TEXT)) STORED; +alter table de drop column type; +alter table de add column type TEXT NOT NULL GENERATED ALWAYS AS (CAST (data->>'type' AS TEXT)) STORED; +alter table de drop column calendar_url; +alter table de add column calendar_url TEXT GENERATED ALWAYS AS (CAST (data->'props'->>'calendar_url' AS TEXT)) STORED; + diff --git a/server/main-api/src/calendar/mod.rs b/server/main-api/src/calendar/mod.rs index d81284645..8d4ebfa4b 100644 --- a/server/main-api/src/calendar/mod.rs +++ b/server/main-api/src/calendar/mod.rs @@ -1,4 +1,4 @@ -use actix_web::{get, web, HttpResponse}; +use actix_web::{get, HttpResponse, web}; use chrono::{DateTime, Utc}; use log::error; use serde::Deserialize; @@ -45,10 +45,11 @@ pub async fn calendar_handler( } Ok(Some(loc)) => loc, }; - let calendar_url = format!( - "https://campus.tum.de/tumonline/wbKalender.wbRessource?pResNr={id}", - id = 0 - ); // TODO: room.tumonline_calendar_id + let Some(calendar_url)=location.calendar_url else { + return HttpResponse::NotFound() + .content_type("text/plain") + .body("Room does not have a calendar"); + }; let fetching_strategy = fetch::StrategyExecutor::new(&data.db, &id, &args.start_after, &args.end_before); match fetching_strategy diff --git a/server/main-api/src/models.rs b/server/main-api/src/models.rs index 2f099ae12..03c90a60e 100644 --- a/server/main-api/src/models.rs +++ b/server/main-api/src/models.rs @@ -6,6 +6,7 @@ pub struct Location { pub name: String, pub last_calendar_scrape_at: Option>, pub tumonline_room_nr: Option, + pub calendar_url: Option, pub r#type: String, pub type_common_name: String, pub lat: f64,