Skip to content

Commit

Permalink
Merge pull request #64 from ReadNow-C14/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hrjuno authored Dec 20, 2023
2 parents 72350fc + 6d20fb7 commit bd755a4
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions pinjam_buku/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .models import Book
from django.http import JsonResponse
import json
from django.contrib.auth import get_user

# Create your views here.

Expand Down Expand Up @@ -56,16 +57,6 @@ def borrow_book_ajax(request, id):

return HttpResponseNotFound()

@csrf_exempt
def return_book_ajax(request, id):
borrowed_book = BorrowedBook.objects.filter(user=request.user)
for book in borrowed_book:
if book.book.id == id:
book.book.status = "Available"
book.book.save()
book.delete()
return HttpResponse(b"RETURNED", status=201)

@csrf_exempt
def borrow_book_flutter(request):
if request.method == 'POST':
Expand All @@ -89,4 +80,27 @@ def borrow_book_flutter(request):

return JsonResponse({"status": "success"}, status=200)
else:
return JsonResponse({"status": "error"}, status=401)
return JsonResponse({"status": "error"}, status=401)

@csrf_exempt
def return_book_ajax(request, id):
borrowed_book = BorrowedBook.objects.filter(user=request.user)
for book in borrowed_book:
if book.book.id == id:
book.book.status = "Available"
book.book.save()
book.delete()
return HttpResponse(b"RETURNED", status=201)

@login_required(login_url='/login')
@csrf_exempt
def return_borrowed_book_flutter(request, book_id):
book = Book.objects.get(pk=book_id)
user = get_user(request)
if BorrowedBook.objects.filter(user=user, book=book).exists():
# Kirim status bahwa data sudah ada
borrowed = BorrowedBook.objects.get(user=user, book=book)
borrowed.delete()
return JsonResponse({"status": "success"}, status=200)
else:
return JsonResponse({"status": "not found"}, status=404)

0 comments on commit bd755a4

Please sign in to comment.