2022-03-30 18:41:59 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
getRecipesByUsername() {
|
|
|
|
local username="$1"
|
|
|
|
|
|
|
|
query <<EOF
|
|
|
|
SELECT
|
|
|
|
recipes.id,
|
|
|
|
name
|
|
|
|
FROM recipes
|
|
|
|
INNER JOIN users
|
|
|
|
ON recipes.userFk = users.id
|
|
|
|
WHERE
|
|
|
|
users.username = '$(escape "$username")'
|
|
|
|
EOF
|
|
|
|
}
|
2022-03-30 20:07:35 +00:00
|
|
|
|
2022-03-30 20:43:32 +00:00
|
|
|
getRecipeById() {
|
|
|
|
local id="$(echo "$1" | sed 's/[^0-9]//g')"
|
|
|
|
|
|
|
|
query <<EOF
|
|
|
|
SELECT
|
|
|
|
recipes.name,
|
|
|
|
description,
|
|
|
|
ingredients.name,
|
|
|
|
ingredients.quantity,
|
|
|
|
users.id,
|
|
|
|
users.username
|
|
|
|
FROM recipes
|
|
|
|
INNER JOIN ingredients
|
|
|
|
ON recipes.id = ingredients.recipeFk
|
|
|
|
INNER JOIN users
|
|
|
|
ON recipes.userFk = users.id
|
|
|
|
WHERE recipes.id = $id
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:00:39 +00:00
|
|
|
getTagsByRecipeId() {
|
|
|
|
local id="$(echo "$1" | sed 's/[^0-9]//g')"
|
|
|
|
|
|
|
|
query <<EOF
|
|
|
|
SELECT DISTINCT name
|
|
|
|
FROM tags
|
|
|
|
INNER JOIN recipeTags
|
|
|
|
ON recipeTags.tagFk = tags.id
|
|
|
|
WHERE recipeFk = $id
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:07:35 +00:00
|
|
|
addRecipe() {
|
|
|
|
local name="$1"
|
|
|
|
local description="$2"
|
|
|
|
local username="$3"
|
|
|
|
local qts="$4"
|
|
|
|
local ingredients="$5"
|
2022-03-31 18:53:46 +00:00
|
|
|
local tags="$6"
|
2022-03-30 20:07:35 +00:00
|
|
|
|
|
|
|
local nrIngredients="$(echo "$qts" | wc -l)"
|
|
|
|
|
2022-03-31 18:53:46 +00:00
|
|
|
rid="$(template <(cat <<EOF
|
2022-03-30 20:07:35 +00:00
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
@uid := id
|
|
|
|
FROM users
|
|
|
|
WHERE username = '$(escape "$username")';
|
|
|
|
|
|
|
|
INSERT INTO recipes
|
|
|
|
(userFk, name, description) VALUES
|
|
|
|
(@uid, '$(escape "$name")', '$(escape "$description")');
|
|
|
|
|
|
|
|
SELECT @rid := LAST_INSERT_ID();
|
|
|
|
|
|
|
|
{{ for i in \$(seq $nrIngredients); do }}
|
|
|
|
INSERT INTO ingredients
|
|
|
|
(recipeFk, name, quantity) VALUES
|
|
|
|
(@rid,
|
|
|
|
'{{ escape "\$(echo "\$ingredients" | tail -n +\$i | head -n 1)" | tr -d \\\\n }}',
|
|
|
|
'{{ escape "\$(echo "\$qts" | tail -n +\$i | head -n 1)" | tr -d \\\\n }}'
|
|
|
|
);
|
|
|
|
{{ done }}
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
EOF
|
2022-03-31 18:53:46 +00:00
|
|
|
) | query | getColumns 1 | tail -n +3 | head -n 1)"
|
|
|
|
|
|
|
|
for tag in $tags; do
|
|
|
|
tid="$(echo "SELECT id FROM tags WHERE name = '$(escape "$tag")'" | query | getColumns 1 | tail -n +2 | head -n 1)"
|
|
|
|
|
|
|
|
if test -z "$tid"; then
|
|
|
|
echo "tid empty" >&2
|
|
|
|
tid="$(query <<EOF | tee >(cat >&2) | getColumns 1
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
INSERT INTO tags (name) VALUES ('$(escape "$tag")');
|
|
|
|
|
|
|
|
SELECT LAST_INSERT_ID();
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "INSERT INTO recipeTags (recipeFk, tagFk) VALUES ($rid, $tid)" | execute
|
|
|
|
done
|
2022-03-30 20:07:35 +00:00
|
|
|
}
|