Skip to content

Commit 7a671df

Browse files
authored
Merge: 채팅방 입장시 상품에 대한 대여정보 내려주도록 변경
[hotfix/inspection] 채팅방 입장시 상품에 대한 대여정보 내려주도록 변경
2 parents 637d1fa + 5205415 commit 7a671df

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

apps/chat/serializers.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
read_messages_at_postgres,
1212
read_messages_at_redis,
1313
)
14-
from apps.product.models import ProductImage
14+
from apps.product.models import ProductImage, RentalHistory
15+
from apps.product.serializers import RentalHistoryStatusSerializer
1516

1617

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

8385
class Meta:
8486
model = Chatroom
85-
fields = ["product", "product_image", "product_name", "product_rental_fee", "product_condition"]
87+
fields = [
88+
"product", "product_image", "product_name", "product_rental_fee", "product_condition", "rental_history"
89+
]
8690

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

100-
def get_product_image(self, obj: ProductImage) -> Any:
104+
def get_product_image(self, obj: Chatroom) -> Any:
101105
if obj.product:
102106
product_images = obj.product.images.first()
103107
if product_images:
104108
return product_images.image.url # 이미지의 URL을 리턴
105109
return None # 이미지가 없을 경우 None을 리턴
110+
111+
def get_rental_history(self, obj: Chatroom) -> Optional[dict[str, Any]]:
112+
if obj.product:
113+
chat_product = obj.product
114+
try:
115+
rental_history = chat_product.rentalhistory_set.get(borrower=obj.borrower, product=obj.product)
116+
return RentalHistoryStatusSerializer(rental_history).data
117+
118+
except RentalHistory.DoesNotExist:
119+
return None
120+
return None

apps/chat/views.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
check_entered_chatroom,
1818
delete_chatroom,
1919
)
20+
from apps.product.serializers import RentalHistorySerializer, RentalHistoryStatusSerializer
2021
from apps.user.api_schema import UserInfoSerializer
2122

2223

@@ -85,6 +86,7 @@ class ChatDetailView(APIView):
8586
"product_rental_fee": serializer.CharField(),
8687
"product_condition": serializer.CharField(),
8788
"messages": serializers.MessageSerializer(many=True),
89+
"rental_history": RentalHistoryStatusSerializer()
8890
},
8991
),
9092
description="""

apps/product/serializers.py

+6
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,9 @@ def to_representation(self, instance: RentalHistory) -> dict[str, Any]:
183183
elif instance.status == "BORROWING":
184184
data["status"] = "대여 진행중"
185185
return data
186+
187+
188+
class RentalHistoryStatusSerializer(serializers.ModelSerializer[RentalHistory]):
189+
class Meta:
190+
model = RentalHistory
191+
fields = ("id", "status")

apps/product/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

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

0 commit comments

Comments
 (0)