Skip to content

Commit

Permalink
Merge pull request #35 from itsdeannat/feat/add-get-orders-functionality
Browse files Browse the repository at this point in the history
Feat/add get orders functionality
  • Loading branch information
itsdeannat authored Jan 14, 2025
2 parents 0a234e4 + 1be52c9 commit 6bedf25
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 16 deletions.
2 changes: 1 addition & 1 deletion brew/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class Meta:

class OrderSerializer(serializers.ModelSerializer):

id = serializers.IntegerField(read_only=True, help_text="The unique order id")
order_items = OrderItemSerializer(many=True, required=True, help_text="The items in the customer's order. Provide the `product_id` and quantity for each product.")
order_date = serializers.DateTimeField(read_only=True, help_text="Order date") # Set to readonly to return to the customer
status = serializers.CharField(read_only=True, help_text="Order status") # Set to readonly to return to the customer
payment_method = serializers.CharField(required=True, help_text="The payment method used")
id = serializers.CharField(read_only=True, help_text="The unique order id")


class Meta:
Expand Down
4 changes: 2 additions & 2 deletions brew/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from .views import UserSignupView
from .views import PingView
from .views import ProductViewSet
from .views import CreateOrderView
from .views import OrderViewSet

router = DefaultRouter()
router.register(r'products', ProductViewSet, basename='product')
router.register(r'orders', OrderViewSet, basename='order')


urlpatterns = [
path('api/signup/', UserSignupView.as_view(), name='signup'),
path('api/ping/', PingView.as_view(), name='ping'),
path('api/orders/', CreateOrderView.as_view(), name='create_order'),
path('api/', include(router.urls))
]
83 changes: 76 additions & 7 deletions brew/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework import viewsets
from rest_framework.viewsets import ReadOnlyModelViewSet
from drf_spectacular.utils import extend_schema, OpenApiExample, OpenApiResponse
from rest_framework.viewsets import ReadOnlyModelViewSet, GenericViewSet
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
from drf_spectacular.utils import extend_schema, OpenApiExample
from drf_spectacular.types import OpenApiTypes
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from .models import Product
from .models import Product, Order
from .serializers import ProductSerializer, UserSignupSerializer, OrderSerializer, BadRequestSerializer, UnauthorizedSerializer, NotFoundSerializer

# Create your views here.
Expand Down Expand Up @@ -168,11 +169,74 @@ def get(self, request, format=None):
}
return Response(content, status=status.HTTP_200_OK)

class CreateOrderView(APIView):
class OrderViewSet(CreateModelMixin, RetrieveModelMixin, GenericViewSet):

authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

pagination_class = None
serializer_class = OrderSerializer
queryset = Order.objects.all()

@extend_schema(
operation_id="retrieve_orders",
description="Returns a single order from the database",
responses={
200: OrderSerializer,
400: BadRequestSerializer,
401: UnauthorizedSerializer,
404: NotFoundSerializer
},
examples=[
OpenApiExample(
name="Successful Response",
description="",
value=[
{
"id": "13",
"payment_method": "Credit",
"order_date": "2025-01-11T03:17:47.746025Z",
"status": "in progress",
"order_items": [
{
"product_id": 2,
"quantity": 1,
"product_name": "blueberry muffin"
}
]
},
],
status_codes=["200"]
),
OpenApiExample(
name="Bad Request",
description="",
value={"detail": "The request body could not be read properly."},
response_only=True,
status_codes=["400"],
),
OpenApiExample(
name="Unauthorized",
description="",
value={"detail": "Authentication credentials were not provided."},
response_only=True,
status_codes=["401"],
),
OpenApiExample(
name="Not Found",
description="",
value={"detail": "No product matches the given query."},
response_only=True,
status_codes=["404"],
),
]
)
def retrieve(self, request, pk=None):
queryset = Order.objects.all()
user = get_object_or_404(queryset, pk=pk)
serializer = OrderSerializer(user)
return Response(serializer.data)

@extend_schema(
operation_id="create_order",
description="Order available products from the database",
Expand All @@ -189,11 +253,15 @@ class CreateOrderView(APIView):
description="",
value=[
{
"id": "13",
"payment_method": "Credit",
"order_date": "2025-01-11T03:17:47.746025Z",
"status": "in progress",
"order_items": [
{
"product_id": 1,
"quantity": 2
"product_id": 2,
"quantity": 1,
"product_name": "blueberry muffin"
}
]
},
Expand Down Expand Up @@ -223,11 +291,12 @@ class CreateOrderView(APIView):
)
]
)
def post(self, request, *args, **kwargs):
def create(self, request, *args, **kwargs):
serializer = OrderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)



91 changes: 85 additions & 6 deletions schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ paths:
examples:
SuccessfulResponse:
value:
- payment_method: Credit
- id: '13'
payment_method: Credit
order_date: '2025-01-11T03:17:47.746025Z'
status: in progress
order_items:
- product_id: 1
quantity: 2
- product_id: 2
quantity: 1
product_name: blueberry muffin
summary: Successful Response
application/x-www-form-urlencoded:
schema:
Expand All @@ -45,10 +49,14 @@ paths:
examples:
SuccessfulResponse:
value:
- payment_method: Credit
- id: '13'
payment_method: Credit
order_date: '2025-01-11T03:17:47.746025Z'
status: in progress
order_items:
- product_id: 1
quantity: 2
- product_id: 2
quantity: 1
product_name: blueberry muffin
summary: Successful Response
description: ''
'400':
Expand Down Expand Up @@ -83,6 +91,72 @@ paths:
detail: The requested resource was not found
summary: Not Found
description: ''
/api/orders/{id}/:
get:
operationId: retrieve_orders
description: Returns a single order from the database
parameters:
- in: path
name: id
schema:
type: integer
description: A unique integer value identifying this order.
required: true
tags:
- orders
security:
- JWTAuth: []
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
examples:
SuccessfulResponse:
value:
- id: '13'
payment_method: Credit
order_date: '2025-01-11T03:17:47.746025Z'
status: in progress
order_items:
- product_id: 2
quantity: 1
product_name: blueberry muffin
summary: Successful Response
description: ''
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/BadRequest'
examples:
BadRequest:
value:
detail: The request body could not be read properly.
summary: Bad Request
description: ''
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/Unauthorized'
examples:
Unauthorized:
value:
detail: Authentication credentials were not provided.
description: ''
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/NotFound'
examples:
NotFound:
value:
detail: No product matches the given query.
summary: Not Found
description: ''
/api/products/:
get:
operationId: list_products
Expand Down Expand Up @@ -291,6 +365,10 @@ components:
Order:
type: object
properties:
id:
type: integer
readOnly: true
description: The unique order id
payment_method:
type: string
description: The payment method used
Expand All @@ -310,6 +388,7 @@ components:
description: The items in the customer's order. Provide the `product_id`
and quantity for each product.
required:
- id
- order_date
- order_items
- payment_method
Expand Down

0 comments on commit 6bedf25

Please sign in to comment.