From a421cacd46b8971d89c12bb9a9a6b83695bfb808 Mon Sep 17 00:00:00 2001 From: AlbertCJC Date: Sat, 20 Sep 2025 13:44:41 +0800 Subject: [PATCH] feat: implemented flask api --- FLASK_LIBRARY/.gitignore | 4 ++ FLASK_LIBRARY/app.py | 27 ++++++++++ FLASK_LIBRARY/controller/__init__.py | 1 + .../controller/library_controller.py | 50 ++++++++++++++++++ FLASK_LIBRARY/models/__init__.py | 5 ++ FLASK_LIBRARY/models/library.py | 17 ++++++ FLASK_LIBRARY/repositories/__init__.py | 1 + .../repositories/library_repository.py | 39 ++++++++++++++ FLASK_LIBRARY/requirements.txt | Bin 0 -> 616 bytes FLASK_LIBRARY/service/__init__.py | 1 + FLASK_LIBRARY/service/library_service.py | 29 ++++++++++ 11 files changed, 174 insertions(+) create mode 100644 FLASK_LIBRARY/.gitignore create mode 100644 FLASK_LIBRARY/app.py create mode 100644 FLASK_LIBRARY/controller/__init__.py create mode 100644 FLASK_LIBRARY/controller/library_controller.py create mode 100644 FLASK_LIBRARY/models/__init__.py create mode 100644 FLASK_LIBRARY/models/library.py create mode 100644 FLASK_LIBRARY/repositories/__init__.py create mode 100644 FLASK_LIBRARY/repositories/library_repository.py create mode 100644 FLASK_LIBRARY/requirements.txt create mode 100644 FLASK_LIBRARY/service/__init__.py create mode 100644 FLASK_LIBRARY/service/library_service.py 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..5d903f4 --- /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:albert123@localhost/studentdb' +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 0000000000000000000000000000000000000000..0dcaafc0545526dfed6cade48454c204a6d5500f GIT binary patch literal 616 zcmZ`$O-sX25S+8%Pq8$wTI<1shk6hTY6XuH<1@CkNyrCOf4n-gYoaLfSQ6gM&d%)n zcu!Gag$5;Nm|~0sr#uP&Mpg;-v4;n|kX2xVi292$!ijN&XNG6Q)ji|=3fS;>*@79 zSx#S(rywt)9T>qJD%*;;4Ib&RKu%80EFEt&Uo4eRzpx#x$qa*zl6cAm?`vl4P%%Zy z6mei_AD;J$E*C6!uNhMK-bY2O#hY~o&J8#kdK=E)8R9+1MBLebyKwJ%i;3;8rFvy4 ka@^D3jHq)$ literal 0 HcmV?d00001 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