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 vehicle to auction #30

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion backend/auction/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from rest_framework import serializers

from .models import Auction
from .models import Auction, AuctionItem


class AuctionSerializer(serializers.ModelSerializer):
class Meta:
model = Auction
fields = ["id", "name", "start_date", "end_date"]

class AuctionItemSerializer(serializers.ModelSerializer):
class Meta:
model = AuctionItem
fields = ['auction_id', 'content_type', 'object_id']

4 changes: 3 additions & 1 deletion backend/auction/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib import admin
from django.urls import include, path

from .views import AuctionDetailApiView, AuctionListApiView
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("add_to_auction/", AddToAuctionApiView.as_view(), name="add_to_auction"),
kevinrczhang marked this conversation as resolved.
Show resolved Hide resolved

]
30 changes: 27 additions & 3 deletions backend/auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Auction
from .models import Auction, AuctionItem
from .serializers import AuctionSerializer

from vehicle.models import Vehicle

class AuctionListApiView(APIView):
def get(self, request, *args, **kwargs):
Expand Down Expand Up @@ -38,7 +38,7 @@ def post(self, request, *args, **kwargs):
end_date = datetime.strptime(request.data.get("end_date"), date_format)

# Check if start date is in the past
if start_date < datetime.now().date():
if start_date.date() < datetime.now().date():
return Response(
{"error": "Start date should be in the future"},
status=status.HTTP_400_BAD_REQUEST,
Expand Down Expand Up @@ -86,3 +86,27 @@ 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)

class AddToAuctionApiView(APIView):
"""
Takes in a vehicle and auction ID and associates it with an auction by creating an AuctionItem
"""
def post(self, request):
kevinrczhang marked this conversation as resolved.
Show resolved Hide resolved
auction_id = request.data.get("auction_id")
vehicle_id = request.data.get("vehicle_id")

try:
auction = Auction.objects.get(id=auction_id)
vehicle = Vehicle.objects.get(id=vehicle_id)

auction_item = AuctionItem(auction_id=auction, content_object=vehicle)
auction_item.save()

return Response({"message": "Vehicle added to auction successfully"}, status=status.HTTP_201_CREATED)

except Auction.DoesNotExist:
return Response({"error": "Auction not found"}, status=status.HTTP_404_NOT_FOUND)
except Vehicle.DoesNotExist:
return Response({"error": "Vehicle not found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Loading