def get_post(post_id): post = Post.query.get(post_id); if (post == None): abort(404); schema = PostSerializer(); data, errors = schema.dump(post); return jsonify({'post':make_public_post(data)});
def create_post(): # 400: bad request if not request.json or not 'title' in request.json or not 'body' in request.json: abort(400); post = Post(request.json['title'], request.json['body']); db.session.add(post); db.session.commit(); # 查询并返回。 post = Post.query.get(post.id); schema = PostSerializer(); data, errors = schema.dump(post); return jsonify({'post': make_public_post(data)}), 201;
def update_post(post_id): post = Post.query.get(post_id); if post == None: abort(404); if not request.json: abort(404); if 'title' in request.json and type(request.json['title']) != str: abort(400); if 'body' in request.json and type(request.json['body']) != str: abort(400); post.title = request.json.get('title'); post.body = request.json.get('body'); db.session.commit(); # 查询并返回。 post = Post.query.get(post.id); schema = PostSerializer(); data, errors = schema.dump(post); return jsonify({'post': make_public_post(data)});
def get_posts(): if request.method == 'GET': posts = Post.query.order_by(Post.created_at.desc()); schema = PostSerializer(); data, errors = schema.dump(posts, many=True); return jsonify({"posts": [make_public_post(post) for post in data]});