[WIP] Library REST API
This commit is contained in:
@@ -1,2 +1,5 @@
|
||||
from .blueprint import api_library
|
||||
from .create import create_library
|
||||
from .create import create_library
|
||||
from .read import read_libraries, read_library
|
||||
from .update import update_library
|
||||
from .delete import delete_library
|
||||
23
app/routes/api/library/delete.py
Normal file
23
app/routes/api/library/delete.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from flask import request
|
||||
from sqlalchemy.exc import NoResultFound
|
||||
|
||||
from .blueprint import api_library
|
||||
|
||||
from ....controller import LibraryController
|
||||
from ....schema.library.library import Library
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@api_library.route("/<_id>", methods=["DELETE"])
|
||||
def delete_library(_id):
|
||||
|
||||
try: # TODO: function
|
||||
controller = LibraryController(_id)
|
||||
except NoResultFound as e:
|
||||
logger.debug({e})
|
||||
return { "status": "error", "error": "Library not found" }, 404
|
||||
|
||||
controller.delete()
|
||||
|
||||
return { "status": "ok" }, 201
|
||||
27
app/routes/api/library/read.py
Normal file
27
app/routes/api/library/read.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from flask import request
|
||||
from sqlalchemy.exc import NoResultFound
|
||||
|
||||
from .blueprint import api_library
|
||||
|
||||
from ....controller import LibraryController
|
||||
from ....schema.library.library import Library
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@api_library.route("/", methods=["GET"])
|
||||
def read_libraries():
|
||||
library = LibraryController()
|
||||
library.read_all()
|
||||
return { "status": "ok", "results": [lib.to_dict() for lib in library.libraries] }, 200
|
||||
|
||||
@api_library.route("/<_id>", methods=["GET"])
|
||||
def read_library(_id):
|
||||
try:
|
||||
library = LibraryController(_id)
|
||||
except NoResultFound as e:
|
||||
logger.debug(f"No result found for Library wid id {_id}")
|
||||
logger.debug(f"Error {e}")
|
||||
logger.debug(f"Error {dir(e)}")
|
||||
return { "status": "error", "result": "Library not found"}, 404
|
||||
return { "status": "ok", "result": library.data.to_dict() }, 200
|
||||
50
app/routes/api/library/update.py
Normal file
50
app/routes/api/library/update.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from flask import request
|
||||
from sqlalchemy.exc import IntegrityError, NoResultFound
|
||||
|
||||
from .blueprint import api_library
|
||||
|
||||
from ....controller import LibraryController
|
||||
from ....schema.library.library import Library
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def update_library_item(library:Library, key, value):
|
||||
if key == "id":
|
||||
raise AttributeError("id is not updatable")
|
||||
try:
|
||||
library.__getattribute__(key)
|
||||
except AttributeError:
|
||||
raise AttributeError(f"{key} not in library")
|
||||
library.__setattr__(key, value)
|
||||
|
||||
@api_library.route("/<_id>", methods=["PATCH"])
|
||||
def update_library(_id):
|
||||
try:
|
||||
data:dict = request.json
|
||||
logger.debug(f"data: {data}")
|
||||
except Exception as e:
|
||||
logger.debug(f"{e}")
|
||||
return { "status": "error", "error": "JSON Required" }, 415
|
||||
|
||||
try: # TODO: function
|
||||
controller = LibraryController(_id)
|
||||
except NoResultFound as e:
|
||||
logger.debug({e})
|
||||
return { "status": "error", "error": "Library not found" }, 404
|
||||
|
||||
library = controller.data
|
||||
for key, value in data.items():
|
||||
try:
|
||||
update_library_item(library, key, value)
|
||||
except AttributeError as e:
|
||||
logger.debug(f"Error updating {e}")
|
||||
return { "status": "error", "error": e.name }
|
||||
|
||||
try:
|
||||
controller.update()
|
||||
except IntegrityError as e:
|
||||
logger.debug(f"DB Error Creating {e}")
|
||||
return { "status": "error", "error": f"{e.orig}" }, 400
|
||||
else:
|
||||
return { "status": "ok", "result": controller.data.to_dict() }, 200
|
||||
Reference in New Issue
Block a user