Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update, delete, and get auction endpoints #14

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")),
]