Skip to content

Commit

Permalink
creating Order API - Getting Order
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmalshahabudeen committed Apr 25, 2023
1 parent 9c54f0f commit d2df7d3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class Meta(BaseUserCreateSerializer.Meta):

class UserSerializer(BaseUserSerializer):
class Meta(BaseUserSerializer.Meta):
fields = ['id', 'username', 'email', 'first_name', 'last_name']
fields = ['id', 'username', 'email', 'first_name', 'last_name']

2 changes: 1 addition & 1 deletion store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Meta:


class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.PROTECT)
order = models.ForeignKey(Order, on_delete=models.PROTECT, related_name='items')
product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name='orderitems')
quantity = models.PositiveIntegerField()
unit_price = models.DecimalField(max_digits=6, decimal_places=2)
Expand Down
19 changes: 17 additions & 2 deletions store/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from decimal import Decimal
from store.models import Customer, Product, Collection, Review, Cart, CartItem
from store.models import Customer, Product, Collection, Review, Cart, CartItem, Order, OrderItem
from rest_framework import serializers

class SimpleProductSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -107,4 +107,19 @@ class CustomerSerializer(serializers.ModelSerializer):

class Meta:
model = Customer
fields = ['id', 'user_id', 'phone', 'birth_date', 'membership']
fields = ['id', 'user_id', 'phone', 'birth_date', 'membership']


class OrderItemSerializer(serializers.ModelSerializer):
product = SimpleProductSerializer()

class Meta:
model = OrderItem
fields = ['id', 'product', 'unit_price', 'quantity']

class OrderSerializers(serializers.ModelSerializer):
items = OrderItemSerializer(many=True)

class Meta:
model = Order
fields = ['id', 'customer', 'placed_at', 'payment_status', 'items']
1 change: 1 addition & 0 deletions store/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
router.register('collections', views.CollectionViewSet)
router.register('carts', views.CartViewSet)
router.register('customers', views.CustomerViewSet)
router.register('orders', views.OrderViewSet)
# pprint(router.urls)

# for nested routers
Expand Down
10 changes: 7 additions & 3 deletions store/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework import status
from .models import Customer, Product, Collection, OrderItem, Review, Cart, CartItem
from .models import Customer, Product, Collection, OrderItem, Review, Cart, CartItem, Order
from .filters import ProductFilter
from .serializers import CustomerSerializer, ProductSerializer, CollectionSerializer, ReviewSerializer, CartSerializer, CartItemSerializer, AddCartItemSerializer, UpdateCartItemSerializer
from .serializers import CustomerSerializer, ProductSerializer, CollectionSerializer, ReviewSerializer, CartSerializer, CartItemSerializer, AddCartItemSerializer, UpdateCartItemSerializer, OrderSerializers
from .pagination import DefaultPagination
from .permissions import IsAdminOrReadOnly, FullDjangoModelPermissions, ViewCustomerHistoryPermission

Expand Down Expand Up @@ -114,4 +114,8 @@ def me(self, request):

@action(detail=True, permission_classes=[ViewCustomerHistoryPermission])
def history(self, request, pk):
return Response('ok')
return Response('ok')

class OrderViewSet(ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrderSerializers

0 comments on commit d2df7d3

Please sign in to comment.