1
1
Fork 0
novelmetrix-python/ras/api/modules/crud.py

129 lines
4.5 KiB
Python
Raw Permalink Normal View History

2023-11-10 11:57:38 +01:00
import jwt, json
from django.contrib.auth import get_user_model
from rest_framework.decorators import api_view
import ras.settings
from sqlalchemy import create_engine
from sqlalchemy.sql import text
from django.http import JsonResponse
import pandas as pd
from rest_framework.response import Response
2023-11-21 08:24:48 +01:00
from .functions import isAuthorized, getBooksData, filterData
2023-11-10 11:57:38 +01:00
2023-11-13 16:09:59 +01:00
engine = create_engine('mysql+mysqldb://' + ras.settings.DATABASES['default']['USER'] + ':' + ras.settings.DATABASES['default']['PASSWORD'] + '@' + ras.settings.DATABASES['default']['HOST'] + ':3306/' + ras.settings.DATABASES['default']['NAME'])
conn = engine.connect()
2023-11-10 11:57:38 +01:00
2023-11-13 16:09:59 +01:00
# -------------------------------
# Get all books in the database
# -------------------------------
2023-11-10 11:57:38 +01:00
@api_view(['GET'])
def getAllBooks(request):
2023-12-21 15:36:28 +01:00
try:
authorization_token = request.headers.get('Authorization')
isLoggedIn = isAuthorized(authorization_token)
if not authorization_token:
return JsonResponse({'error': 'No authorization token'}, safe=False)
if not isLoggedIn:
return JsonResponse({'error': 'Unauthorized'}, safe=False)
books = getBooksData(request.headers.get('userid'))
data = books.to_dict(orient='records')
return Response(data)
except Exception as e:
return JsonResponse({'error': 'An error occurred: {}'.format(str(e))}, safe=False)
2023-11-13 16:09:59 +01:00
# -------------------------------
# Add a book into the database
# -------------------------------
2023-11-10 11:57:38 +01:00
@api_view(['POST'])
def addBook(request):
2023-12-21 15:36:28 +01:00
try:
authorization_token = request.headers.get('Authorization')
isLoggedIn = isAuthorized(authorization_token)
if not authorization_token:
return JsonResponse({'error': 'No authorization token'}, safe=False)
if not isLoggedIn:
return JsonResponse({'error': 'Unauthorized'}, safe=False)
userid = request.headers.get('userid')
book_data = json.loads(request.body)
query = text("INSERT INTO api_books (userid, name, author, genre, readed, rating) VALUES (:userid, :name, :author, :genre, :readed, :rating)")
conn.execute(query, {
'userid': userid,
'name': book_data['name'],
'author': book_data['author'],
'genre': book_data['genre'],
'readed': book_data['readed'],
'rating': book_data['rating']
})
return JsonResponse("OK", safe=False)
except Exception as e:
return JsonResponse({'error': 'An error occurred: {}'.format(str(e))}, safe=False)
2023-11-13 16:09:59 +01:00
# -------------------------------
# Update a book in the database
# -------------------------------
2023-11-10 11:57:38 +01:00
@api_view(['PUT'])
def updateBook(request):
2023-12-21 15:36:28 +01:00
try:
authorization_token = request.headers.get('Authorization')
isLoggedIn = isAuthorized(authorization_token)
if not authorization_token:
return JsonResponse({'error': 'No authorization token'}, safe=False)
if not isLoggedIn:
return JsonResponse({'error': 'Unauthorized'}, safe=False)
book_data = json.loads(request.POST.get('book'))
bookid = request.headers.get('bookid')
query = text("UPDATE api_books SET name=:name, author=:author, genre=:genre, readed=:readed, rating=:rating WHERE id=:bookid")
conn.execute(query, {
'name': book_data['name'],
'author': book_data['author'],
'genre': book_data['genre'],
'readed': book_data['readed'],
'rating': book_data['rating'],
'bookid': bookid
})
return JsonResponse("OK", safe=False)
except Exception as e:
return JsonResponse({'error': 'An error occurred: {}'.format(str(e))}, safe=False)
2023-11-13 16:09:59 +01:00
# -------------------------------
# Delete a book in the database
# -------------------------------
2023-11-10 11:57:38 +01:00
@api_view(['DELETE'])
def deleteBook(request):
2023-12-21 15:36:28 +01:00
try:
authorization_token = request.headers.get('Authorization')
isLoggedIn = isAuthorized(authorization_token)
if not authorization_token:
return JsonResponse({'error': 'No authorization token'}, safe=False)
if not isLoggedIn:
return JsonResponse({'error': 'Unauthorized'}, safe=False)
bookid = request.headers.get('bookid')
query = text("DELETE FROM api_books WHERE id=:bookid")
conn.execute(query, {
'bookid': bookid
})
return JsonResponse("OK", safe=False)
except Exception as e:
return JsonResponse({'error': 'An error occurred: {}'.format(str(e))}, safe=False)