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