33 lines
929 B
Python
33 lines
929 B
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):
|
|
obj = Comments(**data, created_at=get_current_timestamp())
|
|
db.add(obj)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
def update_comments(db: Session, id: int, data: dict):
|
|
obj = db.query(Comments).filter(Comments.id == id).first()
|
|
if obj:
|
|
for key, value in data.items():
|
|
setattr(obj, key, value)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
def delete_comments(db: Session, id: int):
|
|
obj = db.query(Comments).filter(Comments.id == id).first()
|
|
if obj:
|
|
db.delete(obj)
|
|
db.commit()
|
|
return obj
|