ichieh commited on
Commit
9f7a6e2
·
1 Parent(s): c65e601

Update table settings to preserve existing participants

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -97,13 +97,30 @@ def admin_settings():
97
  seats_per_table = data.get('seats_per_table')
98
 
99
  try:
100
- # 刪除所有現有的桌子
101
- Table.query.delete()
 
 
 
 
 
 
 
 
 
 
102
 
103
- # 創建新的桌子
104
- for i in range(1, total_tables + 1):
105
- table = Table(capacity=seats_per_table, name=f'Table {i}')
106
- db.session.add(table)
 
 
 
 
 
 
 
107
 
108
  db.session.commit()
109
  return jsonify({'message': 'Settings updated successfully'})
 
97
  seats_per_table = data.get('seats_per_table')
98
 
99
  try:
100
+ current_tables = Table.query.all()
101
+ current_table_count = len(current_tables)
102
+
103
+ # 更新現有桌子的容量
104
+ for table in current_tables:
105
+ table.capacity = seats_per_table
106
+
107
+ # 如果需要更多桌子,則新增
108
+ if total_tables > current_table_count:
109
+ for i in range(current_table_count + 1, total_tables + 1):
110
+ table = Table(capacity=seats_per_table, name=f'Table {i}')
111
+ db.session.add(table)
112
 
113
+ # 如果需要減少桌子,則移除空桌子
114
+ elif total_tables < current_table_count:
115
+ tables_to_remove = []
116
+ for table in current_tables[total_tables:]:
117
+ # 檢查桌子是否有人
118
+ if Participant.query.filter_by(table_id=table.id).count() == 0:
119
+ tables_to_remove.append(table)
120
+
121
+ # 只刪除沒有人的桌子
122
+ for table in tables_to_remove:
123
+ db.session.delete(table)
124
 
125
  db.session.commit()
126
  return jsonify({'message': 'Settings updated successfully'})