Exemple #1
0
def add_vote(user_id: int, snack_id: int) -> int:
    """
    Adds a vote for the snack by the user

    raises:
        - VotesExceededException if the user has exceeded allotted votes
        - UserNotFoundException if the user_id is not found

    returns:
        - vote_count int
    """

    # count votes for the user that haven't expired
    user_votes: int = users.get_user_votes(user_id)

    if user_votes >= max_votes:
        raise VotesExceededException

    # create new vote
    new_vote = Vote(user_id=user_id,
                    snack_id=snack_id,
                    vote_expiry=vote_expiration())

    session = Session()

    session.add(new_vote)
    session.commit()
    session.close()

    return user_votes + 1
Exemple #2
0
def create_user(username: str, password: str) -> User:
    """
    adds user to database

    :param username: str username
    :param password: str password
    :return: User
    """
    session = Session()

    hash_pw = bcrypt.hashpw(password.encode('utf-8'),
                            bcrypt.gensalt()).decode('utf-8')
    new_user: User = User(username=username, password_hash=hash_pw)
    # TODO: handle generic sql errors
    try:
        session.add(new_user)
        session.commit()
        # gets id
        session.refresh(new_user)
        session.close()
        return new_user
    except IntegrityError as e:
        session.rollback()
        session.close()
        raise UserAlreadyExistsException