diff --git a/.env.example b/.env.example index 0203916..3667da4 100644 --- a/.env.example +++ b/.env.example @@ -10,4 +10,5 @@ POSTGRES_HOST=db POSTGRES_PORT=5432 COMMAND=sh ./resources/scripts/entrypoint-server.sh -PORT=8080 \ No newline at end of file +PORT=8080 +CACHEOPS_ENABLED=True \ No newline at end of file diff --git a/config/conf/redis.py b/config/conf/redis.py index f4e8686..24b8c7b 100644 --- a/config/conf/redis.py +++ b/config/conf/redis.py @@ -1,3 +1,5 @@ +from config.env import env + CACHES = { "default": { "BACKEND": 'django_redis.cache.RedisCache', @@ -19,6 +21,19 @@ CACHEOPS = { "ops": "all", "timeout": 60 * 5, }, + 'orders.*': { + 'ops': 'all', + 'timeout': 60 * 5, + }, + 'products.*': { + 'ops': 'all', + 'timeout': 60 * 5, + }, + 'shared.*': { + 'ops': 'all', + 'timeout': 60 * 5, + } } + CACHEOPS_DEGRADE_ON_FAILURE = True -CACHEOPS_ENABLED = False \ No newline at end of file +CACHEOPS_ENABLED = env.bool('CACHEOPS_ENABLED', False) \ No newline at end of file diff --git a/core/apps/orders/serializers/order.py b/core/apps/orders/serializers/order.py index 669a62d..c536132 100644 --- a/core/apps/orders/serializers/order.py +++ b/core/apps/orders/serializers/order.py @@ -4,6 +4,7 @@ from rest_framework import serializers from core.apps.orders.models import Order, OrderItem from core.apps.products.models import Product +from core.apps.products.serializers.product import ProductListSerializer class OrderItemCreateSerializer(serializers.Serializer): @@ -42,4 +43,23 @@ class OrderCreateSerializer(serializers.Serializer): order.total_price = total_price order.save() return order - \ No newline at end of file + + +class OrderItemListSerializer(serializers.ModelSerializer): + product = ProductListSerializer() + + class Meta: + model = OrderItem + fields = [ + 'id', 'product', 'price', 'quantity' + ] + + +class OrderListSerializer(serializers.ModelSerializer): + items = OrderItemListSerializer(many=True) + + class Meta: + model = Order + fields = [ + 'id', 'total_price', 'items' + ] \ No newline at end of file diff --git a/core/apps/orders/urls.py b/core/apps/orders/urls.py index cdbec43..77a701f 100644 --- a/core/apps/orders/urls.py +++ b/core/apps/orders/urls.py @@ -4,4 +4,5 @@ from core.apps.orders.views import order as order_views urlpatterns = [ path('order/create/', order_views.OrderCreateApiView.as_view()), + path('order/list/', order_views.OrderListApiView.as_view()), ] \ No newline at end of file diff --git a/core/apps/orders/views/order.py b/core/apps/orders/views/order.py index af5e228..49374c9 100644 --- a/core/apps/orders/views/order.py +++ b/core/apps/orders/views/order.py @@ -25,4 +25,19 @@ class OrderCreateApiView(generics.GenericAPIView): 'error': serializer.errors, }, status=400 - ) \ No newline at end of file + ) + + +class OrderListApiView(generics.GenericAPIView): + serializer_class = serializers.OrderListSerializer + queryset = Order.objects.select_related('items', 'items__product') + permission_classes = [permissions.IsAuthenticated] + + def get(self, request): + orders = Order.objects.filter(user=request.user) + page = self.paginate_queryset(orders) + if page is not None: + serializer = self.serializer_class(page, many=True) + return self.get_paginated_response(serializer.data) + serializer = self.serializer_class(orders, many=True) + return Response(serializer.data, status=200) \ No newline at end of file