diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..6900fa0 --- /dev/null +++ b/app.py @@ -0,0 +1,23 @@ +from flask import Flask +from flask_restful import Api +from models import db +from controller import LibraryListResource, LibraryResource + +app = Flask(__name__) +api = Api(app) + +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///library.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +db.init_app(app) + +@app.before_request +def create_tables(): + db.create_all() + + +api.add_resource(LibraryResource, '/api/library/') +api.add_resource(LibraryListResource, '/api/library') + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/controller/__init__.py b/controller/__init__.py new file mode 100644 index 0000000..5c80529 --- /dev/null +++ b/controller/__init__.py @@ -0,0 +1 @@ +from .library_controller import LibraryListResource, LibraryResource \ No newline at end of file diff --git a/controller/__pycache__/__init__.cpython-313.pyc b/controller/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..7d126bb Binary files /dev/null and b/controller/__pycache__/__init__.cpython-313.pyc differ diff --git a/controller/__pycache__/library_controller.cpython-313.pyc b/controller/__pycache__/library_controller.cpython-313.pyc new file mode 100644 index 0000000..dafc11a Binary files /dev/null and b/controller/__pycache__/library_controller.cpython-313.pyc differ diff --git a/controller/library_controller.py b/controller/library_controller.py new file mode 100644 index 0000000..698fab6 --- /dev/null +++ b/controller/library_controller.py @@ -0,0 +1,50 @@ +from flask_restful import Resource +from flask import request +from service import LibraryService +from models import db + +class LibraryListResource(Resource): + def get(self): + books = LibraryService.get_all() + return [book.to_dict() for book in books], 200 + + def post(self): + try: + data = request.get_json() + + if not data or 'isbn' not in data or 'title' not in data or 'author' not in data: + return {'message': 'Invalid data'}, 400 + + book = LibraryService.create(data) + return book.to_dict(), 201 + except Exception as e: + db.session.rollback() + return {"error": "Unexpected error occured", "details":str(e)}, 500 + +class LibraryResource(Resource): + def get(self, isbn): + book = LibraryService.get_by_id(isbn) + if not book: + return {'message': 'Book not found'}, 404 + return book.to_dict(), 200 + + def put(self, isbn): + try: + data = request.get_json() + book = LibraryService.update(isbn, data) + if not book: + return {'message': 'Book not found'}, 404 + return book.to_dict(), 200 + except Exception as e: + db.session.rollback() + return {"error": "Unexpected error occured", "details":str(e)}, 500 + + def delete(self, isbn): + try: + book = LibraryService.delete(isbn) + if not book: + return {'message': 'Book not found'}, 404 + return {"message": f"BOOK with ISBN {isbn} deleted"}, 200 + except Exception as e: + db.session.rollback() + return {"error": "Unexpected error occured", "details":str(e)}, 500 diff --git a/instance/library.db b/instance/library.db new file mode 100644 index 0000000..2a2711c Binary files /dev/null and b/instance/library.db differ diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 0000000..a499d70 --- /dev/null +++ b/models/__init__.py @@ -0,0 +1,3 @@ +from flask_sqlalchemy import SQLAlchemy +db = SQLAlchemy() +from .library import Library \ No newline at end of file diff --git a/models/__pycache__/__init__.cpython-313.pyc b/models/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..ea0f97c Binary files /dev/null and b/models/__pycache__/__init__.cpython-313.pyc differ diff --git a/models/__pycache__/library.cpython-313.pyc b/models/__pycache__/library.cpython-313.pyc new file mode 100644 index 0000000..ef0c5d1 Binary files /dev/null and b/models/__pycache__/library.cpython-313.pyc differ diff --git a/models/library.py b/models/library.py new file mode 100644 index 0000000..0df5330 --- /dev/null +++ b/models/library.py @@ -0,0 +1,14 @@ +from . import db + +class Library(db.Model): + __tablename__ = 'libraries' + isbn = db.Column(db.String(13), primary_key=True) + title = db.Column(db.String(80), nullable=False) + author = db.Column(db.String(80), nullable=False) + + def to_dict(self): + return { + 'isbn': self.isbn, + 'title': self.title, + 'author': self.author + } \ No newline at end of file diff --git a/python-flask-api b/python-flask-api new file mode 160000 index 0000000..49ec626 --- /dev/null +++ b/python-flask-api @@ -0,0 +1 @@ +Subproject commit 49ec626e1ee0797e90e82a59dd50a87d2b72c28c diff --git a/repositories/__init__.py b/repositories/__init__.py new file mode 100644 index 0000000..107ba1e --- /dev/null +++ b/repositories/__init__.py @@ -0,0 +1 @@ +from .library_repository import LibraryRepository diff --git a/repositories/__pycache__/__init__.cpython-313.pyc b/repositories/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..2882303 Binary files /dev/null and b/repositories/__pycache__/__init__.cpython-313.pyc differ diff --git a/repositories/__pycache__/library_repository.cpython-313.pyc b/repositories/__pycache__/library_repository.cpython-313.pyc new file mode 100644 index 0000000..5ce6329 Binary files /dev/null and b/repositories/__pycache__/library_repository.cpython-313.pyc differ diff --git a/repositories/library_repository.py b/repositories/library_repository.py new file mode 100644 index 0000000..7fecdda --- /dev/null +++ b/repositories/library_repository.py @@ -0,0 +1,38 @@ +from models import db, Library + +class LibraryRepository: + @staticmethod + def find_all(): + return Library.query.all() + + @staticmethod + def find_by_id(isbn): + return Library.query.get(isbn) + + @staticmethod + def save(book): + db.session.add(book) + db.session.commit() + return book + @staticmethod + def update(isbn, data: dict): + book = Library.query.get(isbn) + if not book: + return None + + for key, value in data.items(): + if hasattr(book, key): + setattr(book, key, value) + + db.session.commit() + return book + + @staticmethod + def delete(isbn): + book = Library.query.get(isbn) + if not book: + return None + + db.session.delete(book) + db.session.commit() + return book \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..76e3ef5 Binary files /dev/null and b/requirements.txt differ diff --git a/service/__init__.py b/service/__init__.py new file mode 100644 index 0000000..dd29aff --- /dev/null +++ b/service/__init__.py @@ -0,0 +1 @@ +from .library_service import LibraryService diff --git a/service/__pycache__/__init__.cpython-313.pyc b/service/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..36d25a9 Binary files /dev/null and b/service/__pycache__/__init__.cpython-313.pyc differ diff --git a/service/__pycache__/library_service.cpython-313.pyc b/service/__pycache__/library_service.cpython-313.pyc new file mode 100644 index 0000000..f5dc12f Binary files /dev/null and b/service/__pycache__/library_service.cpython-313.pyc differ diff --git a/service/library_service.py b/service/library_service.py new file mode 100644 index 0000000..f765e95 --- /dev/null +++ b/service/library_service.py @@ -0,0 +1,29 @@ +from repositories import LibraryRepository +from models import Library + +class LibraryService: + @staticmethod + def get_all(): + return LibraryRepository.find_all() + + @staticmethod + def get_by_id(isbn): + return LibraryRepository.find_by_id(isbn) + + @staticmethod + def create(data): + book = Library( + isbn=data['isbn'], + title=data['title'], + author=data['author'] + ) + return LibraryRepository.save(book) + + @staticmethod + def update(isbn, data): + return LibraryRepository.update(isbn, data) + + @staticmethod + def delete(isbn): + return LibraryRepository.delete(isbn) + \ No newline at end of file