Fixed websocket

This commit is contained in:
Sviatoslav Tsariov Yurievich 2025-05-24 03:41:35 +03:00
parent 770835efa8
commit 7f9f66c519
3 changed files with 26 additions and 17 deletions

10
run.bat
View File

@ -9,5 +9,15 @@ if not exist "venv" (
)
call venv/Scripts/activate
rem Load environment variables from .env file
if exist ".env" (
for /f "tokens=1,* delims==" %%a in ('findstr /v /r "^#" .env') do (
set "%%a=%%b"
)
) else (
echo .env file not found.
)
python src/app.py
pause

View File

@ -17,15 +17,21 @@ def create_reservation():
current_user = ""#get_jwt_identity()
reservation = Reservation(**request.json, creator=current_user)
id = ReservationRepository.insert(reservation)
notify_clients(reservation.room_id, reservation.date)
notify_clients(reservation.date)
return jsonify({"message": "Reservation created successfully", "id": id})
@reservation_blueprint.route('/<reservation_id>', methods=['DELETE'])
#@jwt_required()
def cancel_reservation(reservation_id):
reservation = ReservationRepository.get_by_id(reservation_id)
if not reservation:
return jsonify({"error": "Reservation not found"}), 404
room_id = reservation["room_id"]
date = reservation["date"]
result = ReservationRepository.delete(reservation_id)
notify_clients(reservation["room_id"], reservation["date"])
notify_clients(date)
if not result or result.deleted_count == 0:
return jsonify({"error": "Reservation not found"}), 404
return jsonify({"message": "Reservation cancelled"})
@ -38,7 +44,7 @@ def change_reservation(reservation_id):
try:
reservation = ReservationRepository.get_by_id(reservation_id)
result = ReservationRepository.update(reservation_id, data)
notify_clients(reservation["room_id"])
notify_clients(reservation["date"])
if not result or result.matched_count == 0:
return jsonify({"error": "Reservation not found"}), 404
except:

View File

@ -17,20 +17,13 @@ def handle_disconnect():
@socketio.on('subscribe_reservations')
def handle_subscribe(data):
room_id = data.get('room_id')
date = data.get('date')
room = f"{room_id}_{date}"
join_room(room)
reservations = ReservationRepository.list_all({
'room_id': room_id,
'date': date
})
join_room(date) # Используем дату как комнату
# Получаем все резервации для указанной даты
reservations = ReservationRepository.list_all({'date': date})
emit('reservations_update', reservations)
def notify_clients(room_id, date):
room = f"{room_id}_{date}"
reservations = ReservationRepository.list_all({
'room_id': room_id,
'date': date
})
socketio.emit('reservations_update', reservations, room=room)
def notify_clients(date):
# Отправляем всем подписчикам этой даты
reservations = ReservationRepository.list_all({'date': date})
socketio.emit('reservations_update', reservations, room=date)