diff --git a/flask_library/.gitignore b/flask_library/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/flask_library/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/flask_library/app.py b/flask_library/app.py new file mode 100644 index 0000000..c8059ec --- /dev/null +++ b/flask_library/app.py @@ -0,0 +1,27 @@ +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(LibraryListResource, '/api/library') +api.add_resource(LibraryResource, '/api/library/') + +# Create all database tables at startup + + +# Run the app +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/flask_library/controller/__init__.py b/flask_library/controller/__init__.py new file mode 100644 index 0000000..de230b2 --- /dev/null +++ b/flask_library/controller/__init__.py @@ -0,0 +1 @@ +from .library_controller import LibraryListResource, LibraryResource diff --git a/flask_library/controller/__pycache__/__init__.cpython-313.pyc b/flask_library/controller/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..48eced6 Binary files /dev/null and b/flask_library/controller/__pycache__/__init__.cpython-313.pyc differ diff --git a/flask_library/controller/__pycache__/library_controller.cpython-313.pyc b/flask_library/controller/__pycache__/library_controller.cpython-313.pyc new file mode 100644 index 0000000..49b8e19 Binary files /dev/null and b/flask_library/controller/__pycache__/library_controller.cpython-313.pyc differ diff --git a/flask_library/controller/library_controller.py b/flask_library/controller/library_controller.py new file mode 100644 index 0000000..1b1b46a --- /dev/null +++ b/flask_library/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, Library + + +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 'isbn' not in data or 'title' not in data or 'author' not in data: + return {"message": "ISBN, title, and author are required"}, 400 + + book = LibraryService.create(data) + return book.to_dict(), 201 + except Exception as e: + db.session.rollback() + return {"error": "Unexpected error occurred", "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 occurred", "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 occurred", "details": str(e)}, 500 \ No newline at end of file diff --git a/flask_library/instance/library.db b/flask_library/instance/library.db new file mode 100644 index 0000000..c3d5e1d Binary files /dev/null and b/flask_library/instance/library.db differ diff --git a/flask_library/models/__init__.py b/flask_library/models/__init__.py new file mode 100644 index 0000000..be18e9c --- /dev/null +++ b/flask_library/models/__init__.py @@ -0,0 +1,5 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +from .library import Library \ No newline at end of file diff --git a/flask_library/models/__pycache__/__init__.cpython-313.pyc b/flask_library/models/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..5baca92 Binary files /dev/null and b/flask_library/models/__pycache__/__init__.cpython-313.pyc differ diff --git a/flask_library/models/__pycache__/library.cpython-313.pyc b/flask_library/models/__pycache__/library.cpython-313.pyc new file mode 100644 index 0000000..076081d Binary files /dev/null and b/flask_library/models/__pycache__/library.cpython-313.pyc differ diff --git a/flask_library/models/library.py b/flask_library/models/library.py new file mode 100644 index 0000000..195148d --- /dev/null +++ b/flask_library/models/library.py @@ -0,0 +1,15 @@ +from . import db # now db is already defined in __init__.py + +class Library(db.Model): + __tablename__ = 'library' + + 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 + } diff --git a/flask_library/repositories/__init__.py b/flask_library/repositories/__init__.py new file mode 100644 index 0000000..9e068cd --- /dev/null +++ b/flask_library/repositories/__init__.py @@ -0,0 +1 @@ +from .library_repository import LibraryRepository \ No newline at end of file diff --git a/flask_library/repositories/library_repository.py b/flask_library/repositories/library_repository.py new file mode 100644 index 0000000..22842d1 --- /dev/null +++ b/flask_library/repositories/library_repository.py @@ -0,0 +1,39 @@ +from models import Library, db + +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/flask_library/requirements.txt b/flask_library/requirements.txt new file mode 100644 index 0000000..76e3ef5 Binary files /dev/null and b/flask_library/requirements.txt differ diff --git a/flask_library/service/__init__.py b/flask_library/service/__init__.py new file mode 100644 index 0000000..f6bf3ff --- /dev/null +++ b/flask_library/service/__init__.py @@ -0,0 +1 @@ +from .library_service import LibraryService \ No newline at end of file diff --git a/flask_library/service/__pycache__/__init__.cpython-313.pyc b/flask_library/service/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..925c5a3 Binary files /dev/null and b/flask_library/service/__pycache__/__init__.cpython-313.pyc differ diff --git a/flask_library/service/__pycache__/library_service.cpython-313.pyc b/flask_library/service/__pycache__/library_service.cpython-313.pyc new file mode 100644 index 0000000..5c0f315 Binary files /dev/null and b/flask_library/service/__pycache__/library_service.cpython-313.pyc differ diff --git a/flask_library/service/library_service.py b/flask_library/service/library_service.py new file mode 100644 index 0000000..bb48cec --- /dev/null +++ b/flask_library/service/library_service.py @@ -0,0 +1,58 @@ +from models import Library, db + +class LibraryService: + @staticmethod + def get_all(): + return Library.query.all() + + @staticmethod + def find_all(): + return Library.query.all() + + @staticmethod + def find_by_id(isbn): + return Library.query.get(isbn) + + @staticmethod + def get_by_id(isbn): + return Library.query.get(isbn) + + @staticmethod + def create(data: dict): + book = Library( + isbn=data.get('isbn'), + title=data.get('title'), + author=data.get('author') + ) + db.session.add(book) + db.session.commit() + return book + + @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