def post(self, name): if ItemModel.get_item(name): return {'message': f"item '{name}' already exists"}, 400 price = ItemResource.parser.parse_args()['price'] item = ItemModel(name=name, price=price) item.add_item() return {'name': name, 'price': price}, 201
def put(self, name): price = ItemResource.parser.parse_args()['price'] if not ItemModel.get_item(name): item = ItemModel(name=name, price=price) item.add_item() return {'name': name, 'price': price}, 201 ItemModel.change_item(name, price) return {'name': name, 'price': price}, 200
def post(self): items = ItemListResource.parser.parse_args()['items'] errors = [] for item in items: if ItemModel.get_item(item['name']): errors.append(item['name']) if errors: return {'message': f'{errors} already exist'}, 400 for item in items: new_item = ItemModel(name=item['name'], price=item['price']) new_item.add_item() return {'items': items}, 201
def post(self): items = ItemListResource.parser.parse_args()['items'] bad_item_names = [] for item in items: existing_item = ItemModel.get_item(item['name']) if existing_item: bad_item_names.append(existing_item.name) if bad_item_names: return {'message': f"items '{bad_item_names}' already exist"}, 400 for item in items: new_item = ItemModel(name=item.name, price=item['price']) new_item.add_item() return {'items': items}, 201
def delete(self, name): item = ItemModel.get_item(name) if not item: return {'message': f"item '{name}' doesn't exist"}, 404 item.delete_item() return {}, 204
def get(self, name): item = ItemModel.get_item(name) if not item: return {'message': f"item '{name}' doesn't exist"}, 404 return {'name': item.name, 'price': item.price}, 200
def delete(self, name): item = ItemModel.get_item(name) if not item: return {'message': 'item doesn\'t exist'}, 404 item.delete_item() return {}, 204
def get(self, name): item = ItemModel.get_item(name) if not item: return {'message': 'item doesn\'t exist'}, 404 return {'name': item.name, 'price': item.price}, 200