Пример #1
0
def api_post_contacts():
    '''
    Create a new contact.
    '''

    request_data = request.get_json()

    new_contact = Contact(
        name=get_or_400(request_data, 'name'),
        email=get_or_400(request_data, 'email'),
    )
    save_contact(new_contact)

    return jsonify(added=True, id=new_contact.id)
Пример #2
0
def api_put_contact(contact_id) -> Response:
    '''
    Update a single contact.
    '''

    request_data = request.get_json()

    contact = Contact.query.get_or_404(contact_id)
    contact.name = get_or_400(request_data, 'name')
    contact.email = get_or_400(request_data, 'email')

    try:
        save_contact(contact)
    except IntegrityError:
        abort(400, 'This contact already exists')

    return jsonify(updated=True)
Пример #3
0
def api_post_contacts() -> Response:
    '''
    Create a new contact.
    '''

    request_data = request.get_json()

    new_contact = Contact(
        name=get_or_400(request_data, 'name'),
        email=get_or_400(request_data, 'email'),
    )

    try:
        save_contact(new_contact)
    except IntegrityError:
        abort(400, 'This contact already exists')

    return jsonify(added=True, id=new_contact.id)
Пример #4
0
def api_put_contact(contact_id):
    '''
    Update a single contact.
    '''

    request_data = request.get_json()

    contact = Contact.query.get_or_404(contact_id)
    contact.name = get_or_400(request_data, 'name')
    contact.email = get_or_400(request_data, 'email')

    try:
        save_contact(contact)
    # If a duplicate of the updated contact exists, just remove this one
    except IntegrityError:
        db.session.rollback()
        delete_contact(contact)

    return jsonify(updated=True)