예제 #1
0
def update_post(db: Session, post: Post):
    db_post = db.query(models.Post).get(post.id)
    post_dict = get_dict_from_model(post)
    for key, value in post_dict.items():
        setattr(db_post, key, value)
    db.commit()
    db.refresh(db_post)

    return db_post
예제 #2
0
def create_post(db: Session, post: PostCreate):
    db_post = models.Post(title=post.title, content=post.content)
    db.add(db_post)
    db.commit()
    db.refresh(db_post)

    return db_post
예제 #3
0
def get_db():
    db = Session()
    try:
        yield db
    finally:
        db.close()
예제 #4
0
 def __enter__(self):
     self._session = Session()
     self._query = self._session.query
     return self
예제 #5
0
def delete_post(db: Session, post_id: int):
    db_post = db.query(models.Post).get(post_id)

    db.delete(db_post)
    db.commit()
예제 #6
0
def get_post_list(db: Session):
    return db.query(models.Post).all()
예제 #7
0
def get_post(db: Session, post_id: int):
    db_post = db.query(models.Post).filter(models.Post.id == post_id)
    return None if db_post.count() > 1 else db_post.first()