85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from flask_socketio import emit
|
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
|
|
|
from src.controllers.websocket_controller import notify_clients
|
|
from src.middlewares.validate_request import validate_request
|
|
from src.dtos.reservation_dtos import CreateReservationDTO, UpdateReservationDTO
|
|
from models.reservation_model import Reservation
|
|
from src.repos.reservation_repo import ReservationRepository
|
|
|
|
reservation_blueprint = Blueprint('reservation', __name__)
|
|
|
|
@reservation_blueprint.route('/', methods=['POST'])
|
|
@validate_request(CreateReservationDTO)
|
|
#@jwt_required()
|
|
def create_reservation():
|
|
current_user = ""#get_jwt_identity()
|
|
reservation = Reservation(**request.json, creator=current_user)
|
|
id = ReservationRepository.insert(reservation)
|
|
notify_clients(reservation.date, reservation.room_id)
|
|
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(date, room_id)
|
|
notify_clients(date)
|
|
|
|
if not result or result.deleted_count == 0:
|
|
return jsonify({"error": "Reservation not found"}), 404
|
|
return jsonify({"message": "Reservation cancelled"})
|
|
|
|
@reservation_blueprint.route('/<reservation_id>', methods=['PUT'])
|
|
@validate_request(UpdateReservationDTO)
|
|
#@jwt_required()
|
|
def change_reservation(reservation_id):
|
|
data = request.json
|
|
try:
|
|
reservation = ReservationRepository.get_by_id(reservation_id)
|
|
|
|
result = ReservationRepository.update(reservation_id, data)
|
|
|
|
notify_clients(reservation["date"], reservation["room_id"])
|
|
notify_clients(reservation["date"])
|
|
if not result or result.matched_count == 0:
|
|
return jsonify({"error": "Reservation not found"}), 404
|
|
except:
|
|
return jsonify({"error": "Invalid reservation ID"}), 400
|
|
|
|
return jsonify({"message": "Reservation updated"})
|
|
|
|
@reservation_blueprint.route('/<reservation_id>', methods=['GET'])
|
|
#@jwt_required()
|
|
def get_reservation(reservation_id):
|
|
try:
|
|
reservation = ReservationRepository.get_by_id(reservation_id)
|
|
if not reservation:
|
|
return jsonify({"error": "Reservation not found"}), 404
|
|
except:
|
|
return jsonify({"error": "Invalid reservation ID"}), 400
|
|
|
|
return jsonify(reservation)
|
|
|
|
@reservation_blueprint.route('/', methods=['GET'])
|
|
#@jwt_required()
|
|
def list_reservations():
|
|
filters = {
|
|
"date": request.args.get("date"),
|
|
"room_id": request.args.get("room_id")
|
|
}
|
|
filters = {k: v for k, v in filters.items() if v is not None}
|
|
|
|
reservations = ReservationRepository.list_all(filters)
|
|
print(reservations)
|
|
return jsonify(reservations) |