def add_reservation(hotel_id, room_type, arrival_date, departure_date, status): """ Add a new reservation to the system """ session = DBSession().get_db_session() # Check for invalid room type (doesn't exist in inventory for desired hotel) room_type_inventory = session.query(HotelInventory).filter(HotelInventory.hotel_id == hotel_id) \ .filter(HotelInventory.room_type == room_type) if len(room_type_inventory.all()) != 1: return OPERATION_ERROR_RETURN_CODE # Check for invalid reservation dates arrival_date = create_datetime_object(arrival_date) departure_date = create_datetime_object(departure_date) if departure_date <= arrival_date or arrival_date < datetime.date.today(): return OPERATION_ERROR_RETURN_CODE # Check availability in inventory before adding reservation max_occupancy = room_type_inventory.first().room_inventory for date in generate_days_list(arrival_date, departure_date): if not get_occupancy(hotel_id, date, room_type) < max_occupancy: return OPERATION_ERROR_RETURN_CODE reservation = Reservations(hotel_id=hotel_id, room_type=room_type, arrival_date=arrival_date, departure_date=departure_date, status=status) session.add(reservation) session.commit() return reservation.id
def cancel_reservation(reservation_id): """ Cancel an existing reservation. Note: this will only change the reservation's status to "Cancelled". """ session = DBSession().get_db_session() reservation = session.query(Reservations).get(reservation_id) reservation.status = CANCELLED_STATUS session.commit()
def get_inventory(hotel_id, room_type): """ Get the general inventory size of a specific room type at a specific hotel Note: This doesn't take into account any existing reservations """ session = DBSession().get_db_session() room_type_inventory = session.query(HotelInventory).filter(HotelInventory.hotel_id == hotel_id) \ .filter(HotelInventory.room_type == room_type).first() return room_type_inventory.room_inventory
def get_hotel_roomtypes(hotel_id): """ Get a given hotel's room types """ session = DBSession().get_db_session() room_inventories = session.query(HotelInventory).filter( HotelInventory.hotel_id == hotel_id) return list( map(lambda room_inventory: room_inventory.room_type, room_inventories))
def activate_reservation(reservation_id): """ Activate an existing reservation. Note: this will only change the reservation's status to "Active". """ session = DBSession().get_db_session() # If this functionality ever becomes an endpoint to 3rd parties, make sure to check # availability in inventory before activating reservation reservation = session.query(Reservations).get(reservation_id) reservation.status = ACTIVE_STATUS session.commit()
def get_occupancy(hotel_id, date, room_type): """ For a given hotel and room type at a certain date, return the amount of reserved rooms """ session = DBSession().get_db_session() return session.query(Reservations).filter(Reservations.arrival_date <= date)\ .filter(Reservations.departure_date > date)\ .filter(Reservations.hotel_id == hotel_id)\ .filter(Reservations.room_type == room_type)\ .filter(Reservations.status == ACTIVE_STATUS)\ .count()
def get_reservation(reservation_id): """ Retrieve an existing reservation from the system """ session = DBSession().get_db_session() reservation = session.query(Reservations).get(reservation_id) return { "reservation_id": reservation.id, "hotel_id": reservation.hotel_id, "room_type": reservation.room_type, "arrival_date": reservation.arrival_date.strftime(DATE_FORMAT), "departure_date": reservation.departure_date.strftime(DATE_FORMAT), "status": reservation.status }