From 934fd615f5ce1f287a37ed291936b25fd1246c7a Mon Sep 17 00:00:00 2001 From: Lance Tan Date: Tue, 7 Nov 2023 16:32:47 -0800 Subject: [PATCH 1/2] Add endpoints --- backend/auction/urls.py | 6 ++++-- backend/auction/views.py | 25 ++++++++++++++++++++++++- backend/core/urls.py | 4 ++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/backend/auction/urls.py b/backend/auction/urls.py index 1b22cf2..7254896 100644 --- a/backend/auction/urls.py +++ b/backend/auction/urls.py @@ -1,8 +1,10 @@ from django.contrib import admin from django.urls import include, path -from .views import AuctionListApiView +from .views import AuctionListApiView, AuctionDetailApiView + urlpatterns = [ - path("", AuctionListApiView.as_view(), name="auction"), + path("", AuctionListApiView.as_view(), name="auction_list"), + path("/", AuctionDetailApiView.as_view(), name="auction_detail"), ] diff --git a/backend/auction/views.py b/backend/auction/views.py index ec6ea04..1b2ad66 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -3,12 +3,12 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView +from rest_framework.generics import get_object_or_404 from .models import Auction from .serializers import AuctionSerializer -# Create your views here. class AuctionListApiView(APIView): def get(self, request, *args, **kwargs): """ @@ -62,3 +62,26 @@ 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) \ No newline at end of file diff --git a/backend/core/urls.py b/backend/core/urls.py index 1337ff4..5fd7f50 100644 --- a/backend/core/urls.py +++ b/backend/core/urls.py @@ -18,6 +18,6 @@ from django.urls import include, path urlpatterns = [ - path("admin", admin.site.urls), - path("api/v1/auction", include("auction.urls")), + path("admin/", admin.site.urls), + path("api/v1/auction/", include("auction.urls")), ] From 9d60dfdf00507a0910a5981bc1be2a1e5b94fbb6 Mon Sep 17 00:00:00 2001 From: Lance Tan Date: Tue, 7 Nov 2023 16:34:43 -0800 Subject: [PATCH 2/2] Fix styling --- backend/auction/urls.py | 3 +-- backend/auction/views.py | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/auction/urls.py b/backend/auction/urls.py index 7254896..91f7a2f 100644 --- a/backend/auction/urls.py +++ b/backend/auction/urls.py @@ -1,8 +1,7 @@ from django.contrib import admin from django.urls import include, path -from .views import AuctionListApiView, AuctionDetailApiView - +from .views import AuctionDetailApiView, AuctionListApiView urlpatterns = [ path("", AuctionListApiView.as_view(), name="auction_list"), diff --git a/backend/auction/views.py b/backend/auction/views.py index 1b2ad66..1905f79 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -1,9 +1,9 @@ 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 rest_framework.generics import get_object_or_404 from .models import Auction from .serializers import AuctionSerializer @@ -68,6 +68,7 @@ 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) @@ -84,4 +85,4 @@ def put(self, request, auction_id, format=None): 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) \ No newline at end of file + return Response(status=status.HTTP_204_NO_CONTENT)