Translation (WIP)

This commit is contained in:
Greg Burri 2025-01-05 22:38:46 +01:00
parent 9b0fcec5e2
commit e9873c1943
29 changed files with 586 additions and 285 deletions

View file

@ -102,8 +102,8 @@ WHERE [Ingredient].[id] = $1 AND [user_id] = $2
.map_err(DBError::from)
}
pub async fn get_recipe(&self, id: i64) -> Result<Option<model::Recipe>> {
sqlx::query_as(
pub async fn get_recipe(&self, id: i64, with_groups: bool) -> Result<Option<model::Recipe>> {
match sqlx::query_as::<_, model::Recipe>(
r#"
SELECT
[id], [user_id], [title], [lang],
@ -114,8 +114,14 @@ FROM [Recipe] WHERE [id] = $1
)
.bind(id)
.fetch_optional(&self.pool)
.await
.map_err(DBError::from)
.await?
{
Some(mut recipe) if with_groups => {
recipe.groups = self.get_groups(id).await?;
Ok(Some(recipe))
}
recipe => Ok(recipe),
}
}
pub async fn create_recipe(&self, user_id: i64) -> Result<i64> {
@ -543,8 +549,6 @@ ORDER BY [name]
#[cfg(test)]
mod tests {
use axum::routing::connect;
use super::*;
#[tokio::test]
@ -555,7 +559,7 @@ mod tests {
let recipe_id = connection.create_recipe(user_id).await?;
connection.set_recipe_title(recipe_id, "Crêpe").await?;
let recipe = connection.get_recipe(recipe_id).await?.unwrap();
let recipe = connection.get_recipe(recipe_id, false).await?.unwrap();
assert_eq!(recipe.title, "Crêpe".to_string());
Ok(())
@ -581,7 +585,7 @@ mod tests {
connection.set_recipe_language(recipe_id, "fr").await?;
connection.set_recipe_is_published(recipe_id, true).await?;
let recipe = connection.get_recipe(recipe_id).await?.unwrap();
let recipe = connection.get_recipe(recipe_id, false).await?.unwrap();
assert_eq!(recipe.id, recipe_id);
assert_eq!(recipe.title, "Ouiche");

View file

@ -76,7 +76,7 @@ FROM [UserLoginToken] WHERE [token] = $1
}
pub async fn load_user(&self, user_id: i64) -> Result<Option<model::User>> {
sqlx::query_as("SELECT [id], [email], [name] FROM [User] WHERE [id] = $1")
sqlx::query_as("SELECT [id], [email], [name], [lang] FROM [User] WHERE [id] = $1")
.bind(user_id)
.fetch_optional(&self.pool)
.await
@ -102,13 +102,14 @@ FROM [UserLoginToken] WHERE [token] = $1
.fetch_one(&mut *tx)
.await?;
let new_email = new_email.map(str::trim);
let email_changed = new_email.is_some_and(|new_email| new_email != email);
// Check if email not already taken.
let validation_token = if email_changed {
if sqlx::query_scalar::<_, i64>(
if sqlx::query_scalar(
r#"
SELECT COUNT(*)
SELECT COUNT(*) > 0
FROM [User]
WHERE [email] = $1
"#,
@ -116,7 +117,6 @@ WHERE [email] = $1
.bind(new_email.unwrap())
.fetch_one(&mut *tx)
.await?
> 0
{
return Ok(UpdateUserResult::EmailAlreadyTaken);
}
@ -148,7 +148,7 @@ WHERE [id] = $1
)
.bind(user_id)
.bind(new_email.unwrap_or(&email))
.bind(new_name.unwrap_or(&name))
.bind(new_name.map(str::trim).unwrap_or(&name))
.bind(hashed_new_password.unwrap_or(hashed_password))
.execute(&mut *tx)
.await?;

View file

@ -7,6 +7,7 @@ pub struct User {
pub id: i64,
pub name: String,
pub email: String,
pub lang: String,
}
#[derive(FromRow)]
@ -30,8 +31,9 @@ pub struct Recipe {
pub servings: Option<u32>,
pub is_published: bool,
// pub tags: Vec<String>,
// pub groups: Vec<Group>,
#[sqlx(skip)]
pub groups: Vec<Group>,
}
#[derive(FromRow)]