Esempio n. 1
0
def create_contact():
    full_name = request.json.get('full_name')
    email = request.json.get('email')
    agenda_slug = request.json.get('agenda_slug')
    address = request.json.get('address')
    phone = request.json.get('phone')

    contact = Contact.query.filter_by(agenda_slug=agenda_slug).first()
    if contact:
        return jsonify({"msg": "agenda already exists"})
    else:
        if not full_name:
            return jsonify({"msg": "Full name is required"}), 400
        if not email:
            return jsonify({"msg": "Email required"}), 400
        if not agenda_slug:
            return jsonify({"msg": "Agenda slug is required"}), 400
        if not address:
            return jsonify({"msg": "Address is required"}), 400
        if not phone:
            return jsonify({"msg": "Phone is required"}), 400

        contact = Contact()
        contact.full_name = full_name
        contact.email = email
        contact.agenda_slug = agenda_slug
        contact.address = address
        contact.phone = phone
        contact.save()

        return jsonify(contact.serialize()), 201
Esempio n. 2
0
def create_contact():
    full_name = request.json.get("full_name", None)
    email = request.json.get("email", None)
    agenda_slug = request.json.get("agenda_slug", None)
    address = request.json.get("address", "")
    phone = request.json.get("phone", "")

    if not full_name:
        return jsonify({"msg": "full_name is required"}), 400

    contact = Contact()
    contact.full_name = full_name
    contact.email = email
    contact.agenda_slug = agenda_slug
    contact.address = address
    contact.phone = phone

    contact.save()

    return jsonify(contact.serialize()), 201
Esempio n. 3
0
def contacts(id=None):
    if request.method == 'GET':
        if id is not None:
            contact = Contact.query.get(id)
            if contact:
                return jsonify(contact.serialize()), 200
            else:
                return jsonify({"msg": "Contact doesn't exist"}), 404
        else:
            contacts = Contact.query.all()
            contacts = list(map(lambda contact: contact.serialize(), contacts))
            return jsonify(contacts), 200

    if request.method == 'POST':
        name = request.json.get('name', None)
        email = request.json.get('email', None)
        phone = request.json.get('phone', "")
        address = request.json.get('address', "")

        if not name:
            return jsonify({"msg": "name is required"}), 400
        if not email:
            return jsonify({"msg": "email is required"}), 400

        contact = Contact()
        contact.name = name
        contact.email = email
        contact.phone = phone
        contact.address = address

        contact.save()

        return jsonify(contact.serialize()), 201

    if request.method == 'PUT':
        name = request.json.get('name', None)
        email = request.json.get('email', None)
        phone = request.json.get('phone', "")
        address = request.json.get('address', "")

        if not name:
            return jsonify({"msg": "name is required"}), 400
        if not email:
            return jsonify({"msg": "email is required"}), 400

        # SELECT * FROM contacts WHERE id = 1
        contact = Contact.query.get(id)

        if not contact:
            return jsonify({"msg": "Contact doesn't exist"}), 404

        contact.name = name
        contact.email = email
        contact.phone = phone
        contact.address = address

        # UPDATE contacts SET name=name, email=email ... WHERE id = 1
        contact.update()

        return jsonify(contact.serialize()), 200

    if request.method == 'DELETE':
        contact = Contact.query.get(id)
        if not contact:
            return jsonify({"msg": "Contact doesn't exist"}), 404
        
        # DELETE FROM contacts WHERE id = 1
        contact.delete()
        return jsonify({"succes", "Contact deleted"}), 200