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 save-units endpoint #31

Merged
merged 14 commits into from
Feb 6, 2024
18 changes: 17 additions & 1 deletion backend/auction/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
from django.contrib import admin
from django.urls import include, path
from .views import (
AuctionDetailApiView,
AuctionListApiView,
SaveUnitApiView,
GetSavedUnitApiView,
AddToAuctionApiView,
)

from .views import AuctionDetailApiView, AuctionListApiView, AddToAuctionApiView

urlpatterns = [
path("", AuctionListApiView.as_view(), name="auction_list"),
path("<uuid:auction_id>/", AuctionDetailApiView.as_view(), name="auction_detail"),
path(
"<uuid:auction_id>/bidders/<uuid:bidder_id>/vehicles/<uuid:vehicle_id>/",
SaveUnitApiView.as_view(),
name="auction_detail",
),
path(
"<uuid:auction_id>/bidders/<uuid:bidder_id>/vehicles/",
GetSavedUnitApiView.as_view(),
name="auction_detail",
),
path(
"<uuid:auction_id>/vehicles/<uuid:vehicle_id>",
AddToAuctionApiView.as_view(),
Expand Down
82 changes: 81 additions & 1 deletion backend/auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from .models import Auction, AuctionItem
from .serializers import AuctionSerializer
from vehicle.models import Vehicle
from vehicle.models import Vehicle, SavedUnits, ContentType
from user.models import Bidder


class AuctionListApiView(APIView):
Expand Down Expand Up @@ -89,6 +90,85 @@ def delete(self, request, auction_id, format=None):
return Response(status=status.HTTP_204_NO_CONTENT)


class SaveUnitApiView(APIView):
"""
An endpoint to handle saving a vehicle to a bidder's saved vehicle list,
as well as retrieving a list of all saved vehicles
"""

def post(self, request, **kwargs):
bidder_id = kwargs.get("bidder_id")
vehicle_id = kwargs.get("vehicle_id")
auction_id = kwargs.get("auction_id")

# Replace this later to fetch authentication details
# from headers instead of body
vehicle = get_object_or_404(Vehicle, id=vehicle_id)
auction_for_vehicle = Auction.objects.get(id=auction_id)
bidder = get_object_or_404(Bidder, id=bidder_id)
if SavedUnits.objects.filter(
auction_id=auction_for_vehicle, bidder_id=bidder, object_id=vehicle.id
):
return Response(
{"message": "Vehicle already saved"},
status=status.HTTP_204_NO_CONTENT,
)

saved_unit = SavedUnits(
auction_id=auction_for_vehicle,
bidder_id=bidder,
object_id=vehicle.id,
content_object=vehicle,
)
saved_unit.save()
return Response(
{"message": "Vehicle saved successfully"},
status=status.HTTP_200_OK,
)

def delete(self, request, **kwargs):
vehicle_id = kwargs.get("vehicle_id")
bidder_id = kwargs.get("bidder_id")
auction = kwargs.get("auction_id")
bidder = get_object_or_404(Bidder, id=bidder_id)

saved_unit = get_object_or_404(
SavedUnits, object_id=vehicle_id, bidder_id=bidder, auction_id=auction
)

saved_unit.delete()

return Response(
{"message": "Saved unit deleted successfully"},
status=status.HTTP_200_OK,
)


class GetSavedUnitApiView(APIView):
"""
An endpoint to retrieve all of bidder's saved units associated with
a provided auction
"""

def get(self, request, **kwargs):
# Replace this line to obtain user id once authentication has been implemented
bidder_id = kwargs.get("bidder_id")
auction_id = kwargs.get("auction_id")
bidder = get_object_or_404(Bidder, id=bidder_id)
auction = get_object_or_404(Auction, id=auction_id)

saved_units = SavedUnits.objects.filter(bidder_id=bidder, auction_id=auction)
vehicle_list = [
saved_unit.content_object
for saved_unit in saved_units
if isinstance(saved_unit.content_object, Vehicle)
]

vehicle_data = [{"id": vehicle.id} for vehicle in vehicle_list]

return Response({"vehicles": vehicle_data}, status=status.HTTP_200_OK)


class AddToAuctionApiView(APIView):
"""
Takes in a vehicle and auction ID and associates
Expand Down
13 changes: 10 additions & 3 deletions backend/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
from django.urls import include, path

from .views import (
AdminDetailApiView, AdminListApiView, BidderBlacklistApiView,
BidderDetailApiView, BidderListApiView, BidderVerifyApiView,
ListBlacklisted, ListUnverified, ListVerified)
AdminDetailApiView,
AdminListApiView,
BidderBlacklistApiView,
BidderDetailApiView,
BidderListApiView,
BidderVerifyApiView,
ListBlacklisted,
ListUnverified,
ListVerified,
)

urlpatterns = [
path("bidders/", BidderListApiView.as_view(), name="bidder-list"),
Expand Down
9 changes: 6 additions & 3 deletions backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from django.contrib.contenttypes.models import ContentType
from .models import Admin, Bidder
from .serializers import (
AdminSerializer, BidderBlacklistedSerializer, BidderSerializer,
BidderVerifiedSerializer)
AdminSerializer,
BidderBlacklistedSerializer,
BidderSerializer,
BidderVerifiedSerializer,
)


class BidderListApiView(APIView):
Expand Down
10 changes: 9 additions & 1 deletion backend/vehicle/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
from django.contrib.contenttypes.models import ContentType

from .models import (
Brand, Equipment, SavedUnits, Supplier, Trailer, Type, UnitImage, Vehicle)
Brand,
Equipment,
SavedUnits,
Supplier,
Trailer,
Type,
UnitImage,
Vehicle,
)


class BrandAdmin(admin.ModelAdmin):
Expand Down
3 changes: 1 addition & 2 deletions backend/vehicle/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .models import (
Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle)
from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle


def infinite_filter(request):
Expand Down
3 changes: 1 addition & 2 deletions backend/vehicle/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from rest_framework import serializers

from .models import (
Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle)
from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle


class BrandSerializer(serializers.ModelSerializer):
Expand Down
7 changes: 5 additions & 2 deletions backend/vehicle/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from django.urls import include, path

from .views import (
VehicleDetailApiView, VehicleFilterList, VehicleListApiView,
VehiclePriceApiView)
VehicleDetailApiView,
VehicleFilterList,
VehicleListApiView,
VehiclePriceApiView,
)

urlpatterns = [
path("", VehicleListApiView.as_view(), name="vehicle"),
Expand Down
14 changes: 10 additions & 4 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
from rest_framework.views import APIView

from .helpers import has_more_data, infinite_filter
from .models import (
Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle)
from user.models import Bidder
from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle
from .serializers import (
BrandSerializer, EquipmentSerializer, SupplierSerializer,
TrailerSerializer, TypeSerializer, UnitImageSerializer, VehicleSerializer)
BrandSerializer,
EquipmentSerializer,
SupplierSerializer,
TrailerSerializer,
TypeSerializer,
UnitImageSerializer,
VehicleSerializer,
)


# Create your views here.
Expand Down
Loading