adding recipes now works

This commit is contained in:
overflowerror 2022-03-30 22:07:35 +02:00
parent cdf92851ff
commit 101dd0bcc6
5 changed files with 130 additions and 2 deletions

View file

@ -10,3 +10,36 @@ backendHome() {
content="$(getRecipesByUsername "$username" | template "templates/backend.fragment.templ")"
template "templates/layout.html.templ"
}
route GET "/backend/new" backendNew
backendNew() {
requireLoggedIn
htmlContent
endHeaders
title="Backend - New"
content="$(template "templates/backend.new.fragment.templ")"
template "templates/layout.html.templ"
}
route POST "/backend/new" backendAdd
backendAdd() {
requireLoggedIn
cacheFormData
name="$(formData "name")"
description="$(formData "description")"
qts="$(formData "qts")"
ingredients="$(formData "ingredients")"
if test "$(echo "$qts" | wc -l)" -ne "$(echo "$ingredients" | wc -l)"; then
status 400
endHeaders
return
fi
addRecipe "$name" "$description" "$username" "$qts" "$ingredients"
redirect "/backend"
endHeaders
}

View file

@ -14,3 +14,40 @@ getRecipesByUsername() {
users.username = '$(escape "$username")'
EOF
}
addRecipe() {
local name="$1"
local description="$2"
local username="$3"
local qts="$4"
local ingredients="$5"
local nrIngredients="$(echo "$qts" | wc -l)"
template <(cat <<EOF
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
) | execute
}

2
shochu

@ -1 +1 @@
Subproject commit 10b8f41076765a411feb7b716ed7a9de3f792531
Subproject commit 10cc312892f2851b60ada93585aeab0ff4f9356e

View file

@ -1,5 +1,7 @@
<h1>Hi</h1>
You are {{ print $username }}.
You are {{ print $username }}.<br />
<a href="/backend/new">New Recipe</a>
<ul>
{{ while read line; do }}

View file

@ -0,0 +1,56 @@
<a href="/backend">Back</a>
<form method="POST" action="?">
<input type="text" name="name" placeholder="Name"><br />
<table>
<tr>
<th>
Qt.
</th>
<th>
Ingredient
</th>
</tr>
<tr>
<td>
<input type="text" name="qts">
</td>
<td>
<input type="text" name="ingredients">
</td>
</tr>
<tr>
<td>
<input type="text" name="qts">
</td>
<td>
<input type="text" name="ingredients">
</td>
</tr>
<tr>
<td>
<input type="text" name="qts">
</td>
<td>
<input type="text" name="ingredients">
</td>
</tr>
<tr>
<td>
<input type="text" name="qts">
</td>
<td>
<input type="text" name="ingredients">
</td>
</tr>
<tr>
<td>
<input type="text" name="qts">
</td>
<td>
<input type="text" name="ingredients">
</td>
</tr>
</table>
<textarea name="description" placeholder="Description"></textarea><br />
<input type="submit">
</form>