Exemple #1
0
def get_a_user(user_id):
    user = User.get_user_by_id(user_id)
    if not user:
        return custom_response({'error': 'produto nao encontrado'}, 404)
    else:
        user = user[0].to_dict_json()
    return custom_response(user, 200)
Exemple #2
0
def get_a_item(item_id):
    item = Item.get_item_by_id(item_id)
    if not item:
        return custom_response({'error': 'Item nao encontrado'}, 404)
    else:
        item = item[0].to_dict_json()
    return custom_response(item, 200)
Exemple #3
0
def delete():
    req_data = request.get_json()
    bid = Bid.get_bid_by_id(req_data['id'])
    if not bid:
        return custom_response({'error': 'Bid nao encontrado'}, 404)
    bid.delete()
    return custom_response({'message': 'Bid removido'}, 204)
Exemple #4
0
def get_a_bid(bid_id):
    bid = Bid.get_bid_by_id(bid_id)
    if not bid:
        return custom_response({'error': 'Bid nao encontrada'}, 404)
    else:
        bid = bid.to_dict_json()
    return custom_response(bid, 200)
Exemple #5
0
def delete():
    req_data = request.get_json()
    item = Item.get_item_by_name(req_data['id'])
    if not item:
        return custom_response({'error': 'Item nao encontrado'}, 404)
    item.delete()
    return custom_response({'message': 'Item removido'}, 204)
Exemple #6
0
def toggle():
    req_data = request.get_json()
    item = Item.get_item_by_name(req_data['nome'])
    if not item:
        return custom_response({'error': 'Item nao encontrado'}, 404)
    item_svc.toggle_item(item)
    return custom_response({'message': 'Estado de item alterado'}, 200)
Exemple #7
0
def delete():
    req_data = request.get_json()
    user = User.get_user_by_id(req_data['id'])
    if not user:
        return custom_response({'error': 'User nao encontrado'}, 404)
    user.delete()
    return custom_response({'message': 'User removido'}, 204)
Exemple #8
0
def get_all():
    bids = Bid.get_all_bids()
    if not bids:
        return custom_response({'error': 'Não existem bids cadastradas'}, 404)
    BidsJson = []
    for bid in bids:
        BidsJson.append(bid.to_dict_json())
    return custom_response(BidsJson, 200)
Exemple #9
0
def get_item_bids(item_id):
    bids = Bid.get_item_bids(item_id)
    if not bids:
        return custom_response({'error': 'Bids nao encontrada'}, 404)
    else:
        bids_array = []
        for bid in bids[::-1]:
            bids_array.append(bid.to_dict_json())
    return custom_response(bids_array, 200)
Exemple #10
0
def get_all():
    users = User.get_all_user()
    if not users:
        return custom_response({'error': 'Não existem produtos cadastrados'},
                               404)
    UsersJson = []
    for user in users:
        UsersJson.append(user.to_dict_json())
    return custom_response(UsersJson, 200)
Exemple #11
0
def get_all():
    items = Item.get_all_items()
    if not items:
        return custom_response({'error': 'Não existem produtos cadastrados'},
                               404)
    ItemsJson = []
    for item in items:
        ItemsJson.append(item.to_dict_json())
    return custom_response(ItemsJson, 200)
Exemple #12
0
def create():
    req_data = request.get_json()
    try:
        data = user_schema.load(req_data)
    except:
        return custom_response({'error': 'Body incorreto'}, 400)
    user = User(data)
    user.save()

    return custom_response({'user': user.to_dict_json()}, 201)
Exemple #13
0
def update():
    try:
        req_data = request.get_json()
        user = User.get_user_by_email(req_data['email'])
        if user:
            user = user_svc.update_user(req_data, user)
            return custom_response(user, 200)

    except ValidationError as e:
        return custom_response(str(e), 400)
Exemple #14
0
def update():
    try:
        req_data = request.get_json()
        item = Item.get_item_by_name(req_data['name'])
        if item:
            item = item_svc.update_item(req_data, item)
            return custom_response(item, 200)
        else:
            return custom_response({"status": "not found"}, 404)

    except ValidationError as e:
        return custom_response(str(e), 400)
Exemple #15
0
def create():
    req_data = request.get_json()
    if 'bid_auto_increment' not in req_data or req_data[
            'bid_auto_increment'] <= 0:
        req_data['bid_auto_increment'] = 1
    try:
        data = item_schema.load(req_data)
    except:
        return custom_response({'error': 'Body incorreto'}, 400)
    item = Item(data)
    item.save()

    return custom_response({'item': item.to_dict_json()}, 201)
Exemple #16
0
def login():
    req_data = request.get_json()
    user_data = {
        'email': req_data['email'],
        'password': req_data['password'],
    }
    if (user_data.get('email') == ''):
        return custom_response({'error': 'Insert an email'}, 400)
    if (user_data.get('password') == ''):
        return custom_response({'error': 'Insert a password'}, 400)
    user = User.get_user_by_email(user_data.get('email'))
    if not user:
        return custom_response({'error': 'User not found'}, 404)
    user = User.to_dict_json(user[0])
    if not User.check_hash(user, user_data.get('password')):
        return custom_response({'error': 'Password incorrect'}, 400)
    token = Auth.generate_token(user_data.get('email'))
    return custom_response({'jwt_token': token, 'user': user}, 200)
Exemple #17
0
def create():
    req_data = request.get_json()
    try:
        user = User.get_user_by_email(req_data['user_email'])
        item = Item.get_item_by_name(req_data['item_name'])
        bid = {
            'bid_value': req_data['bid_value'],
            'item_id': item[0].id,
            'user_id': user[0].id,
        }
        if user and item:
            if item[0].is_on_auction and bid_svc.check_bid(
                    item[0], bid['bid_value']):
                data = bid_schema.load(bid)
            else:
                return custom_response({'error': 'invalid bid'}, 401)
        else:
            return custom_response({'error': 'User or Item not found'}, 404)
    except:
        return custom_response({'error': 'Body incorreto'}, 400)
    bid = Bid(data)
    bid.save()

    return custom_response({'bid': bid.to_dict_json()}, 201)