Set user language to new recipe + cleaning
This commit is contained in:
parent
8b4b788562
commit
03ebbb74fa
12 changed files with 73 additions and 86 deletions
|
|
@ -1,18 +1,38 @@
|
|||
use super::{Connection, DBError, Result};
|
||||
use crate::{consts, data::model};
|
||||
use crate::data::model;
|
||||
|
||||
use common::ron_api::Difficulty;
|
||||
|
||||
impl Connection {
|
||||
pub async fn get_all_published_recipe_titles(&self) -> Result<Vec<(i64, String)>> {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [title]
|
||||
FROM [Recipe]
|
||||
WHERE [is_published] = true
|
||||
ORDER BY [title]
|
||||
"#,
|
||||
)
|
||||
/// Returns all the recipe titles where recipe is written in the given language.
|
||||
/// If a user_id is given, the language constraint is ignored for recipes owned by user_id.
|
||||
pub async fn get_all_published_recipe_titles(
|
||||
&self,
|
||||
lang: &str,
|
||||
user_id: Option<i64>,
|
||||
) -> Result<Vec<(i64, String)>> {
|
||||
if let Some(user_id) = user_id {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [title]
|
||||
FROM [Recipe]
|
||||
WHERE [is_published] = true AND ([lang] = $1 OR [user_id] = $2)
|
||||
ORDER BY [title]
|
||||
"#,
|
||||
)
|
||||
.bind(lang)
|
||||
.bind(user_id)
|
||||
} else {
|
||||
sqlx::query_as(
|
||||
r#"
|
||||
SELECT [id], [title]
|
||||
FROM [Recipe]
|
||||
WHERE [is_published] = true AND [lang] = $1
|
||||
ORDER BY [title]
|
||||
"#,
|
||||
)
|
||||
.bind(lang)
|
||||
}
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(DBError::from)
|
||||
|
|
@ -147,11 +167,18 @@ WHERE [Recipe].[user_id] = $1
|
|||
{
|
||||
Some(recipe_id) => Ok(recipe_id),
|
||||
None => {
|
||||
let db_result =
|
||||
sqlx::query("INSERT INTO [Recipe] ([user_id], [title]) VALUES ($1, '')")
|
||||
.bind(user_id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
let lang: String = sqlx::query_scalar("SELECT [lang] FROM [User] WHERE [id] = $1")
|
||||
.bind(user_id)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
|
||||
let db_result = sqlx::query(
|
||||
"INSERT INTO [Recipe] ([user_id], [lang], [title]) VALUES ($1, $2, '')",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(lang)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
Ok(db_result.last_insert_rowid())
|
||||
|
|
@ -345,9 +372,6 @@ WHERE [id] = $1 AND [id] NOT IN (
|
|||
}
|
||||
|
||||
pub async fn set_recipe_language(&self, recipe_id: i64, lang: &str) -> Result<()> {
|
||||
if !consts::LANGUAGES.iter().any(|(_, l)| *l == lang) {
|
||||
return Err(DBError::UnknownLanguage(lang.to_string()));
|
||||
}
|
||||
sqlx::query("UPDATE [Recipe] SET [lang] = $2 WHERE [id] = $1")
|
||||
.bind(recipe_id)
|
||||
.bind(lang)
|
||||
|
|
@ -598,24 +622,6 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn set_nonexistent_language() -> Result<()> {
|
||||
let connection = Connection::new_in_memory().await?;
|
||||
|
||||
let user_id = create_a_user(&connection).await?;
|
||||
let recipe_id = connection.create_recipe(user_id).await?;
|
||||
|
||||
match connection.set_recipe_language(recipe_id, "asdf").await {
|
||||
// Nominal case.
|
||||
Err(DBError::UnknownLanguage(message)) => {
|
||||
println!("Ok: {}", message);
|
||||
}
|
||||
other => panic!("Set an nonexistent language must fail: {:?}", other),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_a_user(connection: &Connection) -> Result<i64> {
|
||||
let user_id = 1;
|
||||
connection.execute_sql(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue