diff --git a/crates/store_sqlite/src/addressbook_store.rs b/crates/store_sqlite/src/addressbook_store.rs index 0ca74df..9233ef7 100644 --- a/crates/store_sqlite/src/addressbook_store.rs +++ b/crates/store_sqlite/src/addressbook_store.rs @@ -275,20 +275,30 @@ impl AddressbookStore for SqliteStore { principal: String, addressbook_id: String, object: AddressObject, - // TODO: implement overwrite: bool, ) -> Result<(), rustical_store::Error> { let mut tx = self.db.begin().await.map_err(crate::Error::from)?; let (object_id, vcf) = (object.get_id(), object.get_vcf()); - sqlx::query!( + (if overwrite { + sqlx::query!( "REPLACE INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)", principal, addressbook_id, object_id, vcf ) + } else { + // If the object already exists a database error is thrown and handled in error.rs + sqlx::query!( + "INSERT INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)", + principal, + addressbook_id, + object_id, + vcf + ) + }) .execute(&mut *tx) .await .map_err(crate::Error::from)?; diff --git a/crates/store_sqlite/src/calendar_store.rs b/crates/store_sqlite/src/calendar_store.rs index a2305cf..aed9899 100644 --- a/crates/store_sqlite/src/calendar_store.rs +++ b/crates/store_sqlite/src/calendar_store.rs @@ -223,20 +223,29 @@ impl CalendarStore for SqliteStore { principal: String, cal_id: String, object: CalendarObject, - // TODO: implement overwrite: bool, ) -> Result<(), Error> { let mut tx = self.db.begin().await.map_err(crate::Error::from)?; let (object_id, ics) = (object.get_id(), object.get_ics()); - sqlx::query!( - "REPLACE INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)", - principal, - cal_id, - object_id, - ics - ) + (if overwrite { + sqlx::query!( + "REPLACE INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)", + principal, + cal_id, + object_id, + ics + ) + } else { + sqlx::query!( + "INSERT INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)", + principal, + cal_id, + object_id, + ics + ) + }) .execute(&mut *tx) .await .map_err(crate::Error::from)?; diff --git a/crates/store_sqlite/src/error.rs b/crates/store_sqlite/src/error.rs index b93ad4f..b372696 100644 --- a/crates/store_sqlite/src/error.rs +++ b/crates/store_sqlite/src/error.rs @@ -11,6 +11,13 @@ impl From for Error { fn from(value: sqlx::Error) -> Self { match value { sqlx::Error::RowNotFound => Error::StoreError(rustical_store::Error::NotFound), + sqlx::Error::Database(err) => { + if err.is_unique_violation() { + Error::StoreError(rustical_store::Error::AlreadyExists) + } else { + Error::SqlxError(sqlx::Error::Database(err)) + } + } err => Error::SqlxError(err), } }