[main] Updated models according to new database structure

This commit is contained in:
Thastertyn 2025-01-06 21:45:27 +01:00
parent 5b2fe4460e
commit 98d39bba2e
7 changed files with 138 additions and 32 deletions

View File

@ -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.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -8,10 +8,10 @@ Base = declarative_base()
class Author(Base): class Author(Base):
__tablename__ = 'author' __tablename__ = 'author'
__table_args__ = (UniqueConstraint('id'),)
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True)
first_name = Column(String(50), nullable=False) first_name = Column(String(50), nullable=False)
last_name = Column(String(50), nullable=False) last_name = Column(String(50), nullable=False)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()')
books = relationship('Book', back_populates='author')

View File

@ -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.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -8,21 +10,23 @@ from .book_category import BookCategory
Base = declarative_base() Base = declarative_base()
class BookStatusEnum(enum.Enum):
available = 'available'
borrowed = 'borrowed'
reserved = 'reserved'
class Book(Base): class Book(Base):
__tablename__ = 'book' __tablename__ = 'book'
__table_args__ = (UniqueConstraint('isbn'),)
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True)
author_id = Column(Integer, ForeignKey( author_id = Column(Integer, ForeignKey('author.id'), nullable=False)
'author.id'), nullable=False, index=True) title = Column(String(100), nullable=False)
title = Column(String(100), nullable=False) description = Column(Text, nullable=False)
description = Column(Text, nullable=False)
year_published = Column(String(4), nullable=False) year_published = Column(String(4), nullable=False)
isbn = Column(String(13), nullable=False, unique=True) isbn = Column(String(13), nullable=False, unique=True)
status = Column(Enum('available', 'borrowed', 'reserved'), status = Column(Enum(BookStatusEnum), nullable=False, default=BookStatusEnum.available)
nullable=False, default='available') created_at = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()')
created_at = Column(TIMESTAMP, nullable=True, default="CURRENT_TIMESTAMP") last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()')
author = relationship('Author', back_populates='books') author = relationship('Author', backref='books')
# loans = relationship('Loan', back_populates='book')
categories = relationship(
'BookCategory', secondary='book_category_link', back_populates='books')

View File

@ -1,5 +1,5 @@
from .book import Book 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.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -7,13 +7,13 @@ Base = declarative_base()
class BookCategory(Base): class BookCategory(Base):
__tablename__ = 'book_category' __tablename__ = 'book_category'
__table_args__ = (UniqueConstraint('name'),)
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True)
parent_category_id = Column(Integer, ForeignKey( parent_category_id = Column(Integer, ForeignKey('book_category.id'), nullable=True)
'book_category.id'), nullable=True, index=True) name = Column(String(100), nullable=False)
name = Column(String(100), nullable=False, unique=True) mature_content = Column(Integer, nullable=False, default=0)
last_updated = Column(TIMESTAMP, nullable=False, default='CURRENT_TIMESTAMP()')
parent_category = relationship('BookCategory', remote_side=[id]) parent_category = relationship('BookCategory', remote_side=[id])
books = relationship(
'Book', secondary='book_category_link', back_populates='categories')

View File

@ -1,10 +1,18 @@
from sqlalchemy import Column, Integer, ForeignKey from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from .book import Book
from .book_category import BookCategory
Base = declarative_base() Base = declarative_base()
class BookCategoryLink(Base): class BookCategoryLink(Base):
__tablename__ = 'book_category_link' __tablename__ = 'book_category_link'
book_id = Column(Integer, ForeignKey('book.id'), primary_key=True, index=True) book_id = Column(Integer, ForeignKey('book.id'), primary_key=True)
book_category_id = Column(Integer, ForeignKey('book_category.id'), primary_key=True, index=True) book_category_id = Column(Integer, ForeignKey('book_category.id'), primary_key=True)
book = relationship('Book', backref='categories')
book_category = relationship('BookCategory', backref='books')

32
src/models/librarian.py Normal file
View File

@ -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()')

38
src/models/loan.py Normal file
View File

@ -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')

24
src/models/member.py Normal file
View File

@ -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)