Skip to content

Commit

Permalink
Merge: 채팅방 입장시 상품에 대한 대여정보 내려주도록 변경
Browse files Browse the repository at this point in the history
[hotfix/inspection] 채팅방 입장시 상품에 대한 대여정보 내려주도록 변경
  • Loading branch information
yyysolhhh authored May 30, 2024
2 parents 637d1fa + 5205415 commit 7a671df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
21 changes: 18 additions & 3 deletions apps/chat/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
read_messages_at_postgres,
read_messages_at_redis,
)
from apps.product.models import ProductImage
from apps.product.models import ProductImage, RentalHistory
from apps.product.serializers import RentalHistoryStatusSerializer


class ChatroomListSerializer(serializers.ModelSerializer[Chatroom]):
Expand Down Expand Up @@ -79,10 +80,13 @@ class EnterChatroomSerializer(serializers.ModelSerializer[Chatroom]):
product_name = serializers.CharField(source="product.name", read_only=True)
product_rental_fee = serializers.IntegerField(source="product.rental_fee", read_only=True)
product_condition = serializers.CharField(source="product.condition", read_only=True)
rental_history = serializers.SerializerMethodField()

class Meta:
model = Chatroom
fields = ["product", "product_image", "product_name", "product_rental_fee", "product_condition"]
fields = [
"product", "product_image", "product_name", "product_rental_fee", "product_condition", "rental_history"
]

def to_representation(self, instance: Chatroom) -> Dict[str, Any]:
data = super().to_representation(instance)
Expand All @@ -97,9 +101,20 @@ def to_representation(self, instance: Chatroom) -> Dict[str, Any]:
data["messages"] = messages
return data

def get_product_image(self, obj: ProductImage) -> Any:
def get_product_image(self, obj: Chatroom) -> Any:
if obj.product:
product_images = obj.product.images.first()
if product_images:
return product_images.image.url # 이미지의 URL을 리턴
return None # 이미지가 없을 경우 None을 리턴

def get_rental_history(self, obj: Chatroom) -> Optional[dict[str, Any]]:
if obj.product:
chat_product = obj.product
try:
rental_history = chat_product.rentalhistory_set.get(borrower=obj.borrower, product=obj.product)
return RentalHistoryStatusSerializer(rental_history).data

except RentalHistory.DoesNotExist:
return None
return None
2 changes: 2 additions & 0 deletions apps/chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
check_entered_chatroom,
delete_chatroom,
)
from apps.product.serializers import RentalHistorySerializer, RentalHistoryStatusSerializer
from apps.user.api_schema import UserInfoSerializer


Expand Down Expand Up @@ -85,6 +86,7 @@ class ChatDetailView(APIView):
"product_rental_fee": serializer.CharField(),
"product_condition": serializer.CharField(),
"messages": serializers.MessageSerializer(many=True),
"rental_history": RentalHistoryStatusSerializer()
},
),
description="""
Expand Down
6 changes: 6 additions & 0 deletions apps/product/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,9 @@ def to_representation(self, instance: RentalHistory) -> dict[str, Any]:
elif instance.status == "BORROWING":
data["status"] = "대여 진행중"
return data


class RentalHistoryStatusSerializer(serializers.ModelSerializer[RentalHistory]):
class Meta:
model = RentalHistory
fields = ("id", "status")
4 changes: 2 additions & 2 deletions apps/product/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

urlpatterns = [
path("", include(router.urls)),
path("rental_history/borrow", RentalHistoryBorrowerView.as_view(), name="borrowed_rental_history"),
path("rental_history/lending", RentalHistoryLenderView.as_view(), name="lending_rental_history"),
path("rental_history/borrow/", RentalHistoryBorrowerView.as_view(), name="borrowed_rental_history"),
path("rental_history/lending/", RentalHistoryLenderView.as_view(), name="lending_rental_history"),
path("rental_history/<int:pk>/", RentalHistoryUpdateView.as_view(), name="rental_history_update"),
# path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

0 comments on commit 7a671df

Please sign in to comment.