def delete_comment(cls, db, room_id, comment_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") comment = db.query(RoomComment).get(comment_id) if comment is None: raise NotFoundError("room comment") if not comment.is_from(room_id): raise NoRelationError("room", "room comment") db.delete(comment) if not comment.is_answer(): answers = db.query(RoomComment)\ .filter(RoomComment.main_comment_id == comment_id)\ .all() for answer in answers: db.delete(answer) db.commit() return comment.serialize()
async def update_room(payload: RoomPatch, room_id: int, db: Session = Depends(get_db), api_key: Optional[str] = Header(None)): auth_service.verify_apy_key(api_key) room_info = RoomDAO.update_room(db, room_id, payload) return room_info
def get_all_ratings(cls, db, room_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") rating_list = db.query(RoomRating)\ .filter(room_id == RoomRating.room_id)\ .all() serialized_list = [] for rating in rating_list: serialized_list.append(rating.serialize()) return serialized_list
def get_room_review(cls, db, room_id, review_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") room_review = db.query(RoomReview).get(review_id) if room_review is None: raise NotFoundError("room review") if not room_review.is_from(room_id): raise NoRelationError("room", "room review") return room_review.serialize()
def get_room_rating(cls, db, room_id, rating_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") room_rating = db.query(RoomRating).get(rating_id) if room_rating is None: raise NotFoundError("room rating") if not room_rating.is_from(room_id): raise NoRelationError("room", "room rating") return room_rating.serialize()
def get_comment(cls, db, room_id, comment_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") comment = db.query(RoomComment).get(comment_id) if comment is None: raise NotFoundError("room comment") if not comment.is_from(room_id): raise NoRelationError("room", "room comment") return comment.serialize()
def add_new_room_review(cls, db, room_id, room_review_args): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") new_room_review = RoomReview( review=room_review_args.review, room_id=room_id, reviewer=room_review_args.reviewer, reviewer_id=room_review_args.reviewer_id, ) db.add(new_room_review) db.commit() return new_room_review.serialize()
def get_comment_with_answers(cls, db, room_id, comment_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") comment = db.query(RoomComment).get(comment_id) answers = db.query(RoomComment) \ .filter(comment.id == RoomComment.main_comment_id) \ .all() serialized_answers = [] for answer in answers: serialized_answers.append(answer.serialize()) return {"comment": comment.serialize(), "answers": serialized_answers}
def add_new_room_rating(cls, db, room_id, room_rating_args): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") new_room_rating = RoomRating( rating=room_rating_args.rating, room_id=room_id, reviewer=room_rating_args.reviewer, reviewer_id=room_rating_args.reviewer_id, ) db.add(new_room_rating) db.commit() return new_room_rating.serialize()
def get_all_comments(cls, db, room_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") main_comments = db.query(RoomComment) \ .filter(room_id == RoomComment.room_id and RoomComment.main_comment_id is None) \ .all() all_comments = [] for comment in main_comments: comment_with_answers = cls.get_comment_with_answers( db, room_id, comment.id) all_comments.append(comment_with_answers) return all_comments
def delete_room_booking(cls, db, room_id, booking_id): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") room_booking = db.query(RoomBooking)\ .get(booking_id) if room_booking is None: raise NotFoundError("room booking") if not room_booking.is_from(room_id): raise NoRelationError("room", "room booking") db.delete(room_booking) db.commit() return room_booking.serialize()
async def get_all_rooms( db: Session = Depends(get_db), api_key: Optional[str] = Header(None), date_from: Optional[str] = None, date_to: Optional[str] = None, longitude: Optional[float] = None, latitude: Optional[float] = None, people: Optional[int] = None, types: List[str] = Query(None), owner_uuid: Optional[int] = None, min_price: Optional[int] = None, max_price: Optional[int] = None, allow_blocked: Optional[bool] = False, only_blocked: Optional[bool] = False, ids: List[int] = Query(None), ): auth_service.verify_apy_key(api_key) rooms_list = RoomDAO.get_all_rooms(db, date_from, date_to, longitude, latitude, people, types, owner_uuid, min_price, max_price, allow_blocked, only_blocked, ids) amount_rooms = len(rooms_list) return {"amount": amount_rooms, "rooms": rooms_list}
def add_new_comment(cls, db, room_id, comment_args): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") if comment_args.main_comment_id is not None: if not cls.comment_is_present(db, comment_args.main_comment_id): raise NotFoundError("room comment") main_comment = db.query(RoomComment).get( comment_args.main_comment_id) if main_comment.is_answer(): raise MainCommentIsAnswerError() new_comment = RoomComment(comment=comment_args.comment, room_id=room_id, commentator=comment_args.commentator, commentator_id=comment_args.commentator_id, main_comment_id=comment_args.main_comment_id) db.add(new_comment) db.commit() return new_comment.serialize()
def add_new_room_booking(cls, db, room_id, room_booking_args): if not RoomDAO.room_is_present(db, room_id): raise NotFoundError("room") booking_to = room_booking_args.date_to booking_from = room_booking_args.date_from bookings_on_same_date = cls.bookings_on_same_date( db, room_id, booking_from, booking_to) if bookings_on_same_date > 0: raise RoomAlreadyBookedError() new_room_booking = RoomBooking( id=room_booking_args.id, room_id=room_id, date_to=booking_to, date_from=booking_from, ) db.add(new_room_booking) db.commit() return new_room_booking.serialize()
async def create_room(payload: RoomSchema, db: Session = Depends(get_db), api_key: Optional[str] = Header(None)): auth_service.verify_apy_key(api_key) room_info = RoomDAO.add_new_room(db, payload) return room_info
async def get_recomended_rooms(db: Session = Depends(get_db), api_key: Optional[str] = Header(None)): auth_service.verify_apy_key(api_key) rooms_list = RoomDAO.get_recomended_rooms(db) amount_rooms = len(rooms_list) return {"amount": amount_rooms, "rooms": rooms_list}
async def delete_room(room_id: int, db: Session = Depends(get_db), api_key: Optional[str] = Header(None)): auth_service.verify_apy_key(api_key) room_info = RoomDAO.delete_room(db, room_id) return room_info