def get_blogs(): # fetch posts from database session = db.session() post_objects = session.query(Post).order_by(Post.date_posted.desc()).all() # transform into Json serializable objects schema = PostSchema(many=True) posts = schema.dump(post_objects)[0] # serialize as Json session.close() return jsonify(posts)
def add_blog(): # create author object posted_post = PostSchema(only=('title', 'thumbnail', 'author_id', 'preview', 'content'))\ .load(request.form) post = Post(**posted_post.data) # add new author to database session = db.session() session.add(post) session.commit() # return newly added author new_post = PostSchema().dump(post) return jsonify(new_post)
def get_post(id): session = db.session() post_object = session.query(Post).get(id) post = PostSchema().dump(post_object)[0] session.close() return jsonify(post)
async def add_post(post: PostSchema) -> dict: post.id = len(posts) + 1 posts.append(post.dict()) return {"data": "post added."}