def delete_post(id): try: post = Post.get(Post.id == id) post.delete_instance() db.close() return {"msg": "post deleted"}, 200 except: db.close() return {"error": "post does not exists"}, 400
def add_post(): title = g.data['title'] content = g.data['content'] new_post = Post.create(title=title, content=content) new_post.save() db.close() return {"msg": "New post added"}, 201
def update_post(id): title = g.data['title'] content = g.data['content'] query = Post.update(title=title, content=content).where(Post.id == id) if query.execute() == 1: db.close() return {'msg': 'post updated'}, 200 else: db.close() return {'error': 'post not found'}, 400
def get_one_post(id): one_post = {} for post in Post.select().where(Post.id == id): one_post.update({ 'id': post.id, 'title': post.title, 'content': post.content }) db.close() return one_post
def get_all_posts(): all_posts = [] for post in Post.select(): all_posts.append({ 'id': post.id, 'title': post.title, 'content': post.content }) db.close() return jsonify(all_posts), 200