diff --git a/flask-library/.gitignore b/flask-library/.gitignore new file mode 100644 index 0000000..821313b --- /dev/null +++ b/flask-library/.gitignore @@ -0,0 +1,4 @@ +.venv + +__pycache__/ +*.pyc diff --git a/flask-library/app.py b/flask-library/app.py new file mode 100644 index 0000000..6302510 --- /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 LibraryResource, LibraryListResource + +#using mysql instead of sql lite +import pymysql + +pymysql.install_as_MySQLdb() + +app = Flask(__name__) +api = Api(app) + +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:C0nf!d3nt@localhost/flask_library' +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/library_controller.py b/flask-library/controller/library_controller.py new file mode 100644 index 0000000..ef9c073 --- /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 {"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/flask-library/models/__init__.py b/flask-library/models/__init__.py new file mode 100644 index 0000000..9b0443f --- /dev/null +++ b/flask-library/models/__init__.py @@ -0,0 +1,5 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +from .library import Library diff --git a/flask-library/models/library.py b/flask-library/models/library.py new file mode 100644 index 0000000..6dc824f --- /dev/null +++ b/flask-library/models/library.py @@ -0,0 +1,17 @@ +from . import db + +class Library(db.Model): + + # kung library ang tablename sa mysql mag error + __tablename__ = 'books_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/library_repository.py b/flask-library/repositories/library_repository.py new file mode 100644 index 0000000..c40e5b6 --- /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..cc01b87 --- /dev/null +++ b/flask-library/requirements.txt @@ -0,0 +1,17 @@ +aniso8601==10.0.1 +blinker==1.9.0 +click==8.3.0 +colorama==0.4.6 +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 +PyMySQL==1.1.2 +pytz==2025.2 +six==1.17.0 +SQLAlchemy==2.0.43 +typing_extensions==4.15.0 +Werkzeug==3.1.3 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/library_service.py b/flask-library/service/library_service.py new file mode 100644 index 0000000..eb6021d --- /dev/null +++ b/flask-library/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