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 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 python src/app.py
pause pause

View File

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

View File

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