예제 #1
0
    def get(self, name):
        item_to_get = ItemModel.get_item_by_name(name)

        if not item_to_get:
            # HTP Code 404 : Not Found
            return {"message": "Item not found."}, 404

        # HTTP Code 200, OK
        return {"items": item_to_get.json()}, 200
예제 #2
0
    def put(self, name):
        item = ItemModel.get_item_by_name(name)
        data = Item.parser.parse_args()
        if item:
            item.price = data['price']
        else:
            item = ItemModel(name, **data)

        item.save_to_db()
        return item.json()
예제 #3
0
 def post(self, name):
     if ItemModel.get_item_by_name(name):
         return {'message': 'this item already exists'}, 400
     data = Item.parser.parse_args()
     item = ItemModel(name, **data)
     try:
         item.save_to_db()
     except:
         return {"message": "An internal error occurred "}, 500
     return item.json(), 201
예제 #4
0
    def post(self, name):
        if ItemModel.get_item_by_name(name):
            # HTTP Code 400 : Bad request.
            return {"message": "item '{}' already exists.".format(name)}, 404

        data = Item.parser.parse_args()
        new_item = ItemModel(name, data["price"], data["store_id"])
        try:
            new_item.save_to_db()
        except:
            # HTTP Code 500: Internal server error
            return {"message": "Error inserting item !"}, 500

        # HTTP Code 201: Item Creation OK
        return new_item.json(), 201
예제 #5
0
    def delete(self, name):

        item_to_delete = ItemModel.get_item_by_name(name)

        if item_to_delete is None:
            # HTTP Code 400 : Bad Request
            return {"message": "Item to delete is not found"}, 400

        try:
            item_to_delete.delete_from_db()
        except:
            # HTTP Code 500: Internal server error
            return {"message": "Error deleting item"}, 500

        # HTTP Code 200 : OK
        return {"message": "Item is deleted successfully"}, 200
예제 #6
0
    def put(self, name):
        data = Item.parser.parse_args()

        item_to_put = ItemModel.get_item_by_name(name)
        if item_to_put:
            try:
                item_to_put.price = data['price']
                item_to_put.store_id = data['store_id']
                item_to_put.save_to_db()
            except:
                # HTTP Code 500: Internal server error
                return {"message": "Error updating item !"}, 500
        else:
            try:
                item_to_put = ItemModel(name, data["price"], data['store_id'])
                item_to_put.save_to_db()
            except:
                # HTTP Code 500: Internal server error
                return {"message": "Error inserting item !"}, 500

        return item_to_put.json()
예제 #7
0
 def delete(self, name):
     item = ItemModel.get_item_by_name(name)
     if item:
         item.delete_item()
     return {"message": "item deleted"}, 200
예제 #8
0
 def get(self, name):
     item = ItemModel.get_item_by_name(name)
     if item:
         return item.json()
     return {'message': 'item not found'}, 404