store: Add update_calendar method

This commit is contained in:
Lennart
2024-06-16 13:01:53 +02:00
parent d647e45e5c
commit 69d6778193
3 changed files with 28 additions and 0 deletions

View File

@@ -67,6 +67,23 @@ impl CalendarStore for SqliteCalendarStore {
Ok(()) 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> { async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> {
sqlx::query!("DELETE FROM calendars WHERE id = ?", cid) sqlx::query!("DELETE FROM calendars WHERE id = ?", cid)
.execute(&self.db) .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> { 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 // Do this extra step to ensure that the input is actually valid
let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?; let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?;
sqlx::query!( sqlx::query!(

View File

@@ -8,6 +8,7 @@ use crate::{calendar::Calendar, event::Event};
pub trait CalendarStore: Send + Sync + 'static { pub trait CalendarStore: Send + Sync + 'static {
async fn get_calendar(&self, id: &str) -> Result<Calendar, Error>; async fn get_calendar(&self, id: &str) -> Result<Calendar, Error>;
async fn get_calendars(&self, owner: &str) -> Result<Vec<Calendar>, Error>; async fn get_calendars(&self, owner: &str) -> Result<Vec<Calendar>, 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 insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error>;
async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error>; async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error>;

View File

@@ -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> { async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> {
self.events.remove(cid); self.events.remove(cid);
self.save().await.unwrap(); self.save().await.unwrap();