async def add_room_picture( room_id: int, file: UploadFile = File(...), db: Session = Depends(get_db), uuid: int = Depends(get_uuid_from_xtoken), ): room_path = f"/rooms/{room_id}" room, _ = Requester.room_srv_fetch("GET", room_path, {HTTP_200_OK}) if not AuthSender.has_permission_to_modify(room["owner_uuid"], uuid): raise UnauthorizedRequestError( "You can't add photos to another user room!") image_url, firebase_id = photouploader.upload_room_photo(file, room_id) room_photo_path = f"/rooms/{room_id}/photos" new_photo_request = {"url": image_url, "firebase_id": firebase_id} photo_response, _ = Requester.room_srv_fetch("POST", room_photo_path, {HTTP_201_CREATED}, payload=new_photo_request) # photo_id = photo_response["id"] # RoomPhotoDAO.add_new_room_photo(db, firebase_id, photo_id) return photo_response
async def update_room( payload: RoomUpdate, room_id: int, viewer_uuid: int = Depends(get_uuid_from_xtoken), ): path = f"/rooms/{room_id}" room, _ = Requester.room_srv_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) if not AuthSender.has_permission_to_modify(viewer_uuid, room["owner_uuid"]): raise BadRequestError("You can't update other users rooms!") room_req_payload = payload.dict(exclude_unset=True) path = f"/rooms/{room_id}" room, _ = Requester.room_srv_fetch( method="PATCH", path=path, expected_statuses={HTTP_200_OK}, payload=room_req_payload, ) # TODO: Patch room price in payment server return room
async def delete_booking(booking_id: int, uuid: int = Depends(get_uuid_from_xtoken)): path = f"/bookings/{booking_id}" booking, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) room_id = booking["roomId"] room_owner_id = booking["roomOwnerId"] if not AuthSender.has_permission_to_modify(uuid, room_owner_id): raise UnauthorizedRequestError("Can't reject other users bookings") path = f"/bookings/{booking_id}" book_deleted, _ = Requester.payment_fetch("DELETE", path, {HTTP_200_OK}) # Delete the rejected booking in post server, # if it is not found it is also OK! booking_path = f"/rooms/{room_id}/bookings/{booking_id}" booking, _ = Requester.room_srv_fetch("DELETE", booking_path, {HTTP_200_OK, HTTP_404_NOT_FOUND}) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(book_deleted) return booking_camel
async def accept_booking(booking_id: int, uuid: int = Depends(get_uuid_from_xtoken)): path = f"/bookings/{booking_id}" booking, _ = Requester.payment_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) room_owner_id = booking["roomOwnerId"] if not AuthSender.has_permission_to_modify(uuid, room_owner_id): raise UnauthorizedRequestError("Can't accept other users bookings") path = f"/bookings/{booking_id}/accept" payload_accept = {"roomOwnerId": room_owner_id} book_accepted, _ = Requester.payment_fetch("POST", path, {HTTP_200_OK}, payload=payload_accept) # TODO: Change BookingDB model to match camelcase in payment server booking_camel = payment_camel_to_snake(book_accepted) # Send notification sender, _ = Requester.user_srv_fetch( "GET", f"/users/{booking_camel['room_owner_id']}", {HTTP_200_OK}) sender_name = f"{sender['firstname']} {sender['lastname']}" room, _ = Requester.room_srv_fetch("GET", f"/rooms/{booking_camel['room_id']}", {HTTP_200_OK}) room_title = room["title"] notifier.send_booking_accepted_notification(sender_name, room_title, booking_camel["booker_id"]) return booking_camel
async def delete_guest_rating( user_id: int, rating_id: int, uuid: int = Depends(get_uuid_from_xtoken), ): rating_path = f"/users/{user_id}/guest_ratings/{rating_id}" if not AuthSender.has_permission_to_modify(uuid, user_id): raise UnauthorizedRequestError( "You can't delete a rating of another user") review, _ = Requester.user_srv_fetch(method="DELETE", path=rating_path, expected_statuses={HTTP_200_OK}) return review
async def update_user( user_id: int, payload: UserUpdateSchema, uuid: int = Depends(get_uuid_from_xtoken), ): if not AuthSender.has_permission_to_modify(uuid, user_id): raise UnauthorizedRequestError( "You can't update info about other users") path = f"/users/{user_id}" new_user_info, _ = Requester.user_srv_fetch( method="PATCH", path=path, expected_statuses={HTTP_200_OK}, payload=payload.dict(exclude_unset=True), ) return new_user_info
async def delete_room_comment( room_id: int, comment_id: int, viewer_uuid: int = Depends(get_uuid_from_xtoken), ): comment_path = f"/rooms/{room_id}/comments/{comment_id}" comment, _ = Requester.room_srv_fetch(method="GET", path=comment_path, expected_statuses={HTTP_200_OK}) if not AuthSender.has_permission_to_modify(viewer_uuid, comment["commentator_id"]): raise BadRequestError("You can't delete other users room comments!") comment, _ = Requester.room_srv_fetch(method="DELETE", path=comment_path, expected_statuses={HTTP_200_OK}) return comment
async def delete_room_review( room_id: int, review_id: int, viewer_uuid: int = Depends(get_uuid_from_xtoken), ): review_path = f"/rooms/{room_id}/reviews/{review_id}" review, _ = Requester.room_srv_fetch(method="GET", path=review_path, expected_statuses={HTTP_200_OK}) if not AuthSender.has_permission_to_modify(viewer_uuid, review["reviewer_id"]): raise BadRequestError("You can't delete other users room reviews!") review, _ = Requester.room_srv_fetch(method="DELETE", path=review_path, expected_statuses={HTTP_200_OK}) return review
async def delete_user(user_id: int, uuid: int = Depends(get_uuid_from_xtoken), x_access_token: Optional[str] = Header(None)): auth_header = {"x-access-token": x_access_token} if not AuthSender.has_permission_to_modify(uuid, user_id): raise UnauthorizedRequestError("You can't delete other users") path = f"/users/{user_id}" new_user_info, _ = Requester.user_srv_fetch( method="DELETE", path=path, expected_statuses={HTTP_200_OK}) auth_path = f"/user/registered/{uuid}" Requester.auth_srv_fetch("DELETE", path=auth_path, expected_statuses={HTTP_200_OK}, extra_headers=auth_header) return new_user_info
async def delete_room(room_id: int, viewer_uuid: int = Depends(get_uuid_from_xtoken)): path = "/rooms" + f"/{room_id}" room, _ = Requester.room_srv_fetch(method="GET", path=path, expected_statuses={HTTP_200_OK}) if not AuthSender.has_permission_to_modify(viewer_uuid, room["owner_uuid"]): raise BadRequestError("You can't delete other users rooms!") path = "/rooms" + f"/{room_id}" room, _ = Requester.room_srv_fetch(method="DELETE", path=path, expected_statuses={HTTP_200_OK}) room_pay, _ = Requester.payment_fetch(method="DELETE", path=path, expected_statuses={HTTP_200_OK}) return room
async def delete_room_photo( room_id: int, firebase_id: int, db: Session = Depends(get_db), uuid: int = Depends(get_uuid_from_xtoken), ): room_path = f"/rooms/{room_id}" room, _ = Requester.room_srv_fetch("GET", room_path, {HTTP_200_OK}) if not AuthSender.has_permission_to_modify(room["owner_uuid"], uuid): raise UnauthorizedRequestError( "You can't delete photos of another user room!") # photo = RoomPhotoDAO.delete_room_photo(db, firebase_id) # if photo is None: # raise NotFoundError("Photo id") # photo_id = photo["room_photo_id"] # room_photo_path = f"/rooms/{room_id}/photos/{photo_id}" room_photo_path = f"/rooms/{room_id}/photos/{firebase_id}" photo_response, _ = Requester.room_srv_fetch("DELETE", room_photo_path, {HTTP_200_OK}) return photo_response