示例#1
0
def get_users_post(user_id, post_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    post = user.posts.where(Post.id == post_id).first()
    if post is not None:
        return jsonify(post.to_dict()), 200
    return error('Selected user does not have the post.', 404)
示例#2
0
def delete_users_post(user_id, post_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    post = user.posts.where(Post.id == post_id).first()
    if post is not None:
        post.delete_instance()
        return message('Deleted.', 200)
    return error('Selected user does not have the post.', 404)
示例#3
0
def delete_user(user_id):
    data = request.get_json() or {}
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    if data.get('delete_posts'):
        for post in user.posts:
            post.delete_instance()
    user.delete_instance()
    return message('Deleted.', 200)
示例#4
0
def edit_users_post(user_id, post_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    if user.posts.where(Post.id == post_id).first() is None:
        return error('Selected user does not have the post.', 404)
    data = request.get_json()
    post_data = {field: data[field] for field in Post._meta.allowed_fields
                 if data.get(field) is not None}
    Post.update(post_data).where(Post.id == post_id).execute()
    post = Post.get_by_id(post_id)
    return jsonify(post.to_dict()), 200
示例#5
0
def get_users_posts(user_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    posts = [post.to_dict() for post in user.posts]
    return jsonify(posts), 200
示例#6
0
def get_user(user_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    return jsonify(user.to_dict()), 200