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: 18 additions & 0 deletions backend/user/migrations/0004_bidder_saved_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.6 on 2024-01-23 01:07

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("vehicle", "0003_savedunits"),
("user", "0003_remove_admin_password"),
]

operations = [
migrations.AddField(
model_name="bidder",
name="saved_list",
field=models.ManyToManyField(to="vehicle.vehicle"),
),
]
2 changes: 2 additions & 0 deletions backend/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import models

from core.models import MainModel
from vehicle.models import Vehicle
kevinrczhang marked this conversation as resolved.
Show resolved Hide resolved


# Create your models here.
Expand All @@ -24,6 +25,7 @@ class Bidder(MainModel):
bidder_number = models.IntegerField(unique=True, blank=True, null=False)
is_verified = models.BooleanField(default=False)
is_blacklisted = models.BooleanField(default=False)
saved_list = models.ManyToManyField(Vehicle)
kevinrczhang marked this conversation as resolved.
Show resolved Hide resolved

def save(self, *args, **kwargs):
# Check if the bidder_number is not set yet
Expand Down
8 changes: 7 additions & 1 deletion backend/vehicle/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from django.contrib import admin
from django.urls import include, path

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

urlpatterns = [
path("", VehicleListApiView.as_view(), name="vehicle"),
path("filter/", VehicleFilterList.as_view(), name="vehicle-list"),
path("save/", SaveUnitApiView.as_view(), name="save-unit-to-list"),
kevinrczhang marked this conversation as resolved.
Show resolved Hide resolved
path("<uuid:vehicle_id>/", VehicleDetailApiView.as_view(), name="vehicle_detail"),
]
48 changes: 42 additions & 6 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .helpers import has_more_data, infinite_filter
from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle
from user.models import Bidder
from .serializers import (
BrandSerializer,
EquipmentSerializer,
Expand Down Expand Up @@ -41,8 +42,7 @@ def post(self, request, *args, **kwargs):
brand = get_object_or_404(Brand, id=brand_id) if brand_id else None
vehicle_type = get_object_or_404(Type, id=type_id) if type_id else None

vehicle = Vehicle.objects.create(
brand=brand, vehicle_type=vehicle_type, **data)
vehicle = Vehicle.objects.create(brand=brand, vehicle_type=vehicle_type, **data)
# Use the serializer class's data directly
serialized_data = self.serializer_class(vehicle)
return Response(serialized_data.data, status=status.HTTP_201_CREATED)
Expand Down Expand Up @@ -71,8 +71,7 @@ def put(self, request, vehicle_id, format=None):

"""
vehicle = get_object_or_404(Vehicle, id=vehicle_id)
serializer = VehicleSerializer(
vehicle, data=request.data)
serializer = VehicleSerializer(vehicle, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
Expand Down Expand Up @@ -105,8 +104,45 @@ def get(self, request):
serialized_data = VehicleSerializer(vehicles, many=True)

return Response(
{"vehicles": serialized_data.data,
"more_data": has_more_data(request)}
{"vehicles": serialized_data.data, "more_data": has_more_data(request)}
)

return Response(VehicleSerializer(Vehicle.objects.all()[:10], many=True).data)


class SaveUnitApiView(APIView):
"""
An endpoint to handle saving a vehicle to a bidder's saved vehicle list
"""

def get(self, request):
# Replace this line to obtain user id once authentication has been implemented
bidder_id = request.data.get("bidder_id")
try:
bidder = Bidder.objects.get(id=bidder_id)
vehicles = bidder.saved_list.all()
serialized_data = VehicleSerializer(vehicles, many=True)
return Response(serialized_data.data)
except Exception:
return Response("Bidder not found")

def post(self, request):
vehicle_id = request.data.get("vehicle_id")

# Replace this later to fetch authentication details
# from headers instead of body
bidder_id = request.data.get("bidder_id")
delete = request.GET.get("delete")

try:
vehicle = Vehicle.objects.get(id=vehicle_id)
bidder = Bidder.objects.get(id=bidder_id)

if delete:
bidder.saved_list.remove(vehicle)
return Response("Unit removed from saved list")
else:
bidder.saved_list.add(vehicle)
return Response("Unit added successfully")
except Exception:
return Response("Bidder or vehicle not found")