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
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
def get_db(): db = Session() try: yield db finally: db.close()
def __enter__(self): self._session = Session() self._query = self._session.query return self
def delete_post(db: Session, post_id: int): db_post = db.query(models.Post).get(post_id) db.delete(db_post) db.commit()
def get_post_list(db: Session): return db.query(models.Post).all()
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()