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..5e9dcc5 --- /dev/null +++ b/app.py @@ -0,0 +1,22 @@ +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, '/libraries') +api.add_resource(LibraryResource, '/libraries/') + +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..cc9f346 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..5b06b59 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..a8695c7 --- /dev/null +++ b/controller/library_controller.py @@ -0,0 +1,47 @@ +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 {"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": "Book deleted"}, 200 + except Exception as e: + 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..8748686 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..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..787f371 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..0c61c86 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..03c03f7 --- /dev/null +++ b/models/library.py @@ -0,0 +1,15 @@ +from . import db + +class Library(db.Model): + __tablename__ = 'libraries' + + isbn = db.Column(db.String, primary_key=True) + title = db.Column(db.String, nullable=False) + author = db.Column(db.String, 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..ed39183 --- /dev/null +++ b/repositories/__init__.py @@ -0,0 +1 @@ +from .library_repositories import LibraryRepository diff --git a/repositories/__pycache__/__init__.cpython-313.pyc b/repositories/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..46f587b Binary files /dev/null and b/repositories/__pycache__/__init__.cpython-313.pyc differ diff --git a/repositories/__pycache__/library_repositories.cpython-313.pyc b/repositories/__pycache__/library_repositories.cpython-313.pyc new file mode 100644 index 0000000..befe856 Binary files /dev/null and b/repositories/__pycache__/library_repositories.cpython-313.pyc differ diff --git a/repositories/library_repositories.py b/repositories/library_repositories.py new file mode 100644 index 0000000..22842d1 --- /dev/null +++ b/repositories/library_repositories.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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d494ed6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +aniso8601==10.0.1 +blinker==1.9.0 +click==8.3.0 +Flask==3.1.2 +Flask-RESTful==0.3.10 +Flask-SQLAlchemy==3.1.1 +greenlet==3.2.4 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.2 +pytz==2025.2 +six==1.17.0 +SQLAlchemy==2.0.43 +typing_extensions==4.15.0 +Werkzeug==3.1.3 diff --git a/service/__init__.py b/service/__init__.py new file mode 100644 index 0000000..f6bf3ff --- /dev/null +++ b/service/__init__.py @@ -0,0 +1 @@ +from .library_service import LibraryService \ No newline at end of file diff --git a/service/__pycache__/__init__.cpython-313.pyc b/service/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..f26b5a9 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..1f57f28 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..9914d53 --- /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