add: add order list api
This commit is contained in:
@@ -10,4 +10,5 @@ POSTGRES_HOST=db
|
||||
POSTGRES_PORT=5432
|
||||
|
||||
COMMAND=sh ./resources/scripts/entrypoint-server.sh
|
||||
PORT=8080
|
||||
PORT=8080
|
||||
CACHEOPS_ENABLED=True
|
||||
@@ -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
|
||||
CACHEOPS_ENABLED = env.bool('CACHEOPS_ENABLED', False)
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
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'
|
||||
]
|
||||
@@ -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()),
|
||||
]
|
||||
@@ -25,4 +25,19 @@ class OrderCreateApiView(generics.GenericAPIView):
|
||||
'error': serializer.errors,
|
||||
},
|
||||
status=400
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user