From 488bce0792c75aa6d9c05e2043181f84b841261b Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:36:00 +0100 Subject: [PATCH] Some work on the sqlite store --- crates/store/src/sqlite_store.rs | 97 ++++++++++++++++++++++++------ migrations/20231006125208_init.sql | 3 +- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/crates/store/src/sqlite_store.rs b/crates/store/src/sqlite_store.rs index 0f49a99..30cd95d 100644 --- a/crates/store/src/sqlite_store.rs +++ b/crates/store/src/sqlite_store.rs @@ -1,54 +1,113 @@ -use std::sync::Arc; - use anyhow::{anyhow, Result}; use async_trait::async_trait; use sqlx::SqlitePool; -use crate::calendar::{Calendar, CalendarStore}; +use crate::{ + calendar::{Calendar, CalendarStore}, + event::Event, +}; #[derive(Debug)] pub struct SqliteCalendarStore { - db: Arc, + db: SqlitePool, } impl SqliteCalendarStore { - pub fn new(db: Arc) -> Self { + pub fn new(db: SqlitePool) -> Self { Self { db } } } +#[derive(Debug, Clone)] +struct EventRow { + uid: String, + ics: String, +} + +impl TryFrom for Event { + type Error = anyhow::Error; + + fn try_from(value: EventRow) -> Result { + Event::from_ics(value.uid, value.ics) + } +} + #[async_trait] impl CalendarStore for SqliteCalendarStore { async fn get_calendar(&self, id: &str) -> Result { - let a = sqlx::query_as!( + let cal = sqlx::query_as!( Calendar, "SELECT id, name, owner, description, color, timezone FROM calendars WHERE id = ?", id - ); - Err(anyhow!("ok wow")) + ) + .fetch_one(&self.db) + .await?; + Ok(cal) } async fn get_calendars(&self, _owner: &str) -> Result> { - Err(anyhow!("ok wow")) + let cals = sqlx::query_as!( + Calendar, + "SELECT id, name, owner, description, color, timezone FROM calendars" + ) + .fetch_all(&self.db) + .await?; + Ok(cals) } - async fn insert_calendar(&mut self, _cid: String, _calendar: Calendar) -> Result<()> { - Err(anyhow!("ok wow")) + async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<()> { + // TODO: :( + let name = calendar.name; + let description = calendar.description; + let owner = calendar.owner; + let color = calendar.color; + let timezone = calendar.timezone; + sqlx::query!("INSERT INTO calendars (id, name, description, owner, color, timezone) VALUES (?, ?, ?, ?, ?, ?)", cid, name, description, owner, color, timezone).execute(&self.db).await?; + Ok(()) } - async fn get_events(&self, _cid: &str) -> Result> { - Err(anyhow!("ok wow")) + async fn get_events(&self, cid: &str) -> Result> { + let events = sqlx::query_as!(EventRow, "SELECT uid, ics FROM events WHERE cid = ?", cid) + .fetch_all(&self.db) + .await? + .iter_mut() + // TODO: this is an ugly bodge :( + .filter_map(|row| row.clone().try_into().ok()) + .collect(); + Ok(events) } - async fn get_event(&self, _cid: &str, _uid: &str) -> Result { - Err(anyhow!("ok wow")) + async fn get_event(&self, cid: &str, uid: &str) -> Result { + let event = sqlx::query_as!( + EventRow, + "SELECT uid, ics FROM events where cid = ? AND uid = ?", + cid, + uid + ) + .fetch_one(&self.db) + .await? + .try_into()?; + Ok(event) } - async fn upsert_event(&mut self, _cid: String, _uid: String, _ics: String) -> Result<()> { - Err(anyhow!("ok wow")) + async fn upsert_event(&mut self, cid: String, uid: String, ics: String) -> Result<()> { + // Do this extra step to ensure that the input is actually valid + let _ = Event::from_ics(uid.to_owned(), ics.to_owned())?; + sqlx::query!( + "INSERT INTO events (cid, uid, ics) VALUES (?, ?, ?)", + cid, + uid, + ics, + ) + .execute(&self.db) + .await?; + Ok(()) } - async fn delete_event(&mut self, _cid: &str, _uid: &str) -> Result<()> { - Err(anyhow!("ok wow")) + async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<()> { + sqlx::query!("DELETE FROM events WHERE cid = ? AND uid = ?", cid, uid) + .execute(&self.db) + .await?; + Ok(()) } } diff --git a/migrations/20231006125208_init.sql b/migrations/20231006125208_init.sql index 7d780a4..5b1abfd 100644 --- a/migrations/20231006125208_init.sql +++ b/migrations/20231006125208_init.sql @@ -10,7 +10,8 @@ CREATE TABLE calendars ( CREATE TABLE events ( uid TEXT NOT NULL, cid TEXT NOT NULL, + ics TEXT NOT NULL, PRIMARY KEY (cid, uid), - FOREIGN KEY (cid) REFERENCES calendars(uid) + FOREIGN KEY (cid) REFERENCES calendars(id) );