34 lines
1.1 KiB
Python

from sqlalchemy.orm import Session
from app.api.v1.models.comments import Comments
from app.api.utils import get_current_timestamp
def get_comments(db: Session, id: int):
return db.query(Comments).filter(Comments.id == id).first()
def get_all_comments(db: Session):
return db.query(Comments).all()
def create_comments(db: Session, data: dict):
timestamp = get_current_timestamp()
new_comment = Comments(**data, created_at=timestamp, updated_at=timestamp)
db.add(new_comment)
db.commit()
db.refresh(new_comment)
return new_comment
def update_comments(db: Session, id: int, data: dict):
comment = db.query(Comments).filter(Comments.id == id)
if not comment.first():
return None
timestamp = get_current_timestamp()
comment.update({**data, "updated_at": timestamp}, synchronize_session=False)
db.commit()
return comment.first()
def delete_comments(db: Session, id: int):
comment = db.query(Comments).filter(Comments.id == id).first()
if not comment:
return None
db.delete(comment)
db.commit()
return comment