Skip to content

Commit

Permalink
Merge pull request #70 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 9309aed + ff09c56 commit a4e7fa6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions pinjam_buku/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
path('return-borrowed-book-flutter/<int:book_id>/', return_borrowed_book_flutter, name='return_borrowed_book_flutter'),
path('borrow-flutter/<int:book_id>/', borrow_flutter, name='borrow_flutter'),
path('add-wishlist-flutterr/<int:book_id>/', add_wishlist_flutterr, name='add_wishlist_flutterr'),
path('borroww_book_flutter/', borroww_book_flutter, name='borroww_book_flutter'),
]
31 changes: 24 additions & 7 deletions pinjam_buku/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def borrow_flutter(request, book_id):
# Ambil buku yang sesuai dengan book_id
book = Book.objects.get(pk=book_id)
user = request.user
return_date = datetime.now()
return_date = datetime.datetime.now()

borrowed = BorrowedBook(user=user, book=book, return_date = return_date)
borrowed.save()
Expand All @@ -105,16 +105,33 @@ def add_wishlist_flutterr(request, book_id):
# Ambil buku yang sesuai dengan book_id
book = Book.objects.get(pk=book_id)
user = request.user
# Cek apakah buku sudah ada di wishlist
if Wishlist.objects.filter(user=user, book=book).exists():
# Kirim status bahwa data sudah ada
return JsonResponse({"status": "existed"}, status=200)
wishlist = Wishlist(user=user, book=book)
wishlist.save()
return_date = datetime.now()

borrowed = BorrowedBook(user=user, book=book, return_date = return_date)
borrowed.save()
return JsonResponse({"status": "success"}, status=201)
else:
return JsonResponse({"status": "error"}, status=401)

@csrf_exempt
def borroww_book_flutter(request):
if request.method == 'POST':
data = json.loads(request.body)
book_title = data["book_title"]
book = get_object_or_404(Book, book_title=book_title)
return_date_str = data["return_date"]
return_date = datetime.strptime(return_date_str, "%Y-%m-%d %H:%M:%S.%f")
borrowed_book = BorrowedBook.objects.create(
user = request.user,
book = book,
return_date = return_date,
)
borrowed_book.save()

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

@csrf_exempt
def return_book_ajax(request, id):
borrowed_book = BorrowedBook.objects.filter(user=request.user)
Expand Down

0 comments on commit a4e7fa6

Please sign in to comment.