added delete route

This commit is contained in:
Guamss 2025-12-16 11:17:41 +01:00
parent 3f95ee9db8
commit 5d34332ba4
2 changed files with 26 additions and 3 deletions

View File

@ -14,14 +14,14 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from api import settings
from books import views
from books.views import UserProfileView, BookListCreateView
from django.conf.urls.static import static
from books.views import UserProfileView, BookListCreateView, BookDetailView
urlpatterns = [
path('admin/', admin.site.urls),
@ -29,8 +29,8 @@ urlpatterns = [
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/profile', UserProfileView.as_view(), name='user_profile'),
path('api/books', BookListCreateView.as_view(), name='book_list_create'),
path('api/books/<int:book_id>/', BookDetailView.as_view(), name='book-detail'),
path('api/users/<int:user_id>', views.find_user_by_id, name='user-detail'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -25,6 +25,27 @@ class BookListCreateView(APIView):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, book_id):
try:
book = Book.objects.get(id=book_id)
book.delete()
return Response(status=status.HTTP_200_OK)
except Book.DoesNotExist:
return Response({"message": "Ce livre n'existe pas"}, status=status.HTTP_404_NOT_FOUND)
class BookDetailView(APIView):
permission_classes = [IsAuthenticatedOrReadOnly]
def delete(self, request, book_id):
try:
book = Book.objects.get(id=book_id)
book.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
except Book.DoesNotExist:
return Response({"message": "Ce livre n'existe pas"}, status=status.HTTP_404_NOT_FOUND)
@api_view(['GET'])
def find_user_by_id(request, user_id):
try:
@ -32,6 +53,7 @@ def find_user_by_id(request, user_id):
books_queryset = Book.objects.filter(user=user)
books_serializer = BookSerializer(books_queryset, many=True)
return Response({
'id': user.id,
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,
@ -51,6 +73,7 @@ class UserProfileView(APIView):
books_queryset = Book.objects.filter(user=user)
books_serializer = BookSerializer(books_queryset, many=True)
return Response({
'id': user.id,
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,