dermy-api/src/account/db.rs

127 lines
3.1 KiB
Rust
Raw Normal View History

2024-06-06 19:43:32 +00:00
use serde::{Deserialize, Serialize};
use mongodb::{
bson::{DateTime, oid::ObjectId},
Client,
Database,
error::Error,
};
pub async fn get_database_client() -> Result<Database, Error> {
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<ObjectId>,
#[serde(rename="_user_id")]
user_id: ObjectId,
#[serde(rename="_image_path", skip_serializing_if = "Option::is_none")]
image_path: Option<String>,
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<ObjectId>,
#[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<ObjectId>,
#[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<String>,
#[serde(rename = "_salt", skip_serializing_if = "Option::is_none")]
pub salt: Option<String>,
}
impl Auth {
pub fn from_hash(hash: String) -> Self {
let salt = None;
let hash = Some(hash);
Self {
hash,
salt,
}
}
}