diff options
Diffstat (limited to '.config/emacs')
| -rw-r--r-- | .config/emacs/jd/jd-gym.el | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/.config/emacs/jd/jd-gym.el b/.config/emacs/jd/jd-gym.el new file mode 100644 index 0000000..2602e3e --- /dev/null +++ b/.config/emacs/jd/jd-gym.el | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | ;;; jd-gym.el --- Gym workout manager. | ||
| 2 | ;;; Commentary: | ||
| 3 | ;; It will be a gym workout manager in the future. | ||
| 4 | ;;; code: | ||
| 5 | |||
| 6 | (require 'emacsql-sqlite) | ||
| 7 | |||
| 8 | (defconst jd-gym/db--tables-schema | ||
| 9 | '((workout-plans | ||
| 10 | [(id integer :primary-key :autoincrement) | ||
| 11 | (name text)]) | ||
| 12 | |||
| 13 | (workouts | ||
| 14 | [(id integer :primary-key :autoincrement) | ||
| 15 | (workout-plan-id integer :not-null) | ||
| 16 | (day integer :not-null) | ||
| 17 | (month integer :not-null) | ||
| 18 | (year integer :not-null)] | ||
| 19 | (:foreign-key [workout-plan-id] :references workout-plans [id] :on-delete :cascade)) | ||
| 20 | |||
| 21 | (muscle-part | ||
| 22 | [(id integer :primary-key :autoincrement) | ||
| 23 | (name text :not-null)]) | ||
| 24 | |||
| 25 | (exercises | ||
| 26 | [(id integer :primary-key :autoincrement) | ||
| 27 | (name text :not-null) | ||
| 28 | (muscle-part-id integer :not-null)] | ||
| 29 | (:foreign-key [muscle-part-id] :references muscle-part [id] :on-delete :cascade)) | ||
| 30 | |||
| 31 | (workout-plan-exercise-map | ||
| 32 | [(id integer :primary-key :autoincrement) | ||
| 33 | (week-day integer :not-null) | ||
| 34 | (workout-plan-id integer :not-null) | ||
| 35 | (exercise-id integer :not-null)] | ||
| 36 | (:foreign-key [workout-plan-id] :references workout-plans [id] :on-delete :cascade) | ||
| 37 | (:foreign-key [exercise-id] :references exercises [id] :on-delete :cascade)) | ||
| 38 | |||
| 39 | (workout-sets | ||
| 40 | [(id integer :primary-key :autoincrement) | ||
| 41 | (reps integer :not-null) | ||
| 42 | (weight integer :not-null) | ||
| 43 | (workout-plan-exercise-map-id integer :not-null) | ||
| 44 | (comment text)] | ||
| 45 | (:foreign-key [workout-plan-exercise-map-id] :references workout-plan-exercise-map [id] :on-delete :cascade)))) | ||
| 46 | |||
| 47 | (defconst jd-gym/db--test-data | ||
| 48 | '((workout-plans | ||
| 49 | ([nil "Push pull"])) | ||
| 50 | (workouts | ||
| 51 | ([nil 1 3 1 2023])) | ||
| 52 | (muscle-part | ||
| 53 | ([nil "glutes"] | ||
| 54 | [nil "chest"])) | ||
| 55 | (exercises | ||
| 56 | ([nil "Bench press" 2] | ||
| 57 | [nil "RDL" 1])) | ||
| 58 | (workout-plan-exercise-map | ||
| 59 | ([nil 0 1 1] ; Monday, Push pull, Bench press | ||
| 60 | [nil 1 1 2] ; Tuesday, Push pull, RDL | ||
| 61 | [nil 3 1 1] ; Thursday, Push pull, Bench press | ||
| 62 | [nil 4 1 2] ; Friday, Push pull, RDL | ||
| 63 | )) | ||
| 64 | (workout-sets | ||
| 65 | ([nil 7 70 1 "New PR!"] | ||
| 66 | [nil 8 110 2 "It was hard!"])))) | ||
| 67 | |||
| 68 | (defun jd-gym/db--test-data-insert () | ||
| 69 | "Insert test data to jd-gym database." | ||
| 70 | (mapc (lambda (table) | ||
| 71 | (let ((table-name (car table)) | ||
| 72 | (table-data (cdr table))) | ||
| 73 | (emacsql jd-gym/db--conn [:insert :into $i1 | ||
| 74 | :values $v2] | ||
| 75 | table-name table-data))) | ||
| 76 | jd-gym/db--test-data)) | ||
| 77 | |||
| 78 | (defvar jd-gym/db-path "~/Documents/Gym/gym.sqlite" | ||
| 79 | "Path can be relative or absolute.") | ||
| 80 | |||
| 81 | (defvar jd-gym/db--conn nil | ||
| 82 | "Store connection to jd-gym database.") | ||
| 83 | |||
| 84 | (defun jd-gym/db--conn-p () | ||
| 85 | "Check if jd-gym is connected to db." | ||
| 86 | (and (emacsql-sqlite-connection-p jd-gym/db--conn) | ||
| 87 | (emacsql-live-p jd-gym/db--conn))) | ||
| 88 | |||
| 89 | (defun jd-gym/db--connect () | ||
| 90 | "Connect to db if there is no connection yet." | ||
| 91 | (unless (jd-gym/db--conn-p) | ||
| 92 | (setq jd-gym/db--conn (emacsql-sqlite jd-gym/db-path)))) | ||
| 93 | |||
| 94 | (defun jd-gym/db--close () | ||
| 95 | "Close db connection." | ||
| 96 | (when (jd-gym/db--conn-p) | ||
| 97 | (emacsql-close jd-gym/db--conn))) | ||
| 98 | |||
| 99 | (defun jd-gym/db--init () | ||
| 100 | "Initialize database structure." | ||
| 101 | (when (jd-gym/db--conn-p) | ||
| 102 | (emacsql jd-gym/db--conn [:pragma (= foreign_keys ON)]) | ||
| 103 | (emacsql jd-gym/db--conn "PRAGMA foreign_keys=ON") | ||
| 104 | (mapc (lambda (table) | ||
| 105 | (let ((table-name (car table)) | ||
| 106 | (table-schema (cdr table))) | ||
| 107 | (emacsql jd-gym/db--conn [:create-table $i1 $S2] table-name table-schema))) | ||
| 108 | jd-gym/db--tables-schema))) | ||
| 109 | |||
| 110 | (defun jd-gym/db () | ||
| 111 | "Entrypoint to jd-gym db." | ||
| 112 | (unless (file-exists-p jd-gym/db-path) | ||
| 113 | (jd-gym/db--close) | ||
| 114 | (jd-gym/db--connect) ; Restart connection | ||
| 115 | (jd-gym/db--init))) | ||
| 116 | |||
| 117 | |||
| 118 | (provide 'jd-gym) | ||
| 119 | |||
| 120 | ;;; jd-gym.el ends here | ||
