Skip to content

Commit

Permalink
Create endpoint to check whether a request.user ordered a produc
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Dec 30, 2023
1 parent f30571a commit 1aa3e8d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
8 changes: 8 additions & 0 deletions backend/api/products_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ class Meta(ProductSerializer.Meta):
fields = ("name",)


class ProductUserOrderCheckSerializer(serializers.Serializer):
"""Serializer to check if the user has ordered a product."""

product = serializers.IntegerField(read_only=True)
user = serializers.IntegerField(read_only=True)
ordered = serializers.BooleanField(read_only=True)


class FavoriteProductSerializer(serializers.ModelSerializer):
"""Serializer for favorite products list representation."""

Expand Down
49 changes: 45 additions & 4 deletions backend/api/products_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
ValidationErrorResponseSerializer,
)
from drf_yasg.utils import swagger_auto_schema
from rest_framework import decorators, filters, permissions, response, status, viewsets
from rest_framework import filters, permissions, response, status, viewsets
from rest_framework.decorators import action

from .filters import ProductFilter
from .mixins import MESSAGE_ON_DELETE, DestroyWithPayloadMixin
Expand All @@ -26,10 +27,12 @@
ProductCreateSerializer,
ProductSerializer,
ProductUpdateSerializer,
ProductUserOrderCheckSerializer,
PromotionSerializer,
SubcategorySerializer,
TagSerializer,
)
from orders.models import OrderProduct
from products.models import (
Category,
Component,
Expand Down Expand Up @@ -134,7 +137,7 @@ def get_queryset(self):
responses={200: CategoryBriefSerializer},
),
)
@decorators.action(methods=["get"], detail=False, url_path="category-brief-list")
@action(methods=["get"], detail=False, url_path="category-brief-list")
def category_brief_list(self, request):
"""
Shows brief information about categories without indicating subcategories
Expand All @@ -159,7 +162,7 @@ def category_brief_list(self, request):
responses={200: CategoryBriefSerializer, 404: ErrorResponse404Serializer},
),
)
@decorators.action(methods=["get"], detail=True, url_path="category-brief-detail")
@action(methods=["get"], detail=True, url_path="category-brief-detail")
def category_brief_detail(self, request, pk):
"""
Shows brief information about a category without indicating subcategories
Expand Down Expand Up @@ -579,6 +582,8 @@ def get_serializer_class(self):
return ProductUpdateSerializer
if self.action == "favorite":
return FavoriteProductCreateSerializer
if self.action == "order_user_check":
return ProductUserOrderCheckSerializer
return ProductSerializer

def get_queryset(self):
Expand Down Expand Up @@ -666,7 +671,7 @@ def retrieve(self, request, *args, **kwargs):
404: ErrorResponse404Serializer,
},
)
@decorators.action(
@action(
methods=["post", "delete"],
detail=True,
permission_classes=[permissions.IsAuthenticated],
Expand All @@ -675,6 +680,42 @@ def favorite(self, request, pk):
product = get_object_or_404(Product, id=pk)
return self.create_delete_or_scold(FavoriteProduct, product, request)

# TODO: test this endpoint
@method_decorator(
name="retrieve",
decorator=swagger_auto_schema(
operation_summary="Check user product order",
responses={
200: ProductUserOrderCheckSerializer,
404: ErrorResponse404Serializer,
},
),
)
@action(
methods=["get"],
detail=True,
url_path="order-user-check",
permission_classes=[permissions.IsAuthenticated],
)
def order_user_check(self, request, pk):
"""Shows whether the request.user has ordered this product."""
product = get_object_or_404(Product, id=int(pk))
serializer = self.get_serializer_class()
payload = {
"product": pk,
"user": self.request.user.pk,
"ordered": OrderProduct.objects.filter(
product=product, order__user=self.request.user
).exists(),
}
return response.Response(
serializer(
payload,
context={"request": request, "format": self.format_kwarg, "view": self},
).data,
status=status.HTTP_200_OK,
)


@method_decorator(
name="list",
Expand Down
2 changes: 1 addition & 1 deletion backend/api/reviews_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def perform_update(self, serializer):
@method_decorator(
name="retrieve",
decorator=swagger_auto_schema(
operation_summary="Check user review",
operation_summary="Check user product review",
responses={200: ReviewUserCheckSerializer, 404: ErrorResponse404Serializer},
),
)
Expand Down

0 comments on commit 1aa3e8d

Please sign in to comment.