コード例 #1
0
 def get(self):
     user_id = get_jwt_identity()
     print(user_id)
     if user_id:
         return {"items": [item.json for item in ItemModel.find_all()]}, 200
     return {
         "items": [item.name for item in ItemModel.find_all()],
         "message": "Login, to see full response"
     }, 200
コード例 #2
0
 def post(self, name: str):
     if ItemModel.find_by_name(name):
         return {"message": f"Item with name '{name}' already exists."}, 400
     data = self.parser.parse_args()
     item = ItemModel(name, **data)
     try:
         item.save_to_db()
     except:
         return {
             "message": "An error occurred during inserting the item."
         }, 500
     return item.json, 201
コード例 #3
0
ファイル: item.py プロジェクト: vitahoang/learn-flash
    def put(self, name):
        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)

        if item is None:
            new_item = ItemModel(None, name, data['price'], data['store_id'])
            try:
                new_item.save_to_db()
                new_item = ItemModel.find_by_name(name)
            except:
                return {
                    'message': 'An error occurred inserting the item'
                }, 500  # Internal Server Error
            return new_item.json(), 201

        else:
            item.price = data['price']
            item.store_id = data['store_id']
            try:
                item.save_to_db()
                item = ItemModel.find_by_name(name)
            except:
                return {
                    'message': 'An error occurred updating the item'
                }, 500  # Internal Server Error
        return {'message': "Updated successfully", 'item': item.json()}
コード例 #4
0
ファイル: item.py プロジェクト: csrbwilson/rest-api-course
    def delete(self, name):
        item = ItemModel.find_by_name(name)

        if item:
            item.delete_from_db()

        return {'message': 'Item deleted'}
コード例 #5
0
 def delete(self, name):
     claims = get_jwt_claims()
     if not claims['is_admin']:
         return {"message": "Not authorized"}, 403
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
     return {"message": "Item deleted."}
コード例 #6
0
ファイル: item.py プロジェクト: vitahoang/learn-flash
 def delete(self, name):
     item = ItemModel.find_by_name(name)
     if item is None:
         return {'Message': "The item '{}' is not exist".format(name)}, 404
     try:
         item.delete_from_db()
     except:
         return {
             'message': 'An error occurred deleting the item'
         }, 500  # Internal Server Error
     return {'message': "The item '{}' has been deleted".format(name)}
コード例 #7
0
ファイル: item.py プロジェクト: tekrajchhetri/restapi
    def get(self, name):
        try:
            item = ItemModel.find_by_name(name)

        except:
            return {"message": "Database find error"}, 500  #server error

        if item:
            return item.json()

        return {"message": "Item not found"}
コード例 #8
0
ファイル: item.py プロジェクト: csrbwilson/rest-api-course
    def put(self, name):

        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, **data)
        else:
            item.price = data['price']
            item.store_id = data['store_id']

        item.save_to_db()
        return item.json(), 201
コード例 #9
0
ファイル: item.py プロジェクト: tekrajchhetri/restapi
    def put(self, name):
        requested_data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, **requested_data)
        else:
            item.price = requested_data["price"]

        item.save_to_db()

        return item.json()
コード例 #10
0
 def put(self, name):
     data = self.parser.parse_args()
     item = ItemModel.find_by_name(name)
     if item is None:
         item = ItemModel(**data)
     else:
         item.price = data["price"]
     item.save_to_db()
     return item.json, 200
コード例 #11
0
ファイル: item.py プロジェクト: vitahoang/learn-flash
 def post(self, name):
     if ItemModel.find_by_name(name):
         return {
             'Message': "The item '{}' is already exist".format(name)
         }, 400
     data = Item.parser.parse_args()
     new_item = ItemModel(None, name, data['price'], data['store_id'])
     try:
         new_item.save_to_db()
         new_item = ItemModel.find_by_name(name)
     except:
         return {
             'message': 'An error occurred inserting the item'
         }, 500  # Internal Server Error
     return new_item.json(), 201
コード例 #12
0
    def delete(self, name):
        # connection = sqlite3.connect("data.db")
        # cursor = connection.cursor()
        # delete_query = "DELETE FROM items WHERE name=?"
        # cursor.execute(delete_query, (name,))
        # connection.commit()
        # connection.close()

        # JWT extension
        claims = get_jwt_claims()
        if not claims[0]['is_admin']:
            return {'message': 'This user is not an admin'}
        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': f'Item {name} deleted'}
コード例 #13
0
    def put(name):
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item:
            item.price = data['price']
        else:
            item = ItemModel(name, **data)

        item.save_to_db()

        return item.json()
コード例 #14
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return (
                {"message": "An item with name '{}' already exists.".format(name)},
                400,
            )

        data = Item.parser.parse_args()
        item = ItemModel(name, data["price"], data["store_id"])
        # try:
        item.save_to_db()
        # except:
        #    return {"message": "An error occured"}, 500
        return item.json(), 201
コード例 #15
0
    def post(cls, name):
        if ItemModel.find_by_name(name):
            return {'message': f"an item with name {name} already exist"}, 400

        # parsing arguments
        data = cls.parser.parse_args()
        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {'message': 'Error while parsing via server'}, 500

        return item.json(), 201
コード例 #16
0
    def put(name):
        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)
        if item:
            # when exists, replace
            # new_item.update()
            item.price = data['price']
        else:
            # when doesn't exist, create a new one
            # new_item.insert()
            item = ItemModel(name, **data)

        item.save_to_db()

        return item.json()
コード例 #17
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message': "An item with name '{}' already exists".format(name)
            }, 400

        data = Item.parser.parse_args()
        item = ItemModel(name, data['price'], data['store_id'])
        try:
            item.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the item"
            }, 500  # internal server error

        return item.json(), 201
コード例 #18
0
ファイル: item.py プロジェクト: csrbwilson/rest-api-course
    def post(self, name):

        if ItemModel.find_by_name(name):
            return {
                'message': "An item with name '{}' already exists".format(name)
            }, 400  # 400 => BAD REQUEST

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {'message': 'An error ocurred inserting item.'}, 500

        return item.json(), 201  # 201 => CREATED
コード例 #19
0
    def post(name):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }, 400

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except ConnectionError:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201
コード例 #20
0
ファイル: item.py プロジェクト: tekrajchhetri/restapi
    def post(self, name):

        if ItemModel.find_by_name(name):
            return {
                "message": "An item with name {} already exists.".format(name)
            }, 400  #400 - bad request

        requested_data = Item.parser.parse_args()
        item = ItemModel(
            name, **requested_data
        )  # **requested_data === requested_data["price"], requested_data["store_id"]
        try:
            item.save_to_db()
        except:
            return {
                "message": "Error occured inserting the item"
            }, 500  #internal server error
        return item.json(), 201  #201 -- created
コード例 #21
0
 def delete(name):
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
         return {'message': 'Item deleted.'}
     return {'message': 'Item not found.'}, 404
コード例 #22
0
 def get(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     else:
         return {"message": "Item not found"}, 404
コード例 #23
0
 def get():
     return {'items': [x.json() for x in ItemModel.find_all()]}
コード例 #24
0
ファイル: item.py プロジェクト: tekrajchhetri/restapi
    def delete(self, name):
        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {"message": "Item Deleted"}
コード例 #25
0
ファイル: item.py プロジェクト: vitahoang/learn-flash
 def get(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     return {'message': 'Item not found'}, 404