Ejemplo n.º 1
0
def userSites(user_id):

    if request.method == 'GET':
        response = jsonify(s_ctrl.get_sites_by_user_id(user_id))
        return response, 200

    if request.method == 'POST':
        body = request.get_json()
        url = body["url"]
        site_info = s_ctrl.get_site_info(url)
        site_id = s_ctrl.add_or_update_site(site_info)
        response = s_ctrl.add_user_to_site(user_id, site_id, site_info['site_type'], body.get("comment"))
        if response:
            e_ctrl.add_event(user_id, "added a " +site_info['site_type'], site_info['site_type'], site_id)
            return jsonify(response), 201
        else:
            return 'Site has already been added', 302

    if request.method == 'PUT':
        body = request.get_json()
        user_site_id = body['user_site_id']
        response = s_ctrl.edit_user_to_site(user_site_id, body.get("comment"))
        if not response:
            return "Site not found", 404
        return jsonify(response), 201

    if request.method == 'DELETE':
        body = request.get_json()
        removed = s_ctrl.remove_user_from_site(body['user_site_id'])
        if removed:
            return "Site "+ str(removed) +" Removed", 204
        else:
            return "Site not found", 404
Ejemplo n.º 2
0
def userProducts(user_id):

    if request.method == 'GET':
        #lookup all products for in users collection
        response = jsonify(userProducts=p_ctrl.get_products_by_user_id(user_id))
        #respond array of products and a 200
        return response, 200
    
    if request.method == 'POST':
        body = request.get_json()
        #check if product is already in DB
        product_id = p_ctrl.verify_product_by_name_and_brand(body['product_name'], body['brand_name'])
        if product_id == None:    
            #Add product if not
            product_id = p_ctrl.add_product_to_products(body['product_name'], body['brand_name'], body['product_category'])
        #create db relationship between user and product
        response = jsonify(p_ctrl.add_user_to_product(
            user_id, 
            product_id, 
            body.get('product_size'),
            body.get('product_status'),
            body.get('product_notes'),
            body.get('product_color'),
            body.get('stars'),
            body.get('review'),
            body.get('product_image_url'),
            body.get('product_category')
        ))
        e_ctrl.add_event(user_id, "added a product", "product", product_id)
        return response, 201

    if request.method == 'PUT':
        body = request.get_json()
        p = body['product']
        product_id = p_ctrl.verify_product_by_name_and_brand(p['product_name'], p['brand_name'])
        response = jsonify(p_ctrl.edit_user_to_product(
            user_id,
            product_id, 
            p.get('product_size'),
            p.get('product_status'),
            p.get('product_notes'),
            p.get('product_color'),
            p.get('stars'),
            p.get('review'),
            p.get('product_image_url'),
            p.get('product_category')
        ))
        return response, 202

    if request.method == 'DELETE':
        body = request.get_json()
        p_ctrl.remove_user_from_product(body['product_id'])
        return "Product Removed", 204