Recipe edit (WIP): add API to set some recipe values

This commit is contained in:
Greg Burri 2024-12-23 01:37:01 +01:00
parent c6dfff065c
commit dd05a673d9
20 changed files with 690 additions and 2189 deletions

View file

@ -1,13 +1,46 @@
use super::{Connection, DBError, Result};
use crate::{
consts,
data::model::{self, Difficulty},
};
use crate::{consts, data::model};
use common::ron_api::Difficulty;
impl Connection {
pub async fn get_all_recipe_titles(&self) -> Result<Vec<(i64, String)>> {
sqlx::query_as("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")
.fetch_all(&self.pool)
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]
"#,
)
.fetch_all(&self.pool)
.await
.map_err(DBError::from)
}
pub async fn get_all_unpublished_recipe_titles(
&self,
owned_by: i64,
) -> Result<Vec<(i64, String)>> {
sqlx::query_as(
r#"
SELECT [id], [title]
FROM [Recipe]
WHERE [is_published] = false AND [user_id] = $1
ORDER BY [title]
"#,
)
.bind(owned_by)
.fetch_all(&self.pool)
.await
.map_err(DBError::from)
}
pub async fn can_edit_recipe(&self, user_id: i64, recipe_id: i64) -> Result<bool> {
sqlx::query_scalar(r#"SELECT COUNT(*) FROM [Recipe] WHERE [id] = $1 AND [user_id] = $2"#)
.bind(recipe_id)
.bind(user_id)
.fetch_one(&self.pool)
.await
.map_err(DBError::from)
}

View file

@ -1,4 +1,5 @@
use chrono::prelude::*;
use common::ron_api::Difficulty;
use sqlx::{self, FromRow};
#[derive(Debug, Clone, FromRow)]
@ -50,29 +51,3 @@ pub struct Ingredient {
pub quantity: i32,
pub quantity_unit: String,
}
#[derive(PartialEq, Debug)]
pub enum Difficulty {
Unknown = 0,
Easy = 1,
Medium = 2,
Hard = 3,
}
impl TryFrom<u32> for Difficulty {
type Error = &'static str;
fn try_from(value: u32) -> Result<Self, Self::Error> {
Ok(match value {
1 => Self::Easy,
2 => Self::Medium,
3 => Self::Hard,
_ => Self::Unknown,
})
}
}
impl From<Difficulty> for u32 {
fn from(value: Difficulty) -> Self {
value as u32
}
}