# cli/main.py
import click
from uuid import UUID
from tabulate import tabulate
from config.database import CassandraConnection
from models.book import BookRepository, Book
from models.user import UserRepository
from models.borrow import BorrowRepository
# Connexion globale
db = CassandraConnection()
session = db.connect()
book_repo = BookRepository(session)
user_repo = UserRepository(session)
borrow_repo = BorrowRepository(session)
@click.group()
def cli():
"""📚 Système de Gestion de Bibliothèque"""
pass
# ========== BOOKS ==========
@cli.group()
def books():
"""Gestion des livres"""
pass
@books.command()
@click.option('--isbn', prompt='ISBN', help='ISBN du livre')
@click.option('--title', prompt='Titre', help='Titre du livre')
@click.option('--author', prompt='Auteur', help='Auteur')
@click.option('--category', prompt='Catégorie', help='Catégorie')
@click.option('--publisher', prompt='Éditeur', help='Éditeur')
@click.option('--year', prompt='Année', type=int, help='Année de publication')
@click.option('--copies', prompt='Nombre de copies', type=int, default=1)
def add(isbn, title, author, category, publisher, year, copies):
"""Ajouter un livre"""
book = Book(
isbn=isbn, title=title, author=author, category=category,
publisher=publisher, publication_year=year,
total_copies=copies, available_copies=copies
)
if book_repo.add_book(book):
click.echo(click.style(f"✅ Livre ajouté: {title}", fg='green'))
else:
click.echo(click.style(f"❌ Erreur", fg='red'))
@books.command()
@click.option('--isbn', prompt='ISBN', help='ISBN du livre')
def search(isbn):
"""Rechercher un livre par ISBN"""
book = book_repo.get_book_by_isbn(isbn)
if book:
data = [
["ISBN", book.isbn],
["Titre", book.title],
["Auteur", book.author],
["Catégorie", book.category],
["Année", book.publication_year],
["Copies dispo", f"{book.available_copies}/{book.total_copies}"]
]
click.echo("\n" + tabulate(data, tablefmt="grid"))
else:
click.echo(click.style("❌ Livre introuvable", fg='red'))
@books.command()
@click.option('--category', prompt='Catégorie', help='Catégorie')
def list_by_category(category):
"""Lister les livres d'une catégorie"""
books = book_repo.get_books_by_category(category)
if books:
data = [[b['isbn'], b['title'], b['author'], b['available_copies']]
for b in books]
headers = ['ISBN', 'Titre', 'Auteur', 'Disponibles']
click.echo("\n" + tabulate(data, headers=headers, tablefmt="grid"))
else:
click.echo(click.style("Aucun livre trouvé", fg='yellow'))
# ========== USERS ==========
@cli.group()
def users():
"""Gestion des utilisateurs"""
pass
@users.command()
@click.option('--email', prompt='Email', help='Email')
@click.option('--first-name', prompt='Prénom', help='Prénom')
@click.option('--last-name', prompt='Nom', help='Nom')
def register(email, first_name, last_name):
"""Inscrire un utilisateur"""
user_id = user_repo.create_user(email, first_name, last_name)
click.echo(click.style(f"✅ Utilisateur créé: {user_id}", fg='green'))
@users.command()
@click.option('--user-id', prompt='User ID', help='UUID de l\'utilisateur')
def profile(user_id):
"""Voir le profil d'un utilisateur"""
user = user_repo.get_user(UUID(user_id))
if user:
data = [
["ID", user.user_id],
["Nom", f"{user.first_name} {user.last_name}"],
["Email", user.email],
["Inscription", user.registration_date],
["Emprunts totaux", user.total_borrows],
["Emprunts actifs", user.active_borrows]
]
click.echo("\n" + tabulate(data, tablefmt="grid"))
else:
click.echo(click.style("❌ Utilisateur introuvable", fg='red'))
# ========== BORROWS ==========
@cli.group()
def borrows():
"""Gestion des emprunts"""
pass
@borrows.command()
@click.option('--user-id', prompt='User ID', help='UUID de l\'utilisateur')
@click.option('--isbn', prompt='ISBN', help='ISBN du livre')
def borrow(user_id, isbn):
"""Emprunter un livre"""
# Récupérer les infos
user = user_repo.get_user(UUID(user_id))
book = book_repo.get_book_by_isbn(isbn)
if not user:
click.echo(click.style("❌ Utilisateur introuvable", fg='red'))
return
if not book:
click.echo(click.style("❌ Livre introuvable", fg='red'))
return
user_name = f"{user.first_name} {user.last_name}"
if borrow_repo.borrow_book(user.user_id, isbn, book.title, user_name):
click.echo(click.style(f"✅ Emprunt réussi: {book.title}", fg='green'))
else:
click.echo(click.style("❌ Emprunt échoué", fg='red'))
@borrows.command()
@click.option('--user-id', prompt='User ID', help='UUID de l\'utilisateur')
@click.option('--isbn', prompt='ISBN', help='ISBN du livre')
def return_book(user_id, isbn):
"""Retourner un livre"""
if borrow_repo.return_book(UUID(user_id), isbn):
click.echo(click.style("✅ Livre retourné", fg='green'))
else:
click.echo(click.style("❌ Erreur", fg='red'))
@borrows.command()
@click.option('--user-id', prompt='User ID', help='UUID de l\'utilisateur')
def history(user_id):
"""Historique des emprunts"""
borrows = borrow_repo.get_user_borrows(UUID(user_id))
if borrows:
data = [[b['isbn'], b['book_title'], b['borrow_date'], b['status']]
for b in borrows]
headers = ['ISBN', 'Titre', 'Date', 'Statut']
click.echo("\n" + tabulate(data, headers=headers, tablefmt="grid"))
else:
click.echo(click.style("Aucun emprunt", fg='yellow'))
if __name__ == '__main__':
try:
cli()
finally:
db.close()
Utilisation du CLI :
# Ajouter un livre
python cli/main.py books add
# Rechercher un livre
python cli/main.py books search --isbn "978-0-123456-78-9"
# Lister par catégorie
python cli/main.py books list-by-category --category "Science Fiction"
# Inscrire un utilisateur
python cli/main.py users register
# Emprunter
python cli/main.py borrows borrow
# Retourner
python cli/main.py borrows return-book
# Historique
python cli/main.py borrows history --user-id "abc-123-def"