Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 def post():
     new_items = []
     items = ItemList.parser.parse_args()['items']
     if items:
         for item in items:
             abort_if_item_exist(item['name'])
             store_id = item['store_id']
             if StoreModel.query.filter_by(id=store_id).all():
                 try:
                     price = int(item['price'])
                 except:
                     return abort(404,
                                  message='price parameter must be integer')
             else:
                 abort(404,
                       message="store with id={} doesn't exist".format(
                           store_id))
         for item in items:
             new_item = {
                 'name': item['name'],
                 'price': item['price'],
                 'store_id': item['store_id']
             }
             new_item = ItemModel(**new_item)
             new_item.add_item()
             new_items.append(new_item.json())
         return {'new_items': new_items}, 201
     return {"message": "items list mustn't be empty"}, 404
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
 def post(name):
     abort_if_item_exist(name)
     price = Item.parser.parse_args()['price']
     try:
         price = int(price)
         new_item = {'name': name, 'price': price}
         new_item = ItemModel(**new_item)
         new_item.add_item()
         return {'new_item': new_item.json()}, 201
     except:
         abort(404, message='price parameter must be integer')
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
 def put(name):
     price = Item.parser.parse_args()['price']
     try:
         price = int(price)
         if does_item_exist(name):
             changed_item = ItemModel.find_by_name(name)
             changed_item.price = price
             db.session.commit()
             return {'changed_item': changed_item.json()}, 201
         new_item = {'name': name, 'price': price}
         new_item = ItemModel(**new_item)
         new_item.add_item()
         return {'new_item': new_item.json()}, 201
     except:
         abort(404, message='price parameter must be integer')
Exemplo n.º 8
0
 def post(name):
     abort_if_item_exist(name)
     price = Item.parser.parse_args()['price']
     store_id = Item.parser.parse_args()['store_id']
     if StoreModel.query.filter_by(id=store_id).all():
         try:
             price = int(price)
             new_item = {'name': name, 'price': price, 'store_id': store_id}
             new_item = ItemModel(**new_item)
             new_item.add_item()
             return {'new_item': new_item.json()}, 201
         except:
             abort(404, message='price parameter must be integer')
     else:
         abort(404,
               message="store with id={} doesn't exist".format(store_id))
Exemplo n.º 9
0
 def post():
     new_items = []
     items = ItemList.parser.parse_args()['items']
     if items:
         for item in items:
             abort_if_item_exist(item['name'])
             try:
                 price = int(item['price'])
             except:
                 return abort(404,
                              message='price parameter must be integer')
         for item in items:
             new_item = {'name': item['name'], 'price': item['price']}
             new_item = ItemModel(**new_item)
             new_item.add_item()
             new_items.append(new_item.json())
         return {'new_items': new_items}, 201
     return {"message": "items list mustn't be empty"}, 404
Exemplo n.º 10
0
 def put(name):
     price = Item.parser.parse_args()['price']
     store_id = Item.parser.parse_args()['store_id']
     if StoreModel.query.filter_by(id=store_id).all():
         try:
             price = int(price)
             if does_item_exist(name):
                 changed_item = ItemModel.find_by_name(name)
                 changed_item.price = price
                 db.session.commit()
                 return {'changed_item': changed_item.json()}, 201
             new_item = {'name': name, 'price': price, 'store_id': store_id}
             new_item = ItemModel(**new_item)
             new_item.add_item()
             return {'new_item': new_item.json()}, 201
         except:
             abort(404, message='price parameter must be integer')
     else:
         abort(404,
               message="store with id={} doesn't exist".format(store_id))