diff --git a/crates/store/src/sqlite_store.rs b/crates/store/src/sqlite_store.rs index faeb581..07f8ca2 100644 --- a/crates/store/src/sqlite_store.rs +++ b/crates/store/src/sqlite_store.rs @@ -67,6 +67,23 @@ impl CalendarStore for SqliteCalendarStore { Ok(()) } + async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> { + let result = sqlx::query!( + r#"UPDATE calendars SET name = ?, description = ?, owner = ?, "order" = ?, color = ?, timezone = ? WHERE id = ?"#, + calendar.name, + calendar.description, + calendar.owner, + calendar.order, + calendar.color, + calendar.timezone, + cid, + ).execute(&self.db).await?; + if result.rows_affected() == 0 { + return Err(Error::NotFound); + } + Ok(()) + } + async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> { sqlx::query!("DELETE FROM calendars WHERE id = ?", cid) .execute(&self.db) @@ -97,6 +114,7 @@ impl CalendarStore for SqliteCalendarStore { } async fn upsert_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error> { + // TODO: This is not actually an upsert // Do this extra step to ensure that the input is actually valid let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?; sqlx::query!( diff --git a/crates/store/src/store.rs b/crates/store/src/store.rs index 6436e29..f1e18ad 100644 --- a/crates/store/src/store.rs +++ b/crates/store/src/store.rs @@ -8,6 +8,7 @@ use crate::{calendar::Calendar, event::Event}; pub trait CalendarStore: Send + Sync + 'static { async fn get_calendar(&self, id: &str) -> Result; async fn get_calendars(&self, owner: &str) -> Result, Error>; + async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>; async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>; async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error>; diff --git a/crates/store/src/toml_store.rs b/crates/store/src/toml_store.rs index dbeba71..55b79a1 100644 --- a/crates/store/src/toml_store.rs +++ b/crates/store/src/toml_store.rs @@ -66,6 +66,15 @@ impl CalendarStore for TomlCalendarStore { } } + async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> { + if let None = self.calendars.remove(&cid) { + // No old calendar to update, for consistency reasons throw an error + return Err(Error::NotFound); + } + self.calendars.insert(cid, calendar); + Ok(()) + } + async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> { self.events.remove(cid); self.save().await.unwrap();