From 1644c9c51c14f16324c8505b63b90ce11ddbee60 Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Thu, 6 Jun 2024 20:43:32 +0100 Subject: [PATCH] db inits --- .gitmodules | 3 ++ dermy-models | 1 + src/account.rs | 2 + src/account/db.rs | 126 ++++++++++++++++++++++++++++++++++++++++++++++ src/db.rs | 66 ------------------------ src/lib.rs | 1 - 6 files changed, 132 insertions(+), 67 deletions(-) create mode 100644 .gitmodules create mode 160000 dermy-models create mode 100644 src/account/db.rs delete mode 100644 src/db.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..377bf41 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "dermy-models"] + path = dermy-models + url = git@vcs.r0r-5chach.xyz:r0r-5chach/dermy-models.git diff --git a/dermy-models b/dermy-models new file mode 160000 index 0000000..ab88613 --- /dev/null +++ b/dermy-models @@ -0,0 +1 @@ +Subproject commit ab886132c76f98b766f81bf7468b60beb419662c diff --git a/src/account.rs b/src/account.rs index 52bb366..e22283d 100644 --- a/src/account.rs +++ b/src/account.rs @@ -3,6 +3,8 @@ use axum::{ routing::{get, post} }; +mod db; + pub fn router() -> Router { Router::new() .nest("/:user_id", user_router()) diff --git a/src/account/db.rs b/src/account/db.rs new file mode 100644 index 0000000..140bd1d --- /dev/null +++ b/src/account/db.rs @@ -0,0 +1,126 @@ +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, + } + } +} diff --git a/src/db.rs b/src/db.rs deleted file mode 100644 index 23724da..0000000 --- a/src/db.rs +++ /dev/null @@ -1,66 +0,0 @@ -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")] - image_path: Option, - location: 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")] - id: Option, - #[serde(rename = "_date_created")] - date_created: DateTime, - contents: String, - #[serde(rename = "_mole_id")] - mole_id: ObjectId -} - -#[derive(Serialize, Deserialize)] -pub struct User { - #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] - id: Option, - #[serde(rename="_auth")] - auth: Auth, - username: String, -} -#[derive(Serialize, Deserialize)] -pub struct Auth { - #[serde(rename = "_hash", skip_serializing_if = "String::is_empty")] - hash: String, - #[serde(rename = "_salt", skip_serializing_if = "String::is_empty")] - salt: String, -} diff --git a/src/lib.rs b/src/lib.rs index ea956cb..6d69af3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ mod account; -mod db; mod model; pub async fn run() {