Skip to content

Commit

Permalink
Merge pull request #121 from kronos-et-al/backend
Browse files Browse the repository at this point in the history
Version 1.0.0 backend
  • Loading branch information
worldofjoni authored Nov 6, 2023
2 parents e297c10 + 65201a1 commit 235ff06
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 172 deletions.
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ IMAGE_DIR=
#HTTP_PORT=
#BASE_URL=
#RATE_LIMIT=
#MAX_UPLOAD_SIZE=

# --- logging ---
#LOG_CONFIG=warn,mensa_app_backend=trace

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

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

5 changes: 5 additions & 0 deletions backend/Cargo.lock

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

8 changes: 7 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ async-trait = "0.1.68"
chrono = "0.4.26"
thiserror = "1.0.40"
uuid = "1.4.0"
axum = { version = "0.6.18", features = ["http2", "macros", "multipart"] }
axum = { version = "0.6.18", features = [
"http2",
"macros",
"multipart",
"tracing",
] }
async-graphql = { version = "6.0.0", features = [
"chrono",
"uuid",
Expand Down Expand Up @@ -61,6 +66,7 @@ google-jwt-auth = "0.0.2"
hyper = { version = "0.14" }
mime = "0.3.17"
hmac = "0.12.1"
multer = { version = "2.1.0", features = ["tokio-io"] }

[dev-dependencies]
serial_test = "2.0.0"
Expand Down
1 change: 1 addition & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The following options are available:
| `IMAGE_DIR` | Path to folder where images will be stored. Backslashes (`\`) need to be escaped (`\\`). | required |
| `MAX_IMAGE_WIDTH` and `MAX_IMAGE_HEIGHT` | Maximum width and height stored for stored images. Uploaded images will be scaled accordingly. | `1920` and `1080` |
| `RATE_LIMIT` | Limit the number of API requests per second. `0` means disabled. | `0` (disabled) |
| `MAX_UPLOAD_SIZE` | Maximal size (in bytes) an http body can have to get accepted. This implies a maximal size an image upload can have. | `10485760` (10 MiB) |


## Building the backend
Expand Down
37 changes: 6 additions & 31 deletions backend/src/layer/data/database/mealplan_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
similar_name: &str,
meal_type: MealType,
allergens: &[Allergen],
additives: &[Additive],
_additives: &[Additive],
) -> Result<Option<Uuid>> {
sqlx::query_scalar!(
// the `<@` operator checks whether each element in the left array is also present in the right
r#"
SELECT food_id
FROM food JOIN meal USING (food_id)
WHERE similarity(name, $1) >= $5 AND food_type = $2
WHERE similarity(name, $1) >= $4 AND food_type = $2
AND food_id IN (
-- all food_id's with same allergens
SELECT food_id
Expand All @@ -76,14 +76,6 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
HAVING COALESCE(array_agg(allergen) FILTER (WHERE allergen IS NOT NULL), ARRAY[]::allergen[]) <@ $3::allergen[]
AND COALESCE(array_agg(allergen) FILTER (WHERE allergen IS NOT NULL), ARRAY[]::allergen[]) @> $3::allergen[]
)
AND food_id IN (
-- all food_id's with same additives
SELECT food_id
FROM food_additive FULL JOIN food USING (food_id)
GROUP BY food_id
HAVING COALESCE(array_agg(additive) FILTER (WHERE additive IS NOT NULL), ARRAY[]::additive[]) <@ $4::additive[]
AND COALESCE(array_agg(additive) FILTER (WHERE additive IS NOT NULL), ARRAY[]::additive[]) @> $4::additive[]
)
ORDER BY similarity(name, $1) DESC
"#,
similar_name,
Expand All @@ -93,11 +85,6 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
.copied()
.map(Allergen::to_db_string)
.collect::<Vec<_>>() as _,
additives
.iter()
.copied()
.map(Additive::to_db_string)
.collect::<Vec<_>>() as _,
THRESHOLD_MEAL
)
.fetch_optional(&self.pool)
Expand All @@ -110,14 +97,14 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
similar_name: &str,
meal_type: MealType,
allergens: &[Allergen],
additives: &[Additive],
_additives: &[Additive],
) -> Result<Option<Uuid>> {
sqlx::query_scalar!(
// the `<@` operator checks whether each element in the left array is also present in the right
r#"
SELECT food_id
FROM food
WHERE similarity(name, $1) >= $5 AND food_type = $2 AND food_id NOT IN (SELECT food_id FROM meal)
WHERE similarity(name, $1) >= $4 AND food_type = $2 AND food_id NOT IN (SELECT food_id FROM meal)
AND food_id IN (
-- all food_id's with same allergens
SELECT food_id
Expand All @@ -126,14 +113,6 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
HAVING COALESCE(array_agg(allergen) FILTER (WHERE allergen IS NOT NULL), ARRAY[]::allergen[]) <@ $3::allergen[]
AND COALESCE(array_agg(allergen) FILTER (WHERE allergen IS NOT NULL), ARRAY[]::allergen[]) @> $3::allergen[]
)
AND food_id IN (
-- all food_id's with same additives
SELECT food_id
FROM food_additive FULL JOIN food USING (food_id)
GROUP BY food_id
HAVING COALESCE(array_agg(additive) FILTER (WHERE additive IS NOT NULL), ARRAY[]::additive[]) <@ $4::additive[]
AND COALESCE(array_agg(additive) FILTER (WHERE additive IS NOT NULL), ARRAY[]::additive[]) @> $4::additive[]
)
ORDER BY similarity(name, $1) DESC
"#,
similar_name,
Expand All @@ -143,11 +122,6 @@ impl MealplanManagementDataAccess for PersistentMealplanManagementData {
.copied()
.map(Allergen::to_db_string)
.collect::<Vec<_>>() as _,
additives
.iter()
.copied()
.map(Additive::to_db_string)
.collect::<Vec<_>>() as _,
THRESHOLD_MEAL
)
.fetch_optional(&self.pool)
Expand Down Expand Up @@ -369,6 +343,7 @@ mod test {
#![allow(clippy::cast_sign_loss)]

use super::*;
use crate::util::Additive::Sulphur;
use crate::util::Allergen::{Ei, Se, So, We, ML};
use crate::util::Date;
use chrono::Local;
Expand Down Expand Up @@ -525,7 +500,7 @@ mod test {
("f7337122-b018-48ad-b420-6202dc3cb4ff", (vec![], vec![We])),
(
"25cb8c50-75a4-48a2-b4cf-8ab2566d8bec",
(vec![], vec![Ei, ML, We]),
(vec![Sulphur], vec![Ei, ML, We]),
),
(
"0a850476-eda4-4fd8-9f93-579eb85b8c25",
Expand Down
Loading

0 comments on commit 235ff06

Please sign in to comment.