diff --git a/flask_library/.gitignore b/flask_library/.gitignore new file mode 100644 index 0000000..2837996 Binary files /dev/null and b/flask_library/.gitignore differ diff --git a/flask_library/app.py b/flask_library/app.py new file mode 100644 index 0000000..cbc7ac5 --- /dev/null +++ b/flask_library/app.py @@ -0,0 +1,22 @@ +from flask import Flask +from flask_restful import Api +from models import db +from controller import LibraryResource, LibraryListResource + +app = Flask(__name__) +api = Api(app) + +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.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/') + +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..5c80529 --- /dev/null +++ b/flask_library/controller/__init__.py @@ -0,0 +1 @@ +from .library_controller import LibraryListResource, LibraryResource \ No newline at end of file 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..f2a5c6e 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..7a524b6 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..3524342 --- /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 + +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 {"message": "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 {"message": "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 {"message": "Unexpected error occurred", "details": str(e)}, 500 \ No newline at end of file diff --git a/flask_library/instance/test.db b/flask_library/instance/test.db new file mode 100644 index 0000000..55e73bb Binary files /dev/null and b/flask_library/instance/test.db differ diff --git a/flask_library/models/__init__.py b/flask_library/models/__init__.py new file mode 100644 index 0000000..2b42497 --- /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..899aedd 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..cfe8590 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..b153110 --- /dev/null +++ b/flask_library/models/library.py @@ -0,0 +1,14 @@ +from . import db +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 + } \ No newline at end of file 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/__pycache__/__init__.cpython-313.pyc b/flask_library/repositories/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..e6d8c60 Binary files /dev/null and b/flask_library/repositories/__pycache__/__init__.cpython-313.pyc differ diff --git a/flask_library/repositories/__pycache__/library_repository.cpython-313.pyc b/flask_library/repositories/__pycache__/library_repository.cpython-313.pyc new file mode 100644 index 0000000..d7524c4 Binary files /dev/null and b/flask_library/repositories/__pycache__/library_repository.cpython-313.pyc differ diff --git a/flask_library/repositories/library_repository.py b/flask_library/repositories/library_repository.py new file mode 100644 index 0000000..aa695fc --- /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..0f0c29d 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..2b22bba 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..6a1df50 --- /dev/null +++ b/flask_library/service/library_service.py @@ -0,0 +1,27 @@ +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) + + def delete(isbn): + return LibraryRepository.delete(isbn) \ No newline at end of file