Skip to content

Commit

Permalink
Merge pull request #163 from sdp-tech/fix/order
Browse files Browse the repository at this point in the history
order api 수정
  • Loading branch information
Scanf-s authored Nov 22, 2024
2 parents b87b20b + f4a9b4b commit 7deba3d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Dict, List

from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from rest_framework import serializers

Expand Down Expand Up @@ -111,20 +112,27 @@ def create(self, validated_data: Dict[str, Any]) -> Order:
if materials_data:
material_instances: List[Any] = []
for material in materials_data:
material_instances.append(
ServiceMaterial.objects.get(
material_uuid=material["material_uuid"]
material_instance: ServiceMaterial = ServiceMaterial.objects.filter(
material_uuid=material["material_uuid"]
).first()
if material_instance is None:
raise ObjectDoesNotExist(
"material_uuid에 해당하는 선택 옵션에 존재하지 않습니다."
)
)
material_instances.append(material_instance)
order.materials.set(material_instances)

if options_data:
option_instances: List[Any] = []
option_price: int = 0
for option in options_data:
option_instance: ServiceOption = ServiceOption.objects.get(
option_instance: ServiceOption = ServiceOption.objects.filter(
option_uuid=option["option_uuid"]
)
).first()
if option_instance is None:
raise ObjectDoesNotExist(
"option_uuid에 해당하는 선택 옵션이 존재하지 않습니다."
)
option_instances.append(option_instance)
option_price += option_instance.option_price
order.additional_options.set(option_instances)
Expand Down
16 changes: 11 additions & 5 deletions order/views/order_view/customer_order_view/order_list_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import QuerySet
from rest_framework import status
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -54,24 +55,29 @@ def post(self, request, **kwargs) -> Response:
)
service = Service.objects.filter(service_uuid=service_uuid).first()
if not service:
raise Service.DoesNotExist
raise Service.DoesNotExist(
"service_uuid에 해당하는 서비스가 존재하지 않습니다."
)

serializer = OrderCreateSerializer(
data=request.data,
context={"order_user": request.user, "service": service},
)
serializer.is_valid(raise_exception=True)
serializer.save()
created_order: Order = serializer.save()

return Response(data=serializer.data, status=status.HTTP_201_CREATED)
return Response(
data={"order_uuid": created_order.order_uuid},
status=status.HTTP_201_CREATED,
)

except ValidationError as e:
return Response(
data={"message": str(e)}, status=status.HTTP_400_BAD_REQUEST
)
except Service.DoesNotExist:
except ObjectDoesNotExist as e:
return Response(
data={"message": "Service not found"},
data={"message": str(e)},
status=status.HTTP_404_NOT_FOUND,
)
except Exception as e:
Expand Down

0 comments on commit 7deba3d

Please sign in to comment.