Added websocket unscription
This commit is contained in:
parent
17cc489fc9
commit
869d3188de
@ -18,6 +18,7 @@ def create_reservation():
|
|||||||
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.date, reservation.room_id)
|
notify_clients(reservation.date, reservation.room_id)
|
||||||
|
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'])
|
||||||
@ -29,8 +30,11 @@ def cancel_reservation(reservation_id):
|
|||||||
|
|
||||||
room_id = reservation["room_id"]
|
room_id = reservation["room_id"]
|
||||||
date = reservation["date"]
|
date = reservation["date"]
|
||||||
|
|
||||||
result = ReservationRepository.delete(reservation_id)
|
result = ReservationRepository.delete(reservation_id)
|
||||||
|
|
||||||
notify_clients(date, room_id)
|
notify_clients(date, room_id)
|
||||||
|
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
|
||||||
@ -45,6 +49,7 @@ def change_reservation(reservation_id):
|
|||||||
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["date"], reservation["room_id"])
|
notify_clients(reservation["date"], 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:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from flask_jwt_extended import get_jwt_identity, jwt_required
|
from flask_jwt_extended import get_jwt_identity, jwt_required
|
||||||
from flask_socketio import emit, join_room
|
from flask_socketio import emit, join_room, leave_room
|
||||||
|
|
||||||
from infra.server import socketio
|
from infra.server import socketio
|
||||||
from repos.reservation_repo import ReservationRepository
|
from repos.reservation_repo import ReservationRepository
|
||||||
@ -31,6 +31,14 @@ def handle_subscribe(data):
|
|||||||
reservations = ReservationRepository.list_all(filters)
|
reservations = ReservationRepository.list_all(filters)
|
||||||
emit('reservations_update', reservations)
|
emit('reservations_update', reservations)
|
||||||
|
|
||||||
|
@socketio.on('unsubscribe_reservations')
|
||||||
|
def handle_unsubscribe(data):
|
||||||
|
date = data.get('date')
|
||||||
|
leave_room(date)
|
||||||
|
if 'room_id' in data:
|
||||||
|
room = f"{date}_{data['room_id']}"
|
||||||
|
leave_room(room)
|
||||||
|
|
||||||
def notify_clients(date, room_id=None):
|
def notify_clients(date, room_id=None):
|
||||||
all_reservations = ReservationRepository.list_all({'date': date})
|
all_reservations = ReservationRepository.list_all({'date': date})
|
||||||
socketio.emit('reservations_update', all_reservations, room=date)
|
socketio.emit('reservations_update', all_reservations, room=date)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user