コード例 #1
0
    def put(self, name):
        data = Item.parser.parse_args()
        new_item = ItemModel(name, data['price'])

        # check whether item exists
        try:
            existing_item = ItemModel.find_by_name(name)
        except:
            return {'message': 'db search failed'}, 500

        # if item exists then update it
        if existing_item:
            try:
                existing_item.price = data['price']
                existing_item.update_item()
            except:
                return {'message': 'item update failed'}, 500
            return {
                "message": "Item '{0}' updated successfully".format(name)
            }, 200

        # if item is not existing then create a new item
        try:
            new_item.insert_item()
        except:
            return {'message': 'item insertion failed'}, 500
        return {'message': "new item '{0}' created".format(name)}, 201
コード例 #2
0
 def post(cls):
     json_data = request.get_json()
     try:                                        #Check input data whether valid or not
         item_data = item_schema.load(json_data)     #It will export json to dict 
     except ValidationError as err:              #invalid data exception handle
         return err.messages, 400
     
     item = ItemModel.find_by_name(item_data['name'])
     if item:
         return {"Message" : "The item with name {} is already exists in table".format(item_data['name'])}, 400
     
     item = ItemModel(**item_data)               #Created ItemModel object
     item.insert_item()
     return {"Message" : "Item inserted successfully"}, 201
コード例 #3
0
    def post(self, name):
        # item trying to add is already present in the items list
        if ItemModel.find_by_name(name):
            return {'message': "item '{0}' already exists".format(name)}, 400

        # access class variable by ClassName.variable_name
        request_data = Item.parser.parse_args()
        item = ItemModel(name, request_data['price'])
        try:
            item.insert_item()
        except:
            return {'message': 'insertion to db failed'}, 500
        # if error then execution will stop at return statement of except block
        # if no error then return the item and success code
        return item.get_json(), 201