This commit is contained in:
Joshua Perry 2024-06-06 20:43:32 +01:00
parent 3bec1e9d4c
commit 1644c9c51c
6 changed files with 132 additions and 67 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "dermy-models"]
path = dermy-models
url = git@vcs.r0r-5chach.xyz:r0r-5chach/dermy-models.git

1
dermy-models Submodule

@ -0,0 +1 @@
Subproject commit ab886132c76f98b766f81bf7468b60beb419662c

View File

@ -3,6 +3,8 @@ use axum::{
routing::{get, post}
};
mod db;
pub fn router() -> Router {
Router::new()
.nest("/:user_id", user_router())

126
src/account/db.rs Normal file
View File

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

View File

@ -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<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")]
image_path: Option<String>,
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<ObjectId>,
#[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<ObjectId>,
#[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,
}

View File

@ -1,5 +1,4 @@
mod account;
mod db;
mod model;
pub async fn run() {