Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv
23 changes: 23 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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(LibraryResource, '/api/library/<string:isbn>')
api.add_resource(LibraryListResource, '/api/library')

if __name__ == '__main__':
app.run(debug=True)
1 change: 1 addition & 0 deletions controller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library_controller import LibraryListResource, LibraryResource
Binary file added controller/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file not shown.
50 changes: 50 additions & 0 deletions controller/library_controller.py
Original file line number Diff line number Diff line change
@@ -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 not data or 'isbn' not in data or 'title' not in data or 'author' not in data:
return {'message': 'Invalid data'}, 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
Binary file added instance/library.db
Binary file not shown.
3 changes: 3 additions & 0 deletions models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from .library import Library
Binary file added models/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file added models/__pycache__/library.cpython-313.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions models/library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from . import db

class Library(db.Model):
__tablename__ = 'libraries'
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
}
1 change: 1 addition & 0 deletions python-flask-api
Submodule python-flask-api added at 49ec62
1 change: 1 addition & 0 deletions repositories/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library_repository import LibraryRepository
Binary file added repositories/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file not shown.
38 changes: 38 additions & 0 deletions repositories/library_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from models import db, Library

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
Binary file added requirements.txt
Binary file not shown.
1 change: 1 addition & 0 deletions service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library_service import LibraryService
Binary file added service/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions service/library_service.py
Original file line number Diff line number Diff line change
@@ -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)