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/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7e68766 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python-envs.pythonProjects": [] +} \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..96377df --- /dev/null +++ b/app.py @@ -0,0 +1,25 @@ +from flask import Flask +from flask_restful import Api +from models import db +from controller import LibraryResource, LibraryListResource + +app = Flask(__name__) +api = Api(app) + +# DB Configuration +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() + +# Library routes +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/controller/__init__.py b/controller/__init__.py new file mode 100644 index 0000000..de230b2 --- /dev/null +++ b/controller/__init__.py @@ -0,0 +1 @@ +from .library_controller import LibraryListResource, LibraryResource diff --git a/controller/__pycache__/__init__.cpython-313.pyc b/controller/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..c42927d 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..c5f352e 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..b62bb04 --- /dev/null +++ b/controller/library_controller.py @@ -0,0 +1,53 @@ +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() + # Validate required fields + 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/instance/test.db b/instance/test.db new file mode 100644 index 0000000..a32808c Binary files /dev/null and b/instance/test.db differ diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 0000000..2b42497 --- /dev/null +++ b/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/models/__pycache__/__init__.cpython-313.pyc b/models/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..d880ec4 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..2ab01e1 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..272d1de --- /dev/null +++ b/models/library.py @@ -0,0 +1,15 @@ +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/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..087df2c 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..f8d0b2f 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..79cb38e --- /dev/null +++ b/repositories/library_repository.py @@ -0,0 +1,37 @@ +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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1805b1e 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..9e16897 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..e9caddd 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..f6bc385 --- /dev/null +++ b/service/library_service.py @@ -0,0 +1,28 @@ +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