added suport for tags when adding new recipes

This commit is contained in:
overflowerror 2022-03-31 20:53:46 +02:00
parent f822e73362
commit d373f387b7
3 changed files with 26 additions and 3 deletions

View file

@ -31,6 +31,7 @@ backendAdd() {
description="$(formData "description")"
qts="$(formData "qts")"
ingredients="$(formData "ingredients")"
tags="$(formData "tags" | tr '[:upper:]' '[:lower:]')"
if test "$(echo "$qts" | wc -l)" -ne "$(echo "$ingredients" | wc -l)"; then
status 400
@ -38,7 +39,7 @@ backendAdd() {
return
fi
addRecipe "$name" "$description" "$username" "$qts" "$ingredients"
addRecipe "$name" "$description" "$username" "$qts" "$ingredients" "$tags"
redirect "/backend"
endHeaders

View file

@ -41,10 +41,11 @@ addRecipe() {
local username="$3"
local qts="$4"
local ingredients="$5"
local tags="$6"
local nrIngredients="$(echo "$qts" | wc -l)"
template <(cat <<EOF
rid="$(template <(cat <<EOF
START TRANSACTION;
SELECT
@ -69,5 +70,25 @@ addRecipe() {
COMMIT;
EOF
) | execute
) | 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
}

View file

@ -1,6 +1,7 @@
<a href="/backend">Back</a>
<form method="POST" action="?">
<input type="text" name="name" placeholder="Name"><br />
<input type="text" name="tags" placeholder="Tags"><br />
<table>
<tr>
<th>