32 lines
875 B
Python

from sqlalchemy.orm import Session
from app.api.v1.models.posts import Posts
from app.api.utils import get_current_timestamp
def get_posts(db: Session, id: int):
return db.query(Posts).filter(Posts.id == id).first()
def get_all_posts(db: Session):
return db.query(Posts).all()
def create_posts(db: Session, data: dict):
new_post = Posts(**data, created_at=get_current_timestamp())
db.add(new_post)
db.commit()
db.refresh(new_post)
return new_post
def update_posts(db: Session, id: int, data: dict):
post = db.query(Posts).filter(Posts.id == id)
if not post.first():
return None
post.update(data)
db.commit()
return post.first()
def delete_posts(db: Session, id: int):
post = db.query(Posts).filter(Posts.id == id).first()
if not post:
return None
db.delete(post)
db.commit()
return post