Calendar is now displayed on home page and recipes can be scheduled without being logged
This commit is contained in:
parent
ccb1248da3
commit
37721ac3ea
22 changed files with 538 additions and 166 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use chrono::prelude::*;
|
||||
use common::ron_api::Difficulty;
|
||||
use itertools::Itertools;
|
||||
use sqlx::Error;
|
||||
use sqlx::{Error, Sqlite};
|
||||
|
||||
use super::{Connection, DBError, Result};
|
||||
use crate::data::model;
|
||||
|
|
@ -64,6 +64,37 @@ ORDER BY [title]
|
|||
.map_err(DBError::from)
|
||||
}
|
||||
|
||||
/// Returns titles associated to given ids in the same order.
|
||||
/// Empty string for unknown id.
|
||||
pub async fn get_recipe_titles(&self, ids: &[i64]) -> Result<Vec<String>> {
|
||||
let mut query_builder: sqlx::QueryBuilder<Sqlite> =
|
||||
sqlx::QueryBuilder::new("SELECT [id], [title] FROM [Recipe] WHERE [id] IN(");
|
||||
let mut separated = query_builder.separated(", ");
|
||||
for id in ids {
|
||||
separated.push_bind(id);
|
||||
}
|
||||
separated.push_unseparated(")");
|
||||
let query = query_builder.build_query_as::<(i64, String)>();
|
||||
let titles = query.fetch_all(&self.pool).await?;
|
||||
let mut result = vec![];
|
||||
// Warning: O(n^2), OK for small number of ids.
|
||||
for id in ids {
|
||||
result.push(
|
||||
titles
|
||||
.iter()
|
||||
.find_map(|(fetched_id, title)| {
|
||||
if fetched_id == id {
|
||||
Some(title.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn can_edit_recipe(&self, user_id: i64, recipe_id: i64) -> Result<bool> {
|
||||
sqlx::query_scalar(
|
||||
r#"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue