Skip to content

Commit

Permalink
backend order add views created
Browse files Browse the repository at this point in the history
  • Loading branch information
coderj001 committed Jun 19, 2021
1 parent ccbe684 commit ecfe088
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 9 deletions.
31 changes: 31 additions & 0 deletions backend/core/migrations/0004_auto_20210619_1245.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.2 on 2021-06-19 12:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0003_product_image'),
]

operations = [
migrations.AlterModelOptions(
name='orderitem',
options={'verbose_name': 'OrderItem', 'verbose_name_plural': 'OrderItems'},
),
migrations.AlterModelOptions(
name='shippingaddress',
options={'verbose_name': 'ShippingAddress', 'verbose_name_plural': 'ShippingAddresses'},
),
migrations.AddField(
model_name='order',
name='shippingPrice',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True),
),
migrations.AddField(
model_name='order',
name='totalPrice',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2021-06-19 12:51

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0004_auto_20210619_1245'),
]

operations = [
migrations.RenameField(
model_name='shippingaddress',
old_name='postalCode',
new_name='pincode',
),
]
54 changes: 47 additions & 7 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,35 @@ class Order(models.Model):
related_name='order',
on_delete=models.CASCADE
)
paymentMethord = models.CharField(max_length=255, blank=True, null=True)
paymentMethord = models.CharField(
max_length=255,
blank=True,
null=True
)
taxPrice = models.DecimalField(
max_digits=7, decimal_places=2, blank=True, null=True)
max_digits=7,
decimal_places=2,
blank=True,
null=True
)
shippingPrice = models.DecimalField(
max_digits=7,
decimal_places=2,
blank=True,
null=True
)
totalPrice = models.DecimalField(
max_digits=7,
decimal_places=2,
blank=True,
null=True
)
isPaid = models.BooleanField(default=False)
createdAt = models.DateTimeField(auto_now_add=False, blank=True, null=True)
createdAt = models.DateTimeField(
auto_now_add=False,
blank=True,
null=True
)
isDeliverd = models.BooleanField(default=False)
deliverdAt = models.DateTimeField(
auto_now_add=False,
Expand Down Expand Up @@ -129,10 +153,26 @@ class ShippingAddress(models.Model):
null=True,
blank=True
)
address = models.CharField(max_length=200, null=True, blank=True)
city = models.CharField(max_length=200, null=True, blank=True)
postalCode = models.CharField(max_length=200, null=True, blank=True)
country = models.CharField(max_length=200, null=True, blank=True)
address = models.CharField(
max_length=200,
null=True,
blank=True
)
city = models.CharField(
max_length=200,
null=True,
blank=True
)
pincode = models.CharField(
max_length=200,
null=True,
blank=True
)
country = models.CharField(
max_length=200,
null=True,
blank=True
)
shippingPrice = models.DecimalField(
max_digits=7,
decimal_places=2,
Expand Down
42 changes: 42 additions & 0 deletions backend/core/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,45 @@ class Meta:
model = Product
fields = '__all__'
# Product Serializer - end


class ShippingAddressSerializer(ModelSerializer):
class Meta:
model = ShippingAddress
fields = '__all__'


class OrderItemSerializer(ModelSerializer):
class Meta:
model = OrderItem
fields = '__all__'


class OrderSerializer(ModelSerializer):
orders = SerializerMethodField(read_only=True)
shippingAddress = SerializerMethodField(read_only=True)
user = SerializerMethodField(read_only=True)

class Meta:
model = Order
fields = '__all__'

def get_orders(self, obj):
items = obj.orderitem_set.all()
serializer = OrderItemSerializer(items, many=True)
return serializer.data

def get_shippingAddress(self, obj):
try:
address = ShippingAddressSerializer(
obj.shippingAddress,
many=False
)
except:
address = False
return address

def get_user(self, obj):
user = obj.user
serializer = UserSerializer(user, many=False)
return serializer.data
4 changes: 3 additions & 1 deletion backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
getUserProfile,
getUsers,
registerUser,
updateUserProfile
updateUserProfile,
addOrderItem
)

app_name = 'core'
Expand All @@ -25,4 +26,5 @@
path('product/<int:id>/', getProduct, name="get-product"),

# orders urls
path('orders/add', addOrderItem, name="order-add"),
]
57 changes: 56 additions & 1 deletion backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
MyTokenObtainPairSerializer,
ProductSerializer,
UserSerializer,
UserSerializerWithToken
UserSerializerWithToken,
OrderSerializer
)

# Users views
Expand Down Expand Up @@ -100,4 +101,58 @@ def getProduct(request, id):
# Products views - end

# Order views


@api_view(['POST'])
@permission_classes(['IsAuthenticated'])
def addOrderItem(request):
user = request.user
data = request.data

orderItems = data['orderItems']

if orderItems and len(orderItems) == 0:
return Response(
{'detail': 'No Order Items'},
status=HTTP_400_BAD_REQUEST
)
else:
# (1): Create Order
order = Order.objects.create(
user=user,
paymentMethord=data.get('paymentMethord'),
taxPrice=data.get('taxPrice'),
shippingPrice=data.get('shippingPrice'),
totalPrice=data.get('totalPrice')
)
# (2): Shipping Address
shipping = ShippingAddress.objects.create(
order=order,
address=data.get('shippingAddress').get('address'),
city=data.get('shippingAddress').get('city'),
pincode=data.get('shippingAddress').get('pincode'),
country=data.get('shippingAddress').get('country'),
)
# (3): Create OrderItem and set order to orderItems relationship
for item in orderItems:
product = Product.objects.get(_id=item.get('product'))
item = OrderItem.objects.create(
product=product,
order=order,
name=product.name,
qty=item.get('qtn'),
price=item.get('price'),
image=product.image.url
)

# (4) Update Stock

product.countInStock -= item.qty
product.save()

serializer = OrderSerializer(order, many=True)

return Response(serializer.data)


# Order views - end

0 comments on commit ecfe088

Please sign in to comment.