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

Backend vehicle crud #12

Merged
merged 7 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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)