diff --git a/crates/caldav/src/calendar_object/methods.rs b/crates/caldav/src/calendar_object/methods.rs index 6d64281..7c06067 100644 --- a/crates/caldav/src/calendar_object/methods.rs +++ b/crates/caldav/src/calendar_object/methods.rs @@ -6,6 +6,7 @@ use actix_web::web::{Data, Path}; use actix_web::HttpRequest; use actix_web::HttpResponse; use rustical_auth::{AuthInfoExtractor, CheckAuthentication}; +use rustical_store::model::CalendarObject; use rustical_store::CalendarStore; pub async fn get_event( @@ -94,7 +95,8 @@ pub async fn put_event( } } - store.put_object(principal, cid, uid, body).await?; + let object = CalendarObject::from_ics(uid, body)?; + store.put_object(principal, cid, object).await?; Ok(HttpResponse::Created().body("")) } diff --git a/crates/store/src/calendar_store.rs b/crates/store/src/calendar_store.rs index d1bfcbf..a39b253 100644 --- a/crates/store/src/calendar_store.rs +++ b/crates/store/src/calendar_store.rs @@ -43,8 +43,7 @@ pub trait CalendarStore: Send + Sync + 'static { &mut self, principal: String, cid: String, - uid: String, - ics: String, + object: CalendarObject, ) -> Result<(), Error>; async fn delete_object( &mut self, diff --git a/crates/store/src/sqlite_store.rs b/crates/store/src/sqlite_store.rs index e9a7518..64b93be 100644 --- a/crates/store/src/sqlite_store.rs +++ b/crates/store/src/sqlite_store.rs @@ -222,31 +222,24 @@ impl CalendarStore for SqliteCalendarStore { &mut self, principal: String, cid: String, - uid: String, - ics: String, + object: CalendarObject, ) -> Result<(), Error> { let mut tx = self.db.begin().await?; - // input validation - CalendarObject::from_ics(uid.to_owned(), ics.to_owned())?; + let (uid, ics) = (object.get_uid(), object.get_ics()); + sqlx::query!( "REPLACE INTO calendarobjects (principal, cid, uid, ics) VALUES (?, ?, ?, ?)", principal, cid, uid, - ics, + ics ) .execute(&mut *tx) .await?; - log_object_operation( - &mut tx, - &principal, - &cid, - &uid, - CalendarChangeOperation::Add, - ) - .await?; + log_object_operation(&mut tx, &principal, &cid, uid, CalendarChangeOperation::Add).await?; + tx.commit().await?; Ok(()) } diff --git a/crates/store/tests/test_calendar.rs b/crates/store/tests/test_calendar.rs index 7be3088..fd5a53b 100644 --- a/crates/store/tests/test_calendar.rs +++ b/crates/store/tests/test_calendar.rs @@ -1,7 +1,7 @@ use rstest::rstest; use rstest_reuse::{self, apply, template}; use rustical_store::sqlite_store::create_test_store; -use rustical_store::CalendarStore; +use rustical_store::{model::CalendarObject, CalendarStore}; const TIMEZONE: &str = include_str!("examples/timezone.ics"); const EVENT: &str = include_str!("examples/event.ics"); @@ -36,13 +36,9 @@ async fn test_create_event(mut store: CS) { .await .unwrap(); + let object = CalendarObject::from_ics("asd".to_owned(), EVENT.to_owned()).unwrap(); store - .put_object( - "testuser".to_owned(), - "test".to_owned(), - "asd".to_owned(), - EVENT.to_owned(), - ) + .put_object("testuser".to_owned(), "test".to_owned(), object) .await .unwrap();