From 98d39bba2e726e6ad3be21f91d3505dc7b8407d1 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Mon, 6 Jan 2025 21:45:27 +0100 Subject: [PATCH] [main] Updated models according to new database structure --- src/models/author.py | 14 ++++++------ src/models/book.py | 34 +++++++++++++++------------- src/models/book_category.py | 16 +++++++------- src/models/book_category_link.py | 12 ++++++++-- src/models/librarian.py | 32 +++++++++++++++++++++++++++ src/models/loan.py | 38 ++++++++++++++++++++++++++++++++ src/models/member.py | 24 ++++++++++++++++++++ 7 files changed, 138 insertions(+), 32 deletions(-) create mode 100644 src/models/librarian.py create mode 100644 src/models/loan.py create mode 100644 src/models/member.py diff --git a/src/models/author.py b/src/models/author.py index 224259d..4c24543 100644 --- a/src/models/author.py +++ b/src/models/author.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, String +from sqlalchemy import Column, Integer, String, TIMESTAMP, UniqueConstraint from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base @@ -8,10 +8,10 @@ Base = declarative_base() class Author(Base): - __tablename__ = 'author' + __tablename__ = 'author' + __table_args__ = (UniqueConstraint('id'),) - id = Column(Integer, primary_key=True, autoincrement=True) - first_name = Column(String(50), nullable=False) - last_name = Column(String(50), nullable=False) - - books = relationship('Book', back_populates='author') + id = Column(Integer, primary_key=True) + first_name = Column(String(50), nullable=False) + last_name = Column(String(50), nullable=False) + last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') diff --git a/src/models/book.py b/src/models/book.py index c423c32..219c43c 100644 --- a/src/models/book.py +++ b/src/models/book.py @@ -1,4 +1,6 @@ -from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum +import enum + +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base @@ -8,21 +10,23 @@ from .book_category import BookCategory Base = declarative_base() +class BookStatusEnum(enum.Enum): + available = 'available' + borrowed = 'borrowed' + reserved = 'reserved' + class Book(Base): - __tablename__ = 'book' + __tablename__ = 'book' + __table_args__ = (UniqueConstraint('isbn'),) - id = Column(Integer, primary_key=True, autoincrement=True) - author_id = Column(Integer, ForeignKey( - 'author.id'), nullable=False, index=True) - title = Column(String(100), nullable=False) - description = Column(Text, nullable=False) + id = Column(Integer, primary_key=True) + author_id = Column(Integer, ForeignKey('author.id'), nullable=False) + title = Column(String(100), nullable=False) + description = Column(Text, nullable=False) year_published = Column(String(4), nullable=False) - isbn = Column(String(13), nullable=False, unique=True) - status = Column(Enum('available', 'borrowed', 'reserved'), - nullable=False, default='available') - created_at = Column(TIMESTAMP, nullable=True, default="CURRENT_TIMESTAMP") + isbn = Column(String(13), nullable=False, unique=True) + status = Column(Enum(BookStatusEnum), nullable=False, default=BookStatusEnum.available) + created_at = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') + last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') - author = relationship('Author', back_populates='books') - # loans = relationship('Loan', back_populates='book') - categories = relationship( - 'BookCategory', secondary='book_category_link', back_populates='books') + author = relationship('Author', backref='books') diff --git a/src/models/book_category.py b/src/models/book_category.py index 021b045..939e844 100644 --- a/src/models/book_category.py +++ b/src/models/book_category.py @@ -1,5 +1,5 @@ from .book import Book -from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum +from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, UniqueConstraint from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base @@ -7,13 +7,13 @@ Base = declarative_base() class BookCategory(Base): - __tablename__ = 'book_category' + __tablename__ = 'book_category' + __table_args__ = (UniqueConstraint('name'),) - id = Column(Integer, primary_key=True, autoincrement=True) - parent_category_id = Column(Integer, ForeignKey( - 'book_category.id'), nullable=True, index=True) - name = Column(String(100), nullable=False, unique=True) + id = Column(Integer, primary_key=True) + parent_category_id = Column(Integer, ForeignKey('book_category.id'), nullable=True) + name = Column(String(100), nullable=False) + mature_content = Column(Integer, nullable=False, default=0) + last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') parent_category = relationship('BookCategory', remote_side=[id]) - books = relationship( - 'Book', secondary='book_category_link', back_populates='categories') diff --git a/src/models/book_category_link.py b/src/models/book_category_link.py index f1057d7..adde605 100644 --- a/src/models/book_category_link.py +++ b/src/models/book_category_link.py @@ -1,10 +1,18 @@ from sqlalchemy import Column, Integer, ForeignKey +from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base + +from .book import Book +from .book_category import BookCategory + Base = declarative_base() class BookCategoryLink(Base): __tablename__ = 'book_category_link' - book_id = Column(Integer, ForeignKey('book.id'), primary_key=True, index=True) - book_category_id = Column(Integer, ForeignKey('book_category.id'), primary_key=True, index=True) \ No newline at end of file + book_id = Column(Integer, ForeignKey('book.id'), primary_key=True) + book_category_id = Column(Integer, ForeignKey('book_category.id'), primary_key=True) + + book = relationship('Book', backref='categories') + book_category = relationship('BookCategory', backref='books') \ No newline at end of file diff --git a/src/models/librarian.py b/src/models/librarian.py new file mode 100644 index 0000000..06c21e2 --- /dev/null +++ b/src/models/librarian.py @@ -0,0 +1,32 @@ +import enum + +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + + +Base = declarative_base() + + +class LibrarianStatusEnum(enum.Enum): + active = 'active' + inactive = 'inactive' + +class LibrarianRoleEnum(enum.Enum): + staff = 'staff' + admin = 'admin' + + +class Librarian(Base): + __tablename__ = 'librarian' + __table_args__ = (UniqueConstraint('id'),) + + id = Column(Integer, primary_key=True) + first_name = Column(String(50), nullable=False) + last_name = Column(String(50), nullable=False) + email = Column(String(100), nullable=False, unique=True) + phone = Column(String(20), nullable=False) + hire_date = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') + status = Column(Enum(LibrarianStatusEnum), nullable=False, default=LibrarianStatusEnum.active) + role = Column(Enum(LibrarianRoleEnum), nullable=False) + last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') diff --git a/src/models/loan.py b/src/models/loan.py new file mode 100644 index 0000000..3b2d976 --- /dev/null +++ b/src/models/loan.py @@ -0,0 +1,38 @@ +import enum + +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint, Float +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + +from .book import Book +from .member import Member +from .librarian import Librarian + +Base = declarative_base() + + +class LoanStatusEnum(enum.Enum): + borrowed = 'borrowed' + returned = 'returned' + overdue = 'overdue' + reserved = 'reserved' + + +class Loan(Base): + __tablename__ = 'loan' + __table_args__ = (UniqueConstraint('id'),) + + id = Column(Integer, primary_key=True) + book_id = Column(Integer, ForeignKey('book.id'), nullable=False) + member_id = Column(Integer, ForeignKey('member.id'), nullable=False) + librarian_id = Column(Integer, ForeignKey('librarian.id'), nullable=False) + loan_date = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') + due_date = Column(TIMESTAMP, nullable=False) + return_date = Column(TIMESTAMP, nullable=True) + status = Column(Enum(LoanStatusEnum), nullable=False, default=LoanStatusEnum.borrowed) + overdue_fee = Column(Float, nullable=True) + last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()') + + book = relationship('Book', backref='loans') + member = relationship('Member', backref='loans') + librarian = relationship('Librarian', backref='loans') \ No newline at end of file diff --git a/src/models/member.py b/src/models/member.py new file mode 100644 index 0000000..e733e01 --- /dev/null +++ b/src/models/member.py @@ -0,0 +1,24 @@ +import enum + +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum, UniqueConstraint +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + +class MemberStatusEnum(enum.Enum): + active = 'active' + inactive = 'inactive' + +Base = declarative_base() + +class Member(Base): + __tablename__ = 'member' + __table_args__ = (UniqueConstraint('id'),) + + id = Column(Integer, primary_key=True) + first_name = Column(String(50), nullable=False) + last_name = Column(String(50), nullable=False) + email = Column(String(100), nullable=False, unique=True) + phone = Column(String(20), nullable=True) + register_date = Column(TIMESTAMP, nullable=True, default='CURRENT_TIMESTAMP()') + status = Column(Enum(MemberStatusEnum), nullable=True, default=MemberStatusEnum.active) + last_updated = Column(TIMESTAMP, nullable=True) \ No newline at end of file