Example #1
0
    def put(self, id):
        """
        Edit product data
        """
        parser.replace_argument('name', required=False)
        parser.replace_argument('price', required=False)
        parser.replace_argument('description', required=False)
        data = parser.parse_args()
        item = Product.find_by_id(id)
        if not item:
            return {"messages": "User not found"}, 404

        name = data.get('name', None)
        if name and Product.find_by_name(name):
            return {'message': f'Product {name} already exists'}, 400

        for key, value in data.items():
            if data[key] is not None:
                setattr(item, key, value)

        try:
            item.save()
            return {
                'payload': {
                    'product': item.to_json(),
                }
            }
        except Exception as e:
            return {'message': 'Error while updating the product'}, 500
Example #2
0
    def post(self):
        data = parser.parse_args()
        name = data['name']
        # Checking that user is already exist or not
        if Product.find_by_name(name):
            return {'message': f'Product {name} already exists'}, 400

        # create new user
        item = Product(
            name=name,
            price=data['price'],
            description=data.get('description', ''),
        )

        try:
            # Saving user in DB and Generating Access and Refresh token
            item.save()
            return {
                'payload': {
                    'product': item.to_json(),
                }
            }, 201
        except:
            return {'message': 'Error while saving the product'}, 500