]> git.ekhem.eu.org Git - gym.git/commitdiff
Add a script for recording a new workout.
authorJakub Czajka <jakub@ekhem.eu.org>
Sun, 17 Dec 2023 21:07:14 +0000 (22:07 +0100)
committerJakub Czajka <jakub@ekhem.eu.org>
Sun, 31 Dec 2023 11:00:55 +0000 (12:00 +0100)
record.sh [new file with mode: 0755]

diff --git a/record.sh b/record.sh
new file mode 100755 (executable)
index 0000000..8e575b7
--- /dev/null
+++ b/record.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+# Copyright (c) 2023 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+exercises=$(/usr/bin/psql --user=gym_tracker  --dbname="${GYM_DB}" \
+  --tuples-only --no-align --command="\
+WITH per_gym_records AS (
+  SELECT exercise_id, value
+  FROM gyms_records INNER JOIN gyms
+    ON gyms_records.gym_id = gyms.id
+  WHERE name = '${GYM}'
+), records_all_exercises AS (
+  SELECT *
+  FROM per_gym_records
+  UNION
+  SELECT *
+  FROM records
+  WHERE NOT EXISTS (
+    SELECT * FROM
+    per_gym_records
+    WHERE per_gym_records.exercise_id = records.exercise_id
+  )
+)
+SELECT exercises.id, name, value
+FROM exercises LEFT OUTER JOIN records_all_exercises
+  ON exercises.id = records_all_exercises.exercise_id
+ORDER BY exercises.id")
+
+format_for_html_input() {
+  /usr/bin/awk -F "|" '{printf "\
+<div class='\''container'\''>\
+<label for='\''%s'\''>%s</label>\
+<label for='\''%s'\''>%s</label>\
+<input id='\''%s'\'' name='\''%s'\'' type='\''text'\'' />\
+<label for='\''%s_local_record'\''>\
+<input id='\''%s_local_record'\''\
+       name='\''%s_local_record'\''\
+       type='\''checkbox'\'' />\
+ Is gym record?\
+</label>\
+<label for='\''%s_global_record'\''>\
+<input id='\''%s_global_record'\''\
+       name='\''%s_global_record'\''\
+       type='\''checkbox'\'' />\
+ Is record?\
+</label>\
+</div>",\
+    $1, $2, $1, $3, $1, $1, $1, $1, $1, $1, $1, $1}'
+}
+
+PAGE="
+<!DOCTYPE html>
+<html>
+<head>
+  <script>
+    window.onbeforeunload = function (event) {
+      event.preventDefault();
+      event.returnValue = false;
+    }
+  </script>
+  <style>
+    body {
+      background-color: #7fa99b;
+      max-width: max(80vh, 1300px);
+      white-space: pre-wrap;
+    }
+    .container {
+      background-color: #f1d18a;
+      display: grid;
+      grid-template-columns: 1fr 1.2fr 1fr 1fr 1fr;
+      margin-bottom: 15px;
+      padding: 15px;
+    }
+    .container, input, button {
+      border: 1px solid;
+    }
+    button {
+      font-size: 1em;
+      min-width: min(30%, 200px);
+      padding: 7px 15px;
+    }
+    form > label {
+      align-items: center;
+      display: flex;
+      column-gap: 15px;
+    }
+    @media screen and (max-width: 980px) {
+      .container {
+        display: flex;
+        flex-direction: column;
+        gap: 20px;
+      }
+      .container, input, button {
+        border-width: 4px;
+      }
+      label, input[type=text], button {
+        font-size: 4.5em;
+      }
+      input[type=text] {
+        max-width: 98%;
+      }
+      input[type=number] {
+        font-size: 1em;
+        width: 400px;
+      }
+      input[type=checkbox] {
+        width: 55px;
+        height: 55px;
+      }
+    }
+  </style>
+</head>
+<body>
+  <form action='/workout' method='post'>
+    <input type='hidden' name='gym' value='${GYM}' />
+    <input type='hidden' name='date' value='${DATE}' />
+    <label>
+      <p>Duration [min]:</p>
+      <input type='number' name='duration' value='0' required />
+    </label>
+    $(echo "${exercises}" | format_for_html_input)
+    <button>Submit</button>
+  </form>
+</body>
+</html>"
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "${PAGE}" | sed 's/^[ \t]*//g'