[WIP] Library REST API
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from .create import create
|
from .create import create
|
||||||
from .read import read
|
from .read import read, read_all
|
||||||
from .update import update
|
from .update import update
|
||||||
from .delete import delete
|
from .delete import delete
|
||||||
|
|||||||
@@ -5,4 +5,8 @@ from ....schema.library.base import Base
|
|||||||
|
|
||||||
def read(session:Session, _id:int, obj:Base):
|
def read(session:Session, _id:int, obj:Base):
|
||||||
stmt = select(obj).where(obj.id == _id)
|
stmt = select(obj).where(obj.id == _id)
|
||||||
return session.scalars(stmt).one()
|
return session.scalars(stmt).one()
|
||||||
|
|
||||||
|
def read_all(session:Session, obj:Base):
|
||||||
|
stmt = select(obj)
|
||||||
|
return session.scalars(stmt).fetchall() #TODO: Pagination
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from app.api.cruds.base import create, read, update, delete
|
from app.api.cruds.base import create, read, update, delete, read_all
|
||||||
from app.schema.library import Library
|
from app.schema.library import Library
|
||||||
from ..db.config.config import get_engine_configuration
|
from ..db.config.config import get_engine_configuration
|
||||||
|
|
||||||
@@ -22,6 +22,9 @@ class LibraryController:
|
|||||||
self._library = None
|
self._library = None
|
||||||
if library_id is not None:
|
if library_id is not None:
|
||||||
self.read(library_id)
|
self.read(library_id)
|
||||||
|
|
||||||
|
self._libraries = []
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
@@ -49,6 +52,19 @@ class LibraryController:
|
|||||||
def session(self):
|
def session(self):
|
||||||
return self._session
|
return self._session
|
||||||
|
|
||||||
|
@property
|
||||||
|
def libraries(self):
|
||||||
|
if self._library and len(self._libraries) == 0:
|
||||||
|
self._libraries = [self._library]
|
||||||
|
return self._libraries
|
||||||
|
|
||||||
|
def set_library(self, _id):
|
||||||
|
libraries = filter(lambda x: x.get("id") == _id, self.libraries)
|
||||||
|
if len(libraries) == 1:
|
||||||
|
self._library = libraries[0]
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def create(self, library:Library):
|
def create(self, library:Library):
|
||||||
self._library = create(self.session, library)
|
self._library = create(self.session, library)
|
||||||
return self
|
return self
|
||||||
@@ -56,6 +72,9 @@ class LibraryController:
|
|||||||
def read(self, _id):
|
def read(self, _id):
|
||||||
self._library = read(self.session, _id, Library)
|
self._library = read(self.session, _id, Library)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def read_all(self):
|
||||||
|
self._libraries = read_all(self.session, Library)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|||||||
@@ -1,2 +1,5 @@
|
|||||||
from .blueprint import api_library
|
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