def delete_store(name): find_store = mongodbfind.find_one({'name': name}) if find_store != None: res = mongodbdelete.delete_one({'name': name}) if res == 'success': print('1') return get_stores() else: print('2') return jsonify({'message': 'delete fail'}) else: return jsonify({'message': 'store not found'})
def create_item_in_store(name): request_data = request.get_json() res = mongodbfind.find_one({'name': name}, {'_id':False, 'name':True, 'items':True}) if res != None: items = [] old_item = res['items'] new_item = { 'name': request_data['name'], 'price': request_data['price'] } items = old_item if len(items) == 0: items.append(new_item) else: for i in range(0, len(items)): if request_data['name'] == items[i]['name']: items[i]['price'] = request_data['price'] break else: if i == len(items)-1: items.append(new_item) where = {'name': name} value = {'$set': {'items': items}} mongodbupdate.update_one(where, value) return jsonify({'name': new_item['name'], 'price': new_item['price']}) # for item in old_item: # if request_data['name'] != item['name']: # items.append(item) # items.append(new_item) # else: # items.append(item) # if item['name'] == request_data['name']: # item['price'] = request_data['price'] # where = {'name': name} # value = {'$set': {'items': items}} # mongodbupdate.update_one(where, value) # return jsonify({'name': new_item['name'], 'price': new_item['price']}) else: return jsonify({'message': 'store not found'})
def delete_item_in_store(name): request_data = request.get_json() res = mongodbfind.find_one({'name': name}, {'_id':False, 'name':True, 'items':True}) if res != None: items = [] old_item = res['items'] delete_item = { 'name': request_data['name'] } items = old_item for i in range(0, len(items)): if request_data['name'] == items[i]['name']: del items[i] where = {'name': name} value = {'$set': {'items': items}} mongodbupdate.update_one(where, value) return get_item_in_stoe(name) else: if i == len(items)-1: return jsonify({'message': 'item not found'}) else: return jsonify({'messgae': 'store not found'})
def get_store(name): res = mongodbfind.find_one({'name': name}, {'_id':False, 'name':True, 'items':True}) if res != None: return res else: return jsonify({'message': 'store not found'})