Exemple #1
0
def unfollow_by_username(username, follow_username):
    user = get_user_by_username_dict(username=username)
    follow = get_user_by_username_dict(username=follow_username)
    if not user or not follow:
        return abort(404, "Usuário ou Seguidor Inválido.")
    data = Follow.query.filter_by(user_id=user["id"], follower_id=follow["id"]).first()
    db.session.delete(data)
    db.session.commit()
    return {"message": "Deixou de seguir."}
Exemple #2
0
def create_follow_by_username(username, data):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    id = user.id
    for follow in data:
        user = get_user_by_username_dict(username=follow["username"])
        if user and user.username != username:
            follower = Follow(user_id=id, follower_id=user.id)
            db.session.add(follower)
    db.session.commit()
    return {"message": "followers added"}, 201
Exemple #3
0
def create_post_by_username(username, data):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    post = Post(user_id=user.id, content=data["content"])
    db.session.add(post)
    db.session.commit()
    db.session.flush()
    return {"message": "post created.", "post": serial_post.dump(post)}, 201
Exemple #4
0
def delete_post_by_username(username, post_id):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    post = Post.query.filter_by(user_id=user.id, id=post_id).first()
    if not post:
        return abort(404, "Publicação não encontrada")
    db.session.delete(post)
    db.session.commit()
    return {"message": "post deleted."}
Exemple #5
0
def update_post_by_username(username, post_id, data):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    post = Post.query.filter_by(user_id=user.id, id=post_id).first()
    if not post:
        return abort(404, "Publicação não encontrada")
    db.session.query(Post).filter_by(user_id=user.id, id=post_id).update(data)
    db.session.commit()
    return {"messege": "post updated"}
Exemple #6
0
def get_followers(username):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado.")
    followers = Follow.query.filter_by(user_id=user.id).all()
    if not followers:
        return abort(404, "Nenhum seguidor encontrado.")

    list_follewers = []
    for follow in followers:
        follow_dump = serial_user.dump(follow.follower)
        follow_dump.pop("password")
        follow_dump.pop("email")
        list_follewers.append(follow_dump)
    user_dump = serial_user.dump(user)
    return {**user_dump, "followers": list_follewers}
Exemple #7
0
def get_post(username, post_id):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    post = Post.query.filter_by(user_id=user.id, id=post_id).first()
    return serial_post.dump(post)
Exemple #8
0
def get_posts(username):
    user = get_user_by_username_dict(username=username)
    if not user:
        return abort(404, "Usuário não encontrado")
    post = Post.query.filter_by(user_id=user.id).all()
    return {"posts": serial_post_list.dump(post)}