ichieh commited on
Commit
93359ec
·
1 Parent(s): 08794f7

增加桌次命名

Browse files
Files changed (2) hide show
  1. app.py +14 -13
  2. templates/admin.html +5 -3
app.py CHANGED
@@ -570,24 +570,25 @@ def get_tables():
570
  tables = Table.query.order_by(Table.id).all()
571
  print(f"找到 {len(tables)} 張桌子")
572
 
573
- result = [{
574
- 'id': table.id,
575
- 'name': table.name, # 添加桌次名稱
576
- 'capacity': table.capacity,
577
- 'participants': [{
578
- 'id': p.id,
579
- 'nickname': p.nickname,
580
- 'fb_link': p.fb_link,
581
- 'introduction': p.introduction,
582
- 'is_table_leader': p.is_table_leader
583
- } for p in sorted(table.participants, key=lambda x: (not x.is_table_leader, x.created_at))]
584
- } for table in tables]
 
 
585
 
586
  print("桌次資料獲取成功")
587
  return jsonify(result)
588
  except Exception as e:
589
  print(f"獲取桌次資料時出錯: {str(e)}")
590
- db.session.rollback()
591
  return jsonify({'error': str(e)}), 500
592
 
593
  @app.route('/api/register', methods=['POST'])
 
570
  tables = Table.query.order_by(Table.id).all()
571
  print(f"找到 {len(tables)} 張桌子")
572
 
573
+ result = {
574
+ 'tables': [{
575
+ 'id': table.id,
576
+ 'name': table.name or '', # 確保 name 不會是 None
577
+ 'capacity': table.capacity,
578
+ 'participants': [{
579
+ 'id': p.id,
580
+ 'nickname': p.nickname,
581
+ 'fb_link': p.fb_link,
582
+ 'introduction': p.introduction,
583
+ 'is_table_leader': p.is_table_leader
584
+ } for p in sorted(table.participants, key=lambda x: (not x.is_table_leader, x.created_at))]
585
+ } for table in tables]
586
+ }
587
 
588
  print("桌次資料獲取成功")
589
  return jsonify(result)
590
  except Exception as e:
591
  print(f"獲取桌次資料時出錯: {str(e)}")
 
592
  return jsonify({'error': str(e)}), 500
593
 
594
  @app.route('/api/register', methods=['POST'])
templates/admin.html CHANGED
@@ -210,6 +210,10 @@
210
  const container = document.getElementById('tableNameSettings');
211
  container.innerHTML = '';
212
 
 
 
 
 
213
  data.tables.forEach(table => {
214
  const col = document.createElement('div');
215
  col.className = 'col-md-4 mb-3';
@@ -221,19 +225,17 @@
221
  value="${table.name || ''}"
222
  placeholder="輸入桌次名稱"
223
  onchange="updateTableName(${table.id}, this.value)">
224
- ${table.name ? `
225
  <button class="btn btn-outline-secondary" type="button"
226
  onclick="updateTableName(${table.id}, '')">
227
  <i class="bi bi-x"></i>
228
  </button>
229
- ` : ''}
230
  </div>
231
  `;
232
  container.appendChild(col);
233
  });
234
  } catch (error) {
235
  console.error('Error:', error);
236
- Swal.fire('錯誤', '載入桌次名稱失敗', 'error');
237
  }
238
  }
239
 
 
210
  const container = document.getElementById('tableNameSettings');
211
  container.innerHTML = '';
212
 
213
+ if (!data.tables) {
214
+ throw new Error('無效的桌次資料');
215
+ }
216
+
217
  data.tables.forEach(table => {
218
  const col = document.createElement('div');
219
  col.className = 'col-md-4 mb-3';
 
225
  value="${table.name || ''}"
226
  placeholder="輸入桌次名稱"
227
  onchange="updateTableName(${table.id}, this.value)">
 
228
  <button class="btn btn-outline-secondary" type="button"
229
  onclick="updateTableName(${table.id}, '')">
230
  <i class="bi bi-x"></i>
231
  </button>
 
232
  </div>
233
  `;
234
  container.appendChild(col);
235
  });
236
  } catch (error) {
237
  console.error('Error:', error);
238
+ Swal.fire('錯誤', error.message || '載入桌次名稱失敗', 'error');
239
  }
240
  }
241