class Schedules(db.Model): schedule_id = db.Column(db.Integer, unique=True, primary_key=True, autoincrement=True, nullable=False) employee_id = db.Column(db.Integer, db.ForeignKey('employees.employee_id')) date = db.Column(db.DateTime) time_from = db.Column(db.DateTime) time_to = db.Column(db.DateTime) actual_hours = db.Column(db.Float) expected_hours = db.Column(db.Float) salary = db.Column(db.Float) delete_at = db.Column(db.DateTime, default=None) note = db.Column(db.String(1500)) def serialize(self): return { "id": self.schedule_id, "employee": self.employee.serialize(), "date": self.date, "time_from": self.time_from, "time_to": self.time_to, "note": self.note, "actual_hours": self.actual_hours, "expected_hours": self.expected_hours, "salary": self.salary, "delete_at": self.delete_at } def __repr__(self): return f"Schedule('{self.schedule_id}','{self.employee.serialize()}','{self.date}','{self.time_from}','{self.time_to}'," \ f"'{self.note}','{self.actual_hours}','{self.expected_hours}','{self.salary}', '{self.delete_at}')"
class Member(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(length=30), nullable=False, unique=True) rent = db.Column(db.Integer(), nullable=True, default=0) books = db.relationship('Books', backref='owned_book', lazy=True) def __init__(self, name, rent): self.name = name self.rent = rent
class Member(db.Model): id = db.Column(db.String(20), primary_key=True) name = db.Column(db.String(20), nullable=False) address = db.Column(db.String(120), nullable=False) balance = db.Column(db.Integer, nullable=True, default=0) books = db.relationship('Book', secondary=books, lazy='subquery', backref=db.backref('members', lazy=True))
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) usertype = db.Column(db.String(20), nullable=False) def __repr__(self): return f"User('{self.username}', '{self.email}')"
class Book(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) genre = db.Column(db.String(200), nullable=False) title = db.Column(db.String(200), nullable=False) author = db.Column(db.String(100), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Book('{self.genre}', '{self.title}')"
class Request(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), nullable=False) title = db.Column(db.String(1000), nullable=False, unique=False) timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) def __repr__(self): return f("User('{self.email}', '{self.title}', )")
class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(45), nullable=False) surname = db.Column(db.String(45), nullable=False)
class Transaction(db.Model): id = db.Column(db.String(20), primary_key=True) member_id = db.Column(db.String(20), db.ForeignKey('member.id'), nullable=False) book_id = db.Column(db.String(20), db.ForeignKey('book.id'), nullable=False) date_issued = db.Column(db.DateTime, nullable=False) date_returned = db.Column(db.DateTime)
class Book(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) summary = db.Column(db.String(250), nullable=False) author = relationship('Author', secondary=assoc_author_book, backref='book') def __repr__(self): return self.name
class Book(db.Model): __tablename__ = 'books' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(120), nullable=False) author_id = db.Column(db.Integer, db.ForeignKey('authors.id')) def __repr__(self): return f"<Book {self.title}>" def __str__(self): return f"{self.title}"
class Borrow(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime(60), nullable=False, default=datetime.utcnow) #video 4 minute 17:30 to explains the making of databases def __repr__(self): return f"Borrow('{self.title}','{self.DatePosted}')"
class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String()) barcode = db.Column(db.String()) membertype = db.Column(db.String()) def to_json(self): return { "id": self.id, "name": self.name, "barcode": self.barcode, "memberType": self.membertype, }
class Loan(db.Model): id = db.Column(db.Integer, primary_key=True) due_date = db.Column("due_date", db.DateTime()) book = db.Column("book_id", db.Integer, db.ForeignKey("book.id")) user = db.Column("user_id", db.Integer, db.ForeignKey("user.id")) def to_json(self): return { "book": self.book, "user": self.user, "due_date": self.due_date, "id": self.id, }
class Orderdetails(db.Model): order_id = db.Column(db.Integer, db.ForeignKey('orders.order_id'), primary_key=True) book_id = db.Column(db.Integer, db.ForeignKey('books.book_id'), primary_key=True) retail_price = db.Column(db.Float) quantity = db.Column(db.Integer) discount = db.Column(db.Float) total = db.Column(db.Float) note = db.Column(db.String(1500)) delete_at = db.Column(db.DateTime, default=None) def serialize(self): return { "book": self.book.serialize() if self.book != None else None, "order_id": self.order_id, "note": self.note, "retail_price": self.retail_price, "quantity": self.quantity, "discount": self.discount, "total": self.total, "delete_at": self.delete_at } def __repr__(self): return f"Orderdetail('{self.order.serialize()}','{self.note}','{self.retail_price}','{self.quantity}'," \ f"'{self.discount}','{self.note}', {self.delete_at})"
class Orders(db.Model): order_id = db.Column(db.Integer, unique=True, autoincrement=True, primary_key=True, nullable=False) customer_id = db.Column(db.Integer, db.ForeignKey('customers.customer_id')) employee_id = db.Column(db.Integer, db.ForeignKey('employees.employee_id')) order_date = db.Column(db.DateTime) total = db.Column(db.Float) type = db.Column(db.String(50)) note = db.Column(db.String(1500)) delete_at = db.Column(db.DateTime, default=None) orderdetails = db.relationship('Orderdetails', backref="order", lazy=True) def serialize(self): return { "order_id": self.order_id, "customer": self.customer.serialize(), "note": self.note, "employee": self.employee.serialize(), "order_date": self.order_date, "total": self.order_date, "type": self.type, "delete_at": self.delete_at } def __repr__(self): return f"Order('{self.order_id}', '{self.customer.serialize()}', '{self.employee.serialize()}','{self.note}', '{self.order_date}', '{self.total}', " \ f"'{self.type}', '{self.delete_at}')"
class Book(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String()) isbn = db.Column(db.String()) def to_json(self, entities=False): if not entities: return {"title": self.title, "isbn": self.isbn, "id": self.id} else: return { "id": self.id, "title": self.title, "isbn": self.isbn, "authors": [a.name for a in self.authors], }
class Borrowtickets(db.Model): borrow_ticket_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, unique=True) customer_id = db.Column(db.Integer, db.ForeignKey('customers.customer_id')) employee_id = db.Column(db.Integer, db.ForeignKey('employees.employee_id')) quantity = db.Column(db.Integer) borrow_date = db.Column(db.DateTime) appointment_date = db.Column(db.DateTime) return_date = db.Column(db.DateTime) status = db.Column(db.String(20)) delete_at = db.Column(db.DateTime, default=None) note = db.Column(db.String(1500)) borrow_ticket_detail = db.relationship('Borrowticketdetails', backref='borrowticket', lazy=True) def serialize(self): return { "borrow_ticket_id": self.borrow_ticket_id, "customer": self.customer.serialize() if self.customer != None else None, "note": self.note, "employee": self.employee.serialize() if self.employee != None else None, "quantity": self.quantity, "borrow_date": self.borrow_date, "appointment_date": self.appointment_date, "return_date": self.return_date, "status": self.status, "delete_at": self.delete_at, "borrow_ticket_details": ConvertModelListToDictList(self.borrow_ticket_detail) } def __repr__(self): return f"Borrowticket('{self.borrow_ticket_id}','{self.customer.serialize()}','{self.note}','{self.employee.serialize()}'," \ f"'{self.quantity}','{self.borrow_date}','{self.appointment_date}','{self.return_date}','{self.status}', '{self.delete_at}')"
class Authors(db.Model): author_id = db.Column(db.Integer, unique=True, primary_key=True, nullable=False, autoincrement=True) author_name = db.Column(db.String(50)) books = db.relationship('Books', backref='author', lazy=True) delete_at = db.Column(db.DateTime, default=None) def serialize(self): return {"author_id": self.author_id, "author_name": self.author_name} def __repr__(self): return f"Author('{self.author_id}','{self.author_name}')"
class Author(db.Model): __tablename__ = 'authors' id = db.Column(db.Integer, primary_key=True, autoincrement=True) first_name = db.Column(db.String(64), nullable=False) middle_name = db.Column(db.String(64)) last_name = db.Column(db.String(64), nullable=False) books = db.relationship('Book', backref='author', cascade='all, delete-orphan', lazy='select') def __repr__(self): return f"<Author {self.last_name} {self.first_name[0]}. {self.middle_name[0]}.>" def __str__(self): return f"{self.last_name} {self.first_name[0]}. {self.middle_name[0]}."
class Comments(db.Model): comment_id = db.Column(db.Integer, primary_key=True) content = db.Column(db.TEXT) create_at = db.Column(db.DateTime) book_id = db.Column(db.Integer, db.ForeignKey('books.book_id')) customer_id = db.Column(db.Integer) def serialize(self): return { "comment_id": self.comment_id, "content": self.content, "book_id": self.book_id, "customer_id": self.customer_id, "create_at": self.create_at, 'book': self.book.serialize() if self.book != None else None, }
class Suppliers(db.Model): supplier_id = db.Column(db.Integer, unique=True, autoincrement=True, primary_key=True, nullable=False) contact_name = db.Column(db.String(50), nullable=False) address = db.Column(db.String(1500)) phone = db.Column(db.String(50)) email = db.Column(db.String(50), nullable=False) note = db.Column(db.String(1500)) delete_at = db.Column(db.DateTime, default=None) books = db.relationship('Books', backref='supplier', lazy=True) def serialize(self): return { "supplier_id": self.supplier_id, "contact_name": self.contact_name, "note": self.note, "address": self.address, "phone": self.phone, "email": self.email, "delete_at": self.delete_at } def __repr__(self): return f"Supplier('{self.supplier_id}','{self.contact_name}','{self.note}','{self.address}','{self.phone}'," \ f"'{self.email}', '{self.delete_at}')"
class District(db.Model): id = db.Column(db.String(10), nullable=False) code = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50)) provinceId = db.Column(db.String(50), name="province_id") def __init__(self, id, name, provinceId) -> None: self.id = id self.name = name self.provinceId = provinceId super().__init__() def serialize(self): return { "id": self.id, "name": self.name, "provinceId": self.provinceId, }
class Province(db.Model): id = db.Column(db.String(10), nullable=False) code = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50)) region = db.Column(db.String(50), name="region") def __init__(self, id, name, region) -> None: self.id = id self.name = name self.region = region super().__init__() def serialize(self): return { "id": self.id, "name": self.name, "region": self.region, }
class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String()) books = db.relationship( "Book", secondary=books_authors, lazy="subquery", backref=db.backref("authors", lazy=True), ) def to_json(self, entities=False): if not entities: return {"name": self.name, "id": self.id} else: return { "name": self.name, "id": self.id, "books": [book.title for book in self.books], }
class Borrowticketdetails(db.Model): borrow_ticket_id = db.Column( db.Integer, db.ForeignKey('borrowtickets.borrow_ticket_id'), primary_key=True) book_id = db.Column(db.Integer, db.ForeignKey('books.book_id'), primary_key=True) delete_at = db.Column(db.DateTime, default=None) def serialize(self): return { "borrow_ticket_id": self.borrow_ticket_id, "book_id": self.book_id, "delete_at": self.delete_at, 'book': self.book.serialize() if self.book != None else None } def __repr__(self): return f"('book_id':{self.book_id}, 'borrow_ticket_id': {self.borrow_ticket_id})"
class Roles(db.Model): role_id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True, unique=True) role_name = db.Column(db.String(50)) note = db.Column(db.String(1500)) delete_at = db.Column(db.DateTime, default=None) accounts = db.relationship('Accounts', backref="role", lazy=True) def serialize(self): return { "role_id": self.role_id, "role_name": self.role_name, "note": self.note, "delete_at": self.delete_at } def __repr__(self): return f"Role('{self.role_id}','{self.role_name}','{self.note}', '{self.delete_at}')"
class Messages(db.Model): message_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, unique=True) conversation_id = db.Column(db.Integer, db.ForeignKey('conversations.conversation_id'), nullable=False) content = db.Column(db.String(2000)) created_at = db.Column(db.DateTime) deleted_at = db.Column(db.DateTime) account_id = db.Column(db.Integer, db.ForeignKey('accounts.account_id'), nullable=False) def serialize(self): return { 'message_id': self.message_id, 'conversation_id': self.conversation_id, 'content': self.content, 'created_at': self.created_at, 'account_id': self.account_id, 'deleted_at': self.deleted_at, }
class Categories(db.Model): category_id = db.Column(db.Integer, primary_key=True, nullable=False, unique=True) books = db.relationship("Books", backref="category", lazy=False) category_name = db.Column(db.String(50)) image = db.Column(db.String(1500)) description = db.Column(db.String(1500)) note = db.Column(db.String(1500)) delete_at = db.Column(db.DateTime, default=None) books = db.relationship('Books', backref="category", lazy=True) def serialize(self): return { "category_id": self.category_id, "category_name": self.category_name, "note": self.note, "description": self.description, "delete_at": self.delete_at, "image": self.image } def __repr__(self): return f"Category('{self.category_id}','{self.category_name}','{self.note}','{self.description}', '{self.delete_at}')"
class Book(db.Model): id = db.Column(db.String(20), primary_key=True) name = db.Column(db.String(20), nullable=False) author = db.Column(db.String(120), nullable=False) description = db.Column(db.String(60), nullable=False) quantity = db.Column(db.Integer, nullable=False) total_quantity = db.Column(db.Integer, nullable=False)
class Conversations(db.Model): conversation_id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False, unique=True) customer_account_id = db.Column(db.Integer, db.ForeignKey('accounts.account_id'), primary_key=True, nullable=False, unique=True) messages = db.relationship('Messages', backref='conversation', lazy=True) created_at = db.Column( db.DateTime) #Ngay khi tạo tài khoản customer thành công updated_at = db.Column(db.DateTime) #Ngay khi tin nhắn gần nhất được gửi last_message = db.Column(db.String(2000)) is_read = db.Column(db.Boolean) def serialize(self): return { 'conversation_id': self.conversation_id, 'customer_account_id': self.customer_account_id, 'created_at': self.created_at, 'updated_at': self.updated_at, 'last_message': self.last_message, 'account': self.account.serialize() if self.account != None else None, 'is_read': self.is_read }