Skip to content

Commit

Permalink
Add update, delete, and get auction endpoints (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ltan02 authored Nov 8, 2023
1 parent 698be50 commit 79d323f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions backend/auction/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.contrib import admin
from django.urls import include, path

from .views import AuctionListApiView
from .views import AuctionDetailApiView, AuctionListApiView

urlpatterns = [
path("", AuctionListApiView.as_view(), name="auction"),
path("", AuctionListApiView.as_view(), name="auction_list"),
path("<uuid:auction_id>/", AuctionDetailApiView.as_view(), name="auction_detail"),
]
26 changes: 25 additions & 1 deletion backend/auction/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from datetime import datetime

from rest_framework import status
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Auction
from .serializers import AuctionSerializer


# Create your views here.
class AuctionListApiView(APIView):
def get(self, request, *args, **kwargs):
"""
Expand Down Expand Up @@ -62,3 +62,27 @@ def post(self, request, *args, **kwargs):
return Response(serializer.data, status=status.HTTP_201_CREATED)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class AuctionDetailApiView(APIView):
"""
Retrieve, update or delete an auction instance.
"""

def get(self, request, auction_id, format=None):
auction = get_object_or_404(Auction, id=auction_id)
serializer = AuctionSerializer(auction)
return Response(serializer.data)

def put(self, request, auction_id, format=None):
auction = get_object_or_404(Auction, id=auction_id)
serializer = AuctionSerializer(auction, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, auction_id, format=None):
auction = get_object_or_404(Auction, id=auction_id)
auction.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
6 changes: 3 additions & 3 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django.urls import include, path

urlpatterns = [
path("admin", admin.site.urls),
path("api/v1/auction", include("auction.urls")),
path("api/v1/vehicle", include("vehicle.urls")),
path("admin/", admin.site.urls),
path("api/v1/auction/", include("auction.urls")),
path("api/v1/vehicle/", include("vehicle.urls")),
]

0 comments on commit 79d323f

Please sign in to comment.