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 LibraryResource, LibraryListResource

app = Flask(__name__)
api = Api(app)

#DB confifuration
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, '/library')
api.add_resource(LibraryResource, '/library/<string:isbn>')

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 LibraryController
49 changes: 49 additions & 0 deletions controller/library_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 fields"}, 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 successfully"}, 200
except Exception as e:
db.session.rollback()
return {"error": "Unexpected error occured", "details":str(e)}, 500

5 changes: 5 additions & 0 deletions models/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

from .library import Library
15 changes: 15 additions & 0 deletions models/library.py
Original file line number Diff line number Diff line change
@@ -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(255), nullable=False)
author = db.column(db.String(255), nullable=False)

def to_dict(self):
return{
"isbn": self.isbn,
"title": self.title,
"author": self.author
}
1 change: 1 addition & 0 deletions repositiories/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library_repository import LibraryRepository
38 changes: 38 additions & 0 deletions repositiories/library_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from models import Library, db

class LibraryRepository:

@staticmethod
def find_all(self):
return Library.query.all()

@staticmethod
def find_by_id(self, isbn):
return Library.query.get(isbn=isbn).first()

@staticmethod
def save(self, library):
db.session.add(book)
db.session.commit()
return book

@staticmethod
def update(self, book):
book = Library.query.get(isbn=isbn)
if not book:
return None
for key, value in book:
if hasattr(book, key):
setattr(book, key, value)
db.session.commit()
return book

@staticmethod
def delete(self, isbn):
book = Library.query.get(isbn=isbn)
if not book:
return None

db.session.delete(book)
db.session.commit()
return book
16 changes: 16 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
pytz==2025.2
six==1.17.0
SQLAlchemy==2.0.43
typing_extensions==4.15.0
Werkzeug==3.1.3
31 changes: 31 additions & 0 deletions service/_init_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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(book):
book = Library(
isbn = data['isbn'],
title = data['title'],
author = data['author']
)
return LibraryRepository.save(book)

@staticmethod
def update(isbn, data):
book = LibraryRepository.find_by_id(isbn)
book.isbn = data['isbn']
book.title = data['title']
book.author = data['author']
return LibraryRepository.update(book)

@staticmethod
def delete(isbn):
return LibraryRepository.delete(isbn)
1 change: 1 addition & 0 deletions service/library_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library_service import LibraryService