Set up some routes using new Rust API

This commit is contained in:
Ben Grant 2023-05-13 13:46:23 +10:00
parent fdc58b428b
commit fc8e2a4360
26 changed files with 703 additions and 134 deletions

View file

@ -0,0 +1,9 @@
[package]
name = "common"
description = "Shared 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,29 @@
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: Send + Sync {
type Error: Error;
async fn get_stats(&self) -> Result<Stats, Self::Error>;
async fn increment_stat_event_count(&self) -> Result<i32, Self::Error>;
async fn increment_stat_person_count(&self) -> Result<i32, Self::Error>;
async fn get_people(&self, event_id: String) -> Result<Option<Vec<Person>>, Self::Error>;
async fn upsert_person(&self, event_id: String, person: Person) -> Result<Person, Self::Error>;
async fn get_event(&self, id: String) -> Result<Option<Event>, Self::Error>;
async fn create_event(&self, event: Event) -> Result<Event, Self::Error>;
/// Delete an event as well as all related people
async fn delete_event(&self, id: String) -> Result<EventDeletion, Self::Error>;
}

View file

@ -0,0 +1,17 @@
use chrono::{DateTime, Utc};
pub struct Event {
pub id: String,
pub name: String,
pub created_at: DateTime<Utc>,
pub visited_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,
}

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,
}