diff --git a/src/models/author.py b/src/models/author.py index a26c49e..224259d 100644 --- a/src/models/author.py +++ b/src/models/author.py @@ -1,12 +1,17 @@ -from sqlalchemy import Column, Integer, VARCHAR +from sqlalchemy import Column, Integer, String from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base +from .book import Book + Base = declarative_base() -class Author(Base): - __tablename__ = "author" - id = Column(Integer, primary_key=True) - first_name = Column(VARCHAR(length=50)) - last_name = Column(VARCHAR(length=50)) \ No newline at end of file +class Author(Base): + __tablename__ = 'author' + + 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') diff --git a/src/models/book.py b/src/models/book.py new file mode 100644 index 0000000..c423c32 --- /dev/null +++ b/src/models/book.py @@ -0,0 +1,28 @@ +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + +from .author import Author +from .book_category import BookCategory + +Base = declarative_base() + + +class Book(Base): + __tablename__ = 'book' + + 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) + 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") + + author = relationship('Author', back_populates='books') + # loans = relationship('Loan', back_populates='book') + categories = relationship( + 'BookCategory', secondary='book_category_link', back_populates='books') diff --git a/src/models/book_category.py b/src/models/book_category.py new file mode 100644 index 0000000..021b045 --- /dev/null +++ b/src/models/book_category.py @@ -0,0 +1,19 @@ +from .book import Book +from sqlalchemy import Column, Integer, String, TIMESTAMP, Text, ForeignKey, Enum +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base + +Base = declarative_base() + + +class BookCategory(Base): + __tablename__ = 'book_category' + + 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) + + 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 new file mode 100644 index 0000000..f1057d7 --- /dev/null +++ b/src/models/book_category_link.py @@ -0,0 +1,10 @@ +from sqlalchemy import Column, Integer, ForeignKey +from sqlalchemy.ext.declarative import declarative_base + +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