"""Canned Response Category model.""" from product_review.api import db class CannedResponseCategory(db.Model): """Canned Response Category model.""" __tablename__ = "canned_response_category" category_id = db.Column( "id", db.Integer, primary_key=True, autoincrement=True, nullable=False ) name = db.Column(db.String(80), nullable=False) def to_dict(self): """Convert the CannedResponseCategory object to a dictionary representation.""" item = {} for c in self.__table__.columns: column_name = "category_id" if c.name == "id" else c.name item[column_name] = getattr(self, column_name) return item def get_items(): """Get all canned response categories.""" return [item.to_dict() for item in CannedResponseCategory.query.all()] def create_category(name): """Add a record to canned_response_category table.""" existing_entry = ( db.session.query(CannedResponseCategory) .filter(CannedResponseCategory.name == name) .first() ) if existing_entry: return existing_entry else: new_entry = CannedResponseCategory(name=name) db.session.add(new_entry) return new_entry