Rename main folders and write sql backend adaptor

This commit is contained in:
Ben Grant 2023-05-11 17:04:17 +10:00
parent 1d34f8e06d
commit fdc58b428b
212 changed files with 3577 additions and 4775 deletions

9
backend/data/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "data"
description = "Structs and traits for the data storage and transfer of Crab Fit"
version = "0.1.0"
edition = "2021"
[dependencies]
async-trait = "0.1.68"
chrono = "0.4.24"

View file

@ -0,0 +1,37 @@
use std::error::Error;
use async_trait::async_trait;
use crate::{
event::{Event, EventDeletion},
person::Person,
stats::Stats,
};
/// Data storage adaptor, all methods on an adaptor can return an error if
/// something goes wrong, or potentially None if the data requested was not found.
#[async_trait]
pub trait Adaptor {
/// Creates a new adaptor and performs all setup required
///
/// # Panics
/// If an error occurs while setting up the adaptor
async fn new() -> Self;
async fn get_stats(&self) -> Result<Stats, Box<dyn Error>>;
async fn increment_stat_event_count(&self) -> Result<i32, Box<dyn Error>>;
async fn increment_stat_person_count(&self) -> Result<i32, Box<dyn Error>>;
async fn get_people(&self, event_id: String) -> Result<Option<Vec<Person>>, Box<dyn Error>>;
async fn upsert_person(
&self,
event_id: String,
person: Person,
) -> Result<Person, Box<dyn Error>>;
async fn get_event(&self, id: String) -> Result<Option<Event>, Box<dyn Error>>;
async fn create_event(&self, event: Event) -> Result<Event, Box<dyn Error>>;
/// Delete an event as well as all related people
async fn delete_event(&self, id: String) -> Result<EventDeletion, Box<dyn Error>>;
}

16
backend/data/src/event.rs Normal file
View file

@ -0,0 +1,16 @@
use chrono::{DateTime, Utc};
pub struct Event {
pub id: String,
pub name: String,
pub created_at: DateTime<Utc>,
pub times: Vec<String>,
pub timezone: String,
}
/// Info about a deleted event
pub struct EventDeletion {
pub id: String,
/// The amount of people that were in this event that were also deleted
pub person_count: u64,
}

4
backend/data/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod adaptor;
pub mod event;
pub mod person;
pub mod stats;

View file

@ -0,0 +1,8 @@
use chrono::{DateTime, Utc};
pub struct Person {
pub name: String,
pub password_hash: Option<String>,
pub created_at: DateTime<Utc>,
pub availability: Vec<String>,
}

View file

@ -0,0 +1,4 @@
pub struct Stats {
pub event_count: i32,
pub person_count: i32,
}