Skip to content

Commit

Permalink
Backend vehicle crud (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrczhang authored Nov 8, 2023
1 parent bf54019 commit 698be50
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
urlpatterns = [
path("admin", admin.site.urls),
path("api/v1/auction", include("auction.urls")),
path("api/v1/vehicle", include("vehicle.urls")),
]
9 changes: 9 additions & 0 deletions backend/vehicle/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from .models import Vehicle


class VehicleSerializer(serializers.ModelSerializer):
class Meta:
model = Vehicle
fields = '__all__'
8 changes: 8 additions & 0 deletions backend/vehicle/urls.py
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 vehicle import views

urlpatterns = [
path("", views.VehicleAPIView.as_view(), name="vehicle"),
]
35 changes: 33 additions & 2 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
from django.shortcuts import render
from rest_framework import status, generics
from rest_framework.response import Response
from rest_framework.views import APIView

# Create your views here.
from .models import Vehicle, Brand, Type
from .serializers import VehicleSerializer


class VehicleAPIView(APIView):
serializer_class = VehicleSerializer

def get(self, request, *args, **kwargs):
"""
Get specific vehicle
"""
identifier = request.data.get("id")
vehicle = Vehicle.objects.get(id=identifier)
serialized_data = self.serializer_class(vehicle)
try:
return Response(serialized_data.data, status=status.HTTP_200_OK)
except vehicle.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

def post(self, request, *args, **kwargs):
data = request.data
brand_id = data.get("brand")
type_id = data.get("type")
data.pop("brand")
data.pop("type")
brand = Brand.objects.get(id=brand_id)
type = Type.objects.get(id=type_id)
vehicle = Vehicle.objects.create(**data, brand=brand, vehicle_type=type)
serialized_data = self.serializer_class(vehicle)
return Response(serialized_data.data, status=status.HTTP_200_OK)

0 comments on commit 698be50

Please sign in to comment.