def put(self, _id): request_data = Item.parse_item_data() # Verify the brand is an already registered one. brand = BrandModel.find_brand_by_id(request_data['brand_id']) if not brand: return { 'message': "Brand id '{}' does not exist; put failed".format( request_data['brand_id']) }, 400 item = ItemModel.find_item_by_id(_id) # If item already exists, we need to change its values. If not, we need to send in everything to # create a new one. if item: item.name = request_data['name'] item.price = request_data['price'] item.color = request_data['color'] item.brand_id = brand.id else: item = ItemModel(None, request_data['name'], request_data['price'], request_data['color'], brand.id) try: item.add_or_update_item() except: return { 'message': "Add or update item failed for brand '{}' item '{}'".format( brand.name, item.name) }, 500 return item.json()
def post(self, _id): request_data = Item.parse_item_data() brand = BrandModel.find_brand_by_id(request_data['brand_id']) if not brand: return { 'message': "Brand id '{}' does not exist; post failed".format( request_data['brand_id']) }, 400 item = ItemModel.find_item_by_name(brand.id, request_data['name']) if item: return { 'message': "Item '{}' of Brand id '{}' already exists; post failed". format(item.name, item.brand_id) }, 400 item = ItemModel(None, request_data['name'], request_data['price'], request_data['color'], brand.id) try: item.add_or_update_item() except: return { 'message': "Add item failed for brand '{}' item '{}'".format( brand.name, item.name) }, 500 return item.json(), 201