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

update vehicle #28

Merged
merged 1 commit into from
Jan 19, 2024
Merged
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
19 changes: 17 additions & 2 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ 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 All @@ -64,6 +65,19 @@ def get(self, request, vehicle_id, *args, **kwargs):
except vehicle.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

def put(self, request, vehicle_id, format=None):
"""
Update specific vehicle

"""
vehicle = get_object_or_404(Vehicle, id=vehicle_id)
serializer = VehicleSerializer(
vehicle, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, vehicle_id, format=None):
"""
Delete specific vehicle
Expand Down Expand Up @@ -91,7 +105,8 @@ 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)