Viral-808 / endpoints /database.py
Sam Fred
Commit
58e450d
raw
history blame
361 Bytes
from sqlalchemy.orm import Session
from models import Post, SessionLocal
# Get database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Save post to database
def save_post(db: Session, post_data: dict):
post = Post(**post_data)
db.add(post)
db.commit()
db.refresh(post)
return post