Skip to content

Commit

Permalink
switched calendar.entry_type from an enum to a string
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jul 10, 2024
1 parent 13872bd commit 67a054a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 40 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion server/main-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ futures = "0.3.30"
unicode-truncate = { git = "https://github.com/CommanderStorm/unicode-truncate.git", rev = "5cc7798"}

# database
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio", "tls-rustls", "migrate", "macros", "chrono"] }
# v0.8 needs https://github.com/launchbadge/sqlx/issues/3316 is resolved first
sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio", "tls-rustls", "migrate", "macros", "chrono", "json"] }
chrono = { version = "0.4.38", default-features = false, features = ["serde"] }

# search
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
DELETE FROM calendar WHERE 1=1 -- to make migrating simpler and because it is possible
alter table calendar alter column entry_type type entry_type;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add up migration script here
alter table calendar alter column entry_type type text using entry_type::text
12 changes: 6 additions & 6 deletions server/main-api/src/calendar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async fn get_from_db(
) -> Result<LimitedHashMap<String, LocationEvents>, crate::BoxedError> {
let mut located_events: HashMap<String, LocationEvents> = HashMap::new();
for location in locations {
let events = sqlx::query_as!(Event, r#"SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type AS "entry_type!:crate::calendar::models::EventType",detailed_entry_type
let events = sqlx::query_as!(Event, r#"SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type,detailed_entry_type
FROM calendar
WHERE room_code = $1 AND start_at >= $2 AND end_at <= $3"#,
location.key, start_after, end_before).fetch_all(pool).await?;
Expand Down Expand Up @@ -202,7 +202,7 @@ mod tests {
stp_title_de: "Quantenteleportation".into(),
stp_title_en: "Quantum teleportation".into(),
stp_type: "Vorlesung mit Zentralübung".into(),
entry_type: models::EventType::Lecture,
entry_type: models::EventType::Lecture.to_string(),
detailed_entry_type: "Abhaltung".into(),
},
Event {
Expand All @@ -213,7 +213,7 @@ mod tests {
stp_title_de: "Quantenteleportation 2".into(),
stp_title_en: "Quantum teleportation 2".into(),
stp_type: "Vorlesung mit Zentralübung".into(),
entry_type: models::EventType::Lecture,
entry_type: models::EventType::Lecture.to_string(),
detailed_entry_type: "Abhaltung".into(),
},
Event {
Expand All @@ -224,7 +224,7 @@ mod tests {
stp_title_de: "Wartung".into(),
stp_title_en: "maintenance".into(),
stp_type: "Vorlesung mit Zentralübung".into(),
entry_type: models::EventType::Barred,
entry_type: models::EventType::Barred.to_string(),
detailed_entry_type: "Abhaltung".into(),
},
Event {
Expand All @@ -235,7 +235,7 @@ mod tests {
stp_title_de: "Quantenteleportation 3".into(),
stp_title_en: "Quantum teleportation 3".into(),
stp_type: "Vorlesung".into(),
entry_type: models::EventType::Other,
entry_type: models::EventType::Other.to_string(),
detailed_entry_type: "Abhaltung".into(),
},
Event {
Expand All @@ -246,7 +246,7 @@ mod tests {
stp_title_de: "Quantenteleportation 3".into(),
stp_title_en: "Quantum teleportation 3".into(),
stp_type: "Vorlesung".into(),
entry_type: models::EventType::Exam,
entry_type: models::EventType::Exam.to_string(),
detailed_entry_type: "Abhaltung".into(),
},
],
Expand Down
14 changes: 12 additions & 2 deletions server/main-api/src/calendar/models.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -48,7 +50,8 @@ pub(super) struct Event {
/// e.g. Vorlesung mit Zentralübung
pub(super) stp_type: String,
/// e.g. lecture
pub(super) entry_type: EventType,
/// in reality this is a [EventType]
pub(super) entry_type: String,
/// e.g. Abhaltung
pub(super) detailed_entry_type: String,
}
Expand Down Expand Up @@ -77,7 +80,7 @@ impl Event {
self.stp_title_de,
self.stp_title_en,
self.stp_type,
self.entry_type.clone() as EventType, // see https://github.com/launchbadge/sqlx/issues/1004 => our type is not possible (?)
self.entry_type,
self.detailed_entry_type,
).execute(&mut **tx).await
}
Expand All @@ -94,3 +97,10 @@ pub enum EventType {
Barred,
Other,
}

impl Display for EventType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = serde_json::to_string(self).map_err(|_| std::fmt::Error)?;
f.write_str(&str)
}
}

0 comments on commit 67a054a

Please sign in to comment.