use serde::{Deserialize, Serialize}; use mongodb::{ bson::{DateTime, oid::ObjectId}, Client, Database, error::Error, }; pub async fn get_database_client() -> Result { Ok(Client::with_uri_str("mongodb:://localhost:27017").await?.database("dermy")) } #[derive(Serialize, Deserialize)] pub struct Mole { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] id: Option, #[serde(rename="_user_id")] user_id: ObjectId, #[serde(rename="_image_path", skip_serializing_if = "Option::is_none")] image_path: Option, location: Location, } impl Mole { pub fn new(user_id: ObjectId, location: Location) -> Self { let id = None; let image_path = None; Self { id, user_id, image_path, location, } } pub fn with_image(user_id: ObjectId, location: Location, image_path: String) -> Self { let id = None; let image_path = Some(image_path); Self { id, user_id, image_path, location } } } #[derive(Serialize, Deserialize)] pub enum Location { FrontLocation(FrontLocation), LeftSideLocation(LeftSideLocation), RightSideLocation(RightSideLocation), BackLocation(BackLocation), } #[derive(Serialize, Deserialize)] pub enum FrontLocation {} //TODO: Add front locations #[derive(Serialize, Deserialize)] pub enum LeftSideLocation {} //TODO: Add left side locations #[derive(Serialize, Deserialize)] pub enum RightSideLocation {} //TODO: Add right side locations #[derive(Serialize, Deserialize)] pub enum BackLocation {} //TODO: Add back locations #[derive(Serialize, Deserialize)] pub struct LogEntry { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] pub id: Option, #[serde(rename = "_date_created")] pub date_created: DateTime, pub contents: String, #[serde(rename = "_mole_id")] pub mole_id: ObjectId } impl LogEntry { pub fn new(contents: String, mole_id: ObjectId) -> Self { let id = None; let date_created: DateTime = chrono::Utc::now().into(); Self { id, date_created, contents, mole_id, } } } #[derive(Serialize, Deserialize)] pub struct User { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] pub id: Option, #[serde(rename="_auth")] pub auth: Auth, pub username: String, } impl User { pub fn new(username: String) -> Self { let id = None; let auth = Default::default(); Self { id, auth, username, } } } #[derive(Default, Serialize, Deserialize)] pub struct Auth { #[serde(rename = "_hash", skip_serializing_if = "Option::is_none")] pub hash: Option, #[serde(rename = "_salt", skip_serializing_if = "Option::is_none")] pub salt: Option, } impl Auth { pub fn from_hash(hash: String) -> Self { let salt = None; let hash = Some(hash); Self { hash, salt, } } }