Skip to content

Commit

Permalink
Merge pull request #76 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 c10955c + c54f8e4 commit 3c0c468
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions pinjam_buku/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
from django.contrib.auth import get_user
from wishlist.models import Wishlist
from django.http import JsonResponse, HttpResponseBadRequest

# Create your views here.

Expand Down Expand Up @@ -65,9 +66,12 @@ def borrow_book_flutter(request, book_id:int):
if request.method == 'POST':

data = json.loads(request.body)
print("data " + data)
bookk = Book.objects.get(pk=book_id)
return_date_str = data["return_date"]
print("return_date_str ", return_date_str)
return_date = datetime.strptime(return_date_str, "%Y-%m-%d")
print("return_date ", return_date)

bookk.status = "Borrowed"
bookk.return_date = return_date
Expand All @@ -89,25 +93,32 @@ def borrow_book_flutter(request, book_id:int):
@csrf_exempt
def borrow_book_flutteer(request, book_id:int):
if request.method == 'POST':

data = json.loads(request.body)
bookk = Book.objects.get(pk=book_id)
return_date_str = data["return_date"]
return_date = datetime.strptime(return_date_str, "%Y-%m-%d %H:%M:%S.%f")

bookk.status = "Borrowed"
bookk.return_date = return_date
bookk.save()

new_borrowed_book = BorrowedBook(
user = request.user,
book = bookk,
return_date = return_date,
)

new_borrowed_book.save()

return JsonResponse({"status": "success"}, status=200)
try:
data = json.loads(request.body)
print("data " + data)
bookk = Book.objects.get(pk=book_id)
return_date_str = data["return_date"]
print("return_date_str ", return_date_str)
return_date = datetime.strptime(return_date_str, "%Y-%m-%d %H:%M:%S.%f")
print("return_date ", return_date)

bookk.status = "Borrowed"
bookk.return_date = return_date
bookk.save()

new_borrowed_book = BorrowedBook(
user = request.user,
book = bookk,
return_date = return_date,
)

new_borrowed_book.save()

return JsonResponse({"status": "success"}, status=200)
except ValueError:
return HttpResponseBadRequest("Invalid date format")
except Exception as e:
return JsonResponse({"status": "error", "message": str(e)}, status=500)
else:
return JsonResponse({"status": "error"}, status=401)

Expand All @@ -120,6 +131,8 @@ def borrow_book_flutteeer(request, book_id:int):
bookk = Book.objects.get(pk=book_id)
return_date = datetime.datetime.now()

print("date time now:", return_date)

bookk.status = "Borrowed"
bookk.return_date = return_date
bookk.save()
Expand Down

0 comments on commit 3c0c468

Please sign in to comment.