Initial commit

This commit is contained in:
Lennart
2023-09-04 13:20:13 +02:00
commit ccb09f40b4
26 changed files with 10064 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
use crate::Error;
use actix_web::web::{Data, Path};
use actix_web::HttpResponse;
use rustical_store::calendar::CalendarStore;
use tokio::sync::RwLock;
pub async fn delete_event<C: CalendarStore>(
store: Data<RwLock<C>>,
path: Path<(String, String, String)>,
) -> Result<HttpResponse, Error> {
let (_principal, mut cid, uid) = path.into_inner();
if cid.ends_with(".ics") {
cid.truncate(cid.len() - 4);
}
store
.write()
.await
.delete_event(&uid)
.await
.map_err(|_e| Error::InternalError)?;
Ok(HttpResponse::Ok().body(""))
}
pub async fn get_event<C: CalendarStore>(
store: Data<RwLock<C>>,
path: Path<(String, String, String)>,
) -> Result<HttpResponse, Error> {
let (_principal, mut cid, uid) = path.into_inner();
if cid.ends_with(".ics") {
cid.truncate(cid.len() - 4);
}
let event = store
.read()
.await
.get_event(&uid)
.await
.map_err(|_e| Error::NotFound)?;
Ok(HttpResponse::Ok()
.insert_header(("ETag", event.get_etag()))
.body(event.to_ics().to_string()))
}
pub async fn put_event<C: CalendarStore>(
store: Data<RwLock<C>>,
path: Path<(String, String, String)>,
body: String,
) -> Result<HttpResponse, Error> {
let (_principal, mut cid, uid) = path.into_inner();
// Incredibly bodged method of normalising the uid but works for a prototype
if cid.ends_with(".ics") {
cid.truncate(cid.len() - 4);
}
dbg!(&body);
store
.write()
.await
.upsert_event(uid, body)
.await
.map_err(|_e| Error::InternalError)?;
Ok(HttpResponse::Ok().body(""))
}