Jens Grivolla commited on
Commit
4587ee9
·
1 Parent(s): fea83a4

output borrowed books, make table configurable

Browse files
Files changed (2) hide show
  1. app.py +8 -5
  2. index.html +29 -1
app.py CHANGED
@@ -14,6 +14,9 @@ from dbutils.pooled_db import PooledDB
14
  import os
15
  import logging
16
 
 
 
 
17
  # Create a connection pool
18
  db_config = {
19
  'host': os.environ['MURMEL_DB_HOST'],
@@ -88,12 +91,12 @@ def rueckgabe(idBuch, grund='rueckgabe'):
88
  - int: 0 if the book was not found or already returned.
89
 
90
  """
91
- sql = "SELECT `idBuch`, `idKind`, `ausleihe`, `rueckgabe`, `rueckGrund` FROM `ausleihe_test` WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
92
  result = execute_query(sql, (idBuch,))
93
  if len(result) == 0:
94
  return 0
95
  # return the book
96
- sql = "UPDATE `ausleihe_test` SET `rueckgabe` = NOW(), `rueckGrund` = %s WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
97
  execute_query(sql, (grund, idBuch))
98
  #pprint.pprint(result)
99
  # return if the book was returned or not and who had it before
@@ -102,7 +105,7 @@ def rueckgabe(idBuch, grund='rueckgabe'):
102
  @app.get("/borrow/{idBuch}/{idKind}")
103
  def ausleihe(idBuch, idKind):
104
  """
105
- Performs a book loan operation by inserting a new record into the 'ausleihe_test' table.
106
 
107
  Parameters:
108
  - idBuch (int): The ID of the book being loaned.
@@ -122,7 +125,7 @@ def ausleihe(idBuch, idKind):
122
  message += f". Zuvor ausgeliehen von {prev_borrower_name}"
123
 
124
  # Insert new borrowing record
125
- sql = "INSERT INTO `ausleihe_test` (`idBuch`, `idKind`, `ausleihe`) VALUES (%s, %s, NOW());"
126
  execute_query(sql, (idBuch, idKind))
127
 
128
  return {"message": message}
@@ -138,7 +141,7 @@ def ausgeliehen(idKind):
138
  Returns:
139
  list: A list of tuples containing the book ID and the borrowing date for each book that is currently borrowed by the child.
140
  """
141
- sql = "SELECT `idBuch`, `ausleihe` FROM `ausleihe_test` WHERE `idKind` = %s AND `rueckgabe` IS NULL;"
142
  result = execute_query(sql, (idKind,))
143
  #pprint.pprint(result)
144
  return result
 
14
  import os
15
  import logging
16
 
17
+ # Add this line to define the AUSLEIHE_TABLE variable
18
+ AUSLEIHE_TABLE = os.environ.get('AUSLEIHE_TABLE', 'ausleihe_test')
19
+
20
  # Create a connection pool
21
  db_config = {
22
  'host': os.environ['MURMEL_DB_HOST'],
 
91
  - int: 0 if the book was not found or already returned.
92
 
93
  """
94
+ sql = f"SELECT `idBuch`, `idKind`, `ausleihe`, `rueckgabe`, `rueckGrund` FROM `{AUSLEIHE_TABLE}` WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
95
  result = execute_query(sql, (idBuch,))
96
  if len(result) == 0:
97
  return 0
98
  # return the book
99
+ sql = f"UPDATE `{AUSLEIHE_TABLE}` SET `rueckgabe` = NOW(), `rueckGrund` = %s WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
100
  execute_query(sql, (grund, idBuch))
101
  #pprint.pprint(result)
102
  # return if the book was returned or not and who had it before
 
105
  @app.get("/borrow/{idBuch}/{idKind}")
106
  def ausleihe(idBuch, idKind):
107
  """
108
+ Performs a book loan operation by inserting a new record into 'ausleihe_test' or the table defined by the AUSLEIHE_TABLE environment variable.
109
 
110
  Parameters:
111
  - idBuch (int): The ID of the book being loaned.
 
125
  message += f". Zuvor ausgeliehen von {prev_borrower_name}"
126
 
127
  # Insert new borrowing record
128
+ sql = f"INSERT INTO `{AUSLEIHE_TABLE}` (`idBuch`, `idKind`, `ausleihe`) VALUES (%s, %s, NOW());"
129
  execute_query(sql, (idBuch, idKind))
130
 
131
  return {"message": message}
 
141
  Returns:
142
  list: A list of tuples containing the book ID and the borrowing date for each book that is currently borrowed by the child.
143
  """
144
+ sql = f"SELECT `idBuch`, `ausleihe` FROM `{AUSLEIHE_TABLE}` WHERE `idKind` = %s AND `rueckgabe` IS NULL;"
145
  result = execute_query(sql, (idKind,))
146
  #pprint.pprint(result)
147
  return result
index.html CHANGED
@@ -21,13 +21,14 @@
21
  <select id="groupSelect" onchange="loadStudents()">
22
  <option value="">Gruppe auswählen</option>
23
  </select>
24
- <select id="studentSelect">
25
  <option value="">Schüler auswählen</option>
26
  </select>
27
  <input type="number" id="borrowBookId" placeholder="Buchnummer eingeben">
28
  <button onclick="borrowBook()">Buch ausleihen</button>
29
 
30
  <div id="message"></div>
 
31
 
32
  <script>
33
  // Load groups when the page loads
@@ -70,6 +71,33 @@
70
  option.textContent = student.Kind;
71
  select.appendChild(option);
72
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  });
74
  }
75
 
 
21
  <select id="groupSelect" onchange="loadStudents()">
22
  <option value="">Gruppe auswählen</option>
23
  </select>
24
+ <select id="studentSelect" onchange="loadOutstandingBooks()">
25
  <option value="">Schüler auswählen</option>
26
  </select>
27
  <input type="number" id="borrowBookId" placeholder="Buchnummer eingeben">
28
  <button onclick="borrowBook()">Buch ausleihen</button>
29
 
30
  <div id="message"></div>
31
+ <div id="outstandingBooks"></div>
32
 
33
  <script>
34
  // Load groups when the page loads
 
71
  option.textContent = student.Kind;
72
  select.appendChild(option);
73
  });
74
+ // Clear outstanding books when changing students
75
+ document.getElementById('outstandingBooks').innerHTML = '';
76
+ });
77
+ }
78
+
79
+ function loadOutstandingBooks() {
80
+ const studentId = document.getElementById('studentSelect').value;
81
+ if (!studentId) {
82
+ document.getElementById('outstandingBooks').innerHTML = '';
83
+ return;
84
+ }
85
+
86
+ fetch(`/borrowed/${studentId}`)
87
+ .then(response => response.json())
88
+ .then(books => {
89
+ const outstandingBooksDiv = document.getElementById('outstandingBooks');
90
+ if (books.length === 0) {
91
+ outstandingBooksDiv.innerHTML = '<p>Keine ausstehenden Bücher</p>';
92
+ } else {
93
+ let html = '<h3>Ausstehende Bücher:</h3><ul>';
94
+ books.forEach(book => {
95
+ const borrowDate = new Date(book.ausleihe).toLocaleDateString('de-DE');
96
+ html += `<li>Buch ID: ${book.idBuch}, Ausgeliehen am: ${borrowDate}</li>`;
97
+ });
98
+ html += '</ul>';
99
+ outstandingBooksDiv.innerHTML = html;
100
+ }
101
  });
102
  }
103