def add_personal_rec(from_user_id, to_user_id, p): product_id = p_ctrl.verify_product_by_name_and_brand(p['product_name'], p['brand_name']) if not product_id: p_ctrl.add_product_to_products(p['product_name'], p['brand_name'], p.get('category','')) #check that rec doesn't already exists if session.query(Personal_Rec).filter(Personal_Rec.from_user_id == from_user_id, Personal_Rec.to_user_id == to_user_id, Personal_Rec.product_id == product_id).count() > 0: return None #add recomendation rec_id = session.add(Personal_Rec(from_user_id, to_user_id, product_id)) session.commit() #response info rec = {'id': rec_id} rec['product'] = p_ctrl.get_product_as_dictionary(product_id) rec['to_user'] = u_ctrl.get_user_as_dictionary(to_user_id) return rec
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