mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 05:52:19 +00:00
Fix data model to fix event collisions with multiple principals
This commit is contained in:
@@ -32,10 +32,13 @@ impl TryFrom<EventRow> for Event {
|
||||
|
||||
#[async_trait]
|
||||
impl CalendarStore for SqliteCalendarStore {
|
||||
async fn get_calendar(&self, id: &str) -> Result<Calendar, Error> {
|
||||
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> {
|
||||
let cal = sqlx::query_as!(
|
||||
Calendar,
|
||||
r#"SELECT id, name, owner, "order", description, color, timezone FROM calendars WHERE id = ?"#,
|
||||
r#"SELECT principal, id, "order", displayname, description, color, timezone, deleted_at
|
||||
FROM calendars
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
id
|
||||
)
|
||||
.fetch_one(&self.db)
|
||||
@@ -43,40 +46,54 @@ impl CalendarStore for SqliteCalendarStore {
|
||||
Ok(cal)
|
||||
}
|
||||
|
||||
async fn get_calendars(&self, _owner: &str) -> Result<Vec<Calendar>, Error> {
|
||||
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
|
||||
let cals = sqlx::query_as!(
|
||||
Calendar,
|
||||
r#"SELECT id, name, owner, "order", description, color, timezone FROM calendars"#,
|
||||
r#"SELECT principal, id, displayname, "order", description, color, timezone, deleted_at
|
||||
FROM calendars
|
||||
WHERE principal = ? AND deleted_at IS NULL"#,
|
||||
principal
|
||||
)
|
||||
.fetch_all(&self.db)
|
||||
.await?;
|
||||
Ok(cals)
|
||||
}
|
||||
|
||||
async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> {
|
||||
async fn insert_calendar(&mut self, calendar: Calendar) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r#"INSERT INTO calendars (id, name, description, owner, "order", color, timezone) VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
||||
cid,
|
||||
calendar.name,
|
||||
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)"#,
|
||||
calendar.principal,
|
||||
calendar.id,
|
||||
calendar.displayname,
|
||||
calendar.description,
|
||||
calendar.owner,
|
||||
calendar.order,
|
||||
calendar.color,
|
||||
calendar.timezone
|
||||
).execute(&self.db).await?;
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> {
|
||||
async fn update_calendar(
|
||||
&mut self,
|
||||
principal: String,
|
||||
id: String,
|
||||
calendar: Calendar,
|
||||
) -> Result<(), Error> {
|
||||
let result = sqlx::query!(
|
||||
r#"UPDATE calendars SET name = ?, description = ?, owner = ?, "order" = ?, color = ?, timezone = ? WHERE id = ?"#,
|
||||
calendar.name,
|
||||
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
calendar.principal,
|
||||
calendar.id,
|
||||
calendar.displayname,
|
||||
calendar.description,
|
||||
calendar.owner,
|
||||
calendar.order,
|
||||
calendar.color,
|
||||
calendar.timezone,
|
||||
cid,
|
||||
principal,
|
||||
id
|
||||
).execute(&self.db).await?;
|
||||
if result.rows_affected() == 0 {
|
||||
return Err(Error::NotFound);
|
||||
@@ -84,26 +101,66 @@ impl CalendarStore for SqliteCalendarStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> {
|
||||
sqlx::query!("DELETE FROM calendars WHERE id = ?", cid)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
// Does not actually delete the calendar but just disables it
|
||||
async fn delete_calendar(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
id: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error> {
|
||||
match use_trashbin {
|
||||
true => {
|
||||
sqlx::query!(
|
||||
r#"UPDATE calendars SET deleted_at = datetime() WHERE (principal, id) = (?, ?)"#,
|
||||
principal, id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
false => {
|
||||
sqlx::query!(
|
||||
r#"DELETE FROM calendars WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_events(&self, cid: &str) -> Result<Vec<Event>, Error> {
|
||||
sqlx::query_as!(EventRow, "SELECT uid, ics FROM events WHERE cid = ?", cid)
|
||||
.fetch_all(&self.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| row.try_into())
|
||||
.collect()
|
||||
// Does not actually delete the calendar but just disables it
|
||||
async fn restore_calendar(&mut self, principal: &str, id: &str) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r"UPDATE calendars SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
|
||||
principal,
|
||||
id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_event(&self, cid: &str, uid: &str) -> Result<Event, Error> {
|
||||
async fn get_events(&self, principal: &str, cid: &str) -> Result<Vec<Event>, Error> {
|
||||
sqlx::query_as!(
|
||||
EventRow,
|
||||
"SELECT uid, ics FROM events WHERE principal = ? AND cid = ?",
|
||||
principal,
|
||||
cid
|
||||
)
|
||||
.fetch_all(&self.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| row.try_into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn get_event(&self, principal: &str, cid: &str, uid: &str) -> Result<Event, Error> {
|
||||
let event = sqlx::query_as!(
|
||||
EventRow,
|
||||
"SELECT uid, ics FROM events where cid = ? AND uid = ?",
|
||||
"SELECT uid, ics FROM events WHERE (principal, cid, uid) = (?, ?, ?)",
|
||||
principal,
|
||||
cid,
|
||||
uid
|
||||
)
|
||||
@@ -113,10 +170,17 @@ impl CalendarStore for SqliteCalendarStore {
|
||||
Ok(event)
|
||||
}
|
||||
|
||||
async fn put_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error> {
|
||||
async fn put_event(
|
||||
&mut self,
|
||||
principal: String,
|
||||
cid: String,
|
||||
uid: String,
|
||||
ics: String,
|
||||
) -> Result<(), Error> {
|
||||
let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?;
|
||||
sqlx::query!(
|
||||
"REPLACE INTO events (cid, uid, ics) VALUES (?, ?, ?)",
|
||||
"REPLACE INTO events (principal, cid, uid, ics) VALUES (?, ?, ?, ?)",
|
||||
principal,
|
||||
cid,
|
||||
uid,
|
||||
ics,
|
||||
@@ -126,10 +190,42 @@ impl CalendarStore for SqliteCalendarStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<(), Error> {
|
||||
sqlx::query!("DELETE FROM events WHERE cid = ? AND uid = ?", cid, uid)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
async fn delete_event(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
uid: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error> {
|
||||
match use_trashbin {
|
||||
true => {
|
||||
sqlx::query!(
|
||||
"UPDATE events SET deleted_at = datetime() WHERE (principal, cid, uid) = (?, ?, ?)",
|
||||
principal,
|
||||
cid,
|
||||
uid
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
false => {
|
||||
sqlx::query!("DELETE FROM events WHERE cid = ? AND uid = ?", cid, uid)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn restore_event(&mut self, principal: &str, cid: &str, uid: &str) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r#"UPDATE events SET deleted_at = NULL WHERE (principal, cid, uid) = (?, ?, ?)"#,
|
||||
principal,
|
||||
cid,
|
||||
uid
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user