-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add endpoint for creating auction (#7)
- Loading branch information
Showing
12 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
from django.contrib import admin | ||
|
||
from auction.models import Auction | ||
from bid.models import Bid | ||
from user.models import User | ||
from vehicle.models import Brand, Vehicle, VehicleType | ||
|
||
# Register your models here. | ||
admin.site.register(Auction) | ||
admin.site.register(Vehicle) | ||
admin.site.register(Brand) | ||
admin.site.register(VehicleType) | ||
admin.site.register(Bid) | ||
admin.site.register(User) |
50 changes: 50 additions & 0 deletions
50
backend/auction/migrations/0002_remove_auction_identifier_auction_name_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.2.6 on 2023-10-22 02:26 | ||
|
||
import datetime | ||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("auction", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="auction", | ||
name="identifier", | ||
), | ||
migrations.AddField( | ||
model_name="auction", | ||
name="name", | ||
field=models.CharField(default="Auction", max_length=150), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name="auction", | ||
name="start_date", | ||
field=models.DateTimeField( | ||
default=datetime.datetime( | ||
2023, 10, 22, 2, 26, 16, 722675, tzinfo=datetime.timezone.utc | ||
) | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.AlterField( | ||
model_name="auction", | ||
name="end_date", | ||
field=models.DateTimeField(), | ||
), | ||
migrations.AlterField( | ||
model_name="auction", | ||
name="id", | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
from django.db import models | ||
|
||
from core.models import MainModel | ||
from user.models import User | ||
from vehicle.models import Vehicle | ||
|
||
|
||
class Auction(MainModel): | ||
# start date is given by MainMode's created_at date | ||
end_date = models.DateTimeField(editable=False) | ||
""" | ||
Auction that bidders can place bid on vehicles | ||
""" | ||
|
||
name = models.CharField(max_length=150) | ||
start_date = models.DateTimeField(null=False) | ||
end_date = models.DateTimeField(null=False) | ||
|
||
def __str__(self): | ||
return ( | ||
self.name | ||
if self.name is not None | ||
else "Auction at {start_date} to {end_date}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import Auction | ||
|
||
|
||
class AuctionSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Auction | ||
fields = ["id", "name", "start_date", "end_date"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.contrib import admin | ||
from django.urls import include, path | ||
|
||
from .views import AuctionListApiView | ||
|
||
urlpatterns = [ | ||
path("", AuctionListApiView.as_view(), name="auction"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,64 @@ | ||
from django.shortcuts import render | ||
from datetime import datetime | ||
|
||
from rest_framework import status | ||
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): | ||
""" | ||
Get all auctions | ||
""" | ||
auctions = Auction.objects.all() | ||
serializer = AuctionSerializer(auctions, many=True) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
def post(self, request, *args, **kwargs): | ||
""" | ||
Create the Auction with given auction data | ||
""" | ||
date_format = "%Y-%m-%d" | ||
|
||
# Check if start_date and end_date are provided | ||
if ( | ||
request.data.get("start_date") is None | ||
or request.data.get("end_date") is None | ||
): | ||
return Response( | ||
{"error": "Start date and end date are required"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
start_date = datetime.strptime(request.data.get("start_date"), date_format) | ||
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(): | ||
return Response( | ||
{"error": "Start date should be in the future"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
# Check if end date is before start date | ||
if end_date < start_date: | ||
return Response( | ||
{"error": "End date should be after start date"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
data = { | ||
"name": request.data.get("name"), | ||
"start_date": start_date, | ||
"end_date": end_date, | ||
} | ||
serializer = AuctionSerializer(data=data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
28 changes: 28 additions & 0 deletions
28
backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 4.2.6 on 2023-10-22 02:26 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("bid", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="bid", | ||
name="identifier", | ||
), | ||
migrations.AlterField( | ||
model_name="bid", | ||
name="id", | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...end/vehicle/migrations/0002_remove_brand_identifier_remove_vehicle_identifier_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Generated by Django 4.2.6 on 2023-10-22 02:26 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("vehicle", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="brand", | ||
name="identifier", | ||
), | ||
migrations.RemoveField( | ||
model_name="vehicle", | ||
name="identifier", | ||
), | ||
migrations.RemoveField( | ||
model_name="vehicletype", | ||
name="identifier", | ||
), | ||
migrations.AlterField( | ||
model_name="brand", | ||
name="id", | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="vehicle", | ||
name="id", | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="vehicletype", | ||
name="id", | ||
field=models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
unique=True, | ||
), | ||
), | ||
] |