[main] Added the new table as a model

This commit is contained in:
Thastertyn 2025-01-15 23:05:34 +01:00
parent ec55394d79
commit 6a42e522f9
5 changed files with 39 additions and 8 deletions

View File

@ -29,9 +29,8 @@ def create_new_book(book: Book):
def update_book(book: Book):
session = DatabaseManager.get_session()
try:
with session:
with DatabaseManager.get_session() as session:
logger.debug(f"Updating book {book.title}")
existing_book = session.query(Book).get(book.id)
@ -57,10 +56,10 @@ def update_book(book: Book):
raise DatabaseConnectionError(
"Connection with database interrupted") from e
except SQLAlchemyError as e:
logger.error(f"An error occured when saving book: {e}")
logger.error(f"An error occurred when saving book: {e}")
session.rollback()
raise DatabaseError(
"An error occured when updating the book") from e
"An error occurred when updating the book") from e
__all__ = ["create_new_book", "update_book"]

View File

@ -1,14 +1,19 @@
from typing import List, Dict
import os
import logging
import time
from typing import List, Dict
from xml.etree import ElementTree as ET
from xmlschema import XMLSchema
from sqlalchemy.exc import IntegrityError
from database.manager import DatabaseManager
from utils.errors.import_error.xsd_scheme_not_found import XsdSchemeNotFoundError
from utils.errors.import_error.invalid_contents_error import InvalidContentsError
from utils.errors.import_error.import_error import ImportError
from utils.config import UserConfig
from models import Book, Author, BookCategory
from sqlalchemy.exc import IntegrityError
class BookImporter:
def __init__(self):
@ -124,6 +129,12 @@ class BookImporter:
categories=filtered_categories
)
session.add(book)
user_config = UserConfig()
if user_config.simulate_slowdown:
self.logger.info("Simulating slowdown for 10 seconds")
time.sleep(10)
# Commit all changes
session.commit()
except e:

View File

@ -2,6 +2,7 @@ from .author import *
from .book import *
from .book_category import *
from .book_category_link import *
from .book_category_statistics import *
from .book_overview import *
from .member import *
from .librarian import *
@ -12,6 +13,7 @@ __all__ = [
*book.__all__,
*book_category.__all__,
*book_category_link.__all__,
*book_category_statistics.__all__,
*book_overview.__all__,
*member.__all__,
*librarian.__all__,

View File

@ -26,8 +26,9 @@ class Book(Base):
created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now())
author = relationship('Author', back_populates='books')
categories = relationship('BookCategory',secondary='book_category_link',back_populates='books')
author = relationship('Author', back_populates='books')
categories = relationship('BookCategory',secondary='book_category_link',back_populates='books')
book_category_statistics = relationship('BookCategoryStatistics', back_populates='book_category_statistics')
__all__ = ["Book", "BookStatusEnum"]

View File

@ -0,0 +1,18 @@
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from .base import Base
class BookCategoryStatistics(Base):
__tablename__ = 'book_category_statistics'
book_category_id = Column(Integer, ForeignKey('book_category.id', ondelete="cascade"), primary_key=True)
count = Column(Integer(unsigned=True), nullable=False, default=0)
category = relationship(
'BookCategory',
back_populates='book_category_statistics'
)
__all__ = ["BookCategoryStatistics"]