예제 #1
0
 def patch(self, id):
     product = Product.query.get_or_404(id)
     product_dict = request.get_json(force=True)
     print(product_dict)
     if 'name' in product_dict and product_dict['name'] is not None:
         product_name = product_dict['name']
         if not Product.is_name_unique(id=0, name=product_name):
             response = {
                 'error':
                 'A product with this name {} already exists'.format(
                     product_name)
             }
             return response, HttpStatus.bad_request_400.value
         product.name = product_name
     if 'price' in product_dict and product_dict['price'] is not None:
         product.price = product_dict['price']
     if 'description' in product_dict and product_dict[
             'description'] is not None:
         product.description = product_dict['description']
     if 'tags' in product_dict and product_dict['tags'] is not None:
         product.tags = product_dict['tags']
     try:
         product.update()
         return self.get(id)
     except SQLAlchemyError as e:
         db.session.rollback()
         response = {"error": str(e)}
         return response, HttpStatus.bad_request_400.value
예제 #2
0
 def post(self):
     product_collection = request.get_json()
     if not product_collection:
         response = {'message': 'No input data provorder_ided'}
         return response, HttpStatus.bad_request_400.value
     #errors = product_schema.validate(product_collection)
     #if errors:
     #return errors, HttpStatus.bad_request_400.value
     product_name = product_collection['name']
     if not Product.is_name_unique(id=0, name=product_name):
         response = {'error': 'Product already exist'.format(product_name)}
         return response, HttpStatus.bad_request_400.value
     try:
         product_category_name = product_collection['product_category']
         category = ProductCategory.query.filter_by(
             name=product_category_name).first()
         if category is None:
             category = ProductCategory(name=product_category_name)
             db.session.add(category)
         product = Product(name=product_collection['name'],
                           price=product_collection['price'],
                           description=product_collection['description'],
                           product_category=category,
                           tags=product_collection['tags'])
         product.add(product)
         query = Product.query.get(product.id)
         dump_result = product_schema.dump(query)
         return dump_result, HttpStatus.created_201.value
     except SQLAlchemyError as e:
         db.session.rollback()
         response = {"error": str(e)}
         return response, HttpStatus.bad_request_400.value