Skip to content

Commit

Permalink
Use ulid for tag and recipe ids
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenrayhorn committed Jun 8, 2024
1 parent 01699a6 commit a845470
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 190 deletions.
35 changes: 23 additions & 12 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ serde_json = "1.0.117"
cookie = "0.18.1"
sha256 = "1.5.0"
toml = "0.8.13"
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
chrono = { version = "0.4.38", default-features = false, features = [
"now",
"serde",
Expand All @@ -36,3 +35,7 @@ sea-query = { version = "0.30.7", default-features = false, features = [
"backend-sqlite",
"derive",
] }
ulid = { version = "1.1.2", default-features = false, features = [
"std",
"serde",
] }
18 changes: 11 additions & 7 deletions server/src/core/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub async fn create(
datastore: &Pool,
user: domain::user::Authenticated,
recipe: CreatingRecipe,
) -> Result<uuid::Uuid, Error> {
) -> Result<domain::recipe::Id, Error> {
let document = RecipeDocument {
title: recipe.title.into(),
instructions: recipe.instructions.into(),
Expand All @@ -17,10 +17,10 @@ pub async fn create(
tag_ids: recipe.tag_ids,
};

let id = uuid::Uuid::new_v4();
let id = domain::recipe::Id::new();

datastore
.create_recipe(id.to_string(), user.id, document)
.create_recipe(id.clone().into(), user.id, document)
.await
.map_err(|err| Error::Other(err.into()))?;

Expand All @@ -39,10 +39,14 @@ pub async fn update(
notes: recipe.notes.map(std::convert::Into::into),
tag_ids: recipe.tag_ids,
};
let id = recipe.id.to_string();

datastore
.update_recipe(id, user.id, document, recipe.previous_hash)
.update_recipe(
recipe.id.clone().into(),
user.id,
document,
recipe.previous_hash,
)
.await
.map_err(|err| match err {
datastore::Error::NotFound => {
Expand All @@ -54,9 +58,9 @@ pub async fn update(
Ok(())
}

pub async fn get(datastore: &Pool, id: uuid::Uuid) -> Result<Recipe, Error> {
pub async fn get(datastore: &Pool, id: domain::recipe::Id) -> Result<Recipe, Error> {
datastore
.get_recipe(id.to_string())
.get_recipe(id.clone().into())
.await
.map_err(|err| match err {
datastore::Error::NotFound => Error::NotFound(format!("recipe {id} does not exist")),
Expand Down
20 changes: 3 additions & 17 deletions server/src/core/tag.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::{core::Error, datastore::Pool, domain};

fn validation_to_other(err: domain::ValidationError) -> Error {
Error::Other(err.into())
}

pub async fn create(
datastore: &Pool,
user: domain::user::Authenticated,
tag: domain::tag::Creating,
) -> Result<i64, Error> {
) -> Result<domain::tag::Id, Error> {
let id = datastore
.create_tag(user.id, tag.name.into())
.await
Expand All @@ -18,18 +14,8 @@ pub async fn create(
}

pub async fn get_all(datastore: &Pool) -> Result<Vec<domain::tag::Tag>, Error> {
let result = datastore
datastore
.get_tags()
.await
.map_err(|err| Error::Other(err.into()))?;

result
.into_iter()
.map(|tag| {
Ok(domain::tag::Tag {
id: tag.id,
name: tag.name.try_into().map_err(validation_to_other)?,
})
})
.collect()
.map_err(|err| Error::Other(err.into()))
}
6 changes: 2 additions & 4 deletions server/src/core/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use uuid::Uuid;

use crate::{
core::Error,
datastore::{self, Pool},
domain::{RegisteringUser, SessionKey},
domain::{self, RegisteringUser, SessionKey},
oidc,
session_store::SessionStore,
};
Expand All @@ -28,7 +26,7 @@ pub async fn on_authenticated(
authenticated: &oidc::Authenticated,
) -> Result<SessionKey, Error> {
let registering = RegisteringUser {
potential_id: Uuid::new_v4().to_string(),
potential_id: domain::user::Id::new().into(),
oauth_id: format!("custom|{}", authenticated.subject),
name: authenticated.name.to_string(),
};
Expand Down
26 changes: 9 additions & 17 deletions server/src/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct RecipeDocument {
pub ingredients: String,
pub instructions: String,
pub notes: Option<String>,
pub tag_ids: Vec<i64>,
pub tag_ids: Vec<domain::tag::Id>,
}

#[derive(Debug, Clone)]
Expand All @@ -22,12 +22,6 @@ pub struct HashedRecipeDocument {
pub hash: String,
}

#[derive(Debug, Clone)]
pub struct Tag {
pub id: i64,
pub name: String,
}

// Error

#[derive(Error, Debug)]
Expand All @@ -51,12 +45,6 @@ impl From<domain::ValidationError> for Error {
}
}

impl From<uuid::Error> for Error {
fn from(value: uuid::Error) -> Self {
Error::Unknown(value.into())
}
}

impl From<tokio::sync::oneshot::error::RecvError> for Error {
fn from(value: tokio::sync::oneshot::error::RecvError) -> Self {
Error::Unknown(anyhow!(value))
Expand Down Expand Up @@ -226,7 +214,11 @@ impl Pool {
}

// tags
pub async fn create_tag(&self, user_id: String, name: String) -> Result<i64, Error> {
pub async fn create_tag(
&self,
user_id: String,
name: String,
) -> Result<domain::tag::Id, Error> {
let (tx, rx) = oneshot::channel();
let msg = Message::CreateTag {
user_id,
Expand All @@ -237,7 +229,7 @@ impl Pool {
self.send_message(rx, msg).await
}

pub async fn get_tags(&self) -> Result<Vec<Tag>, Error> {
pub async fn get_tags(&self) -> Result<Vec<domain::tag::Tag>, Error> {
let (tx, rx) = oneshot::channel();
let msg = Message::GetTags { respond_to: tx };

Expand Down Expand Up @@ -305,11 +297,11 @@ pub enum Message {

// tags
GetTags {
respond_to: oneshot::Sender<Result<Vec<Tag>, Error>>,
respond_to: oneshot::Sender<Result<Vec<domain::tag::Tag>, Error>>,
},
CreateTag {
user_id: String,
name: String,
respond_to: oneshot::Sender<Result<i64, Error>>,
respond_to: oneshot::Sender<Result<domain::tag::Id, Error>>,
},
}
Loading

0 comments on commit a845470

Please sign in to comment.