def createNewProduct(): user = User.query.get(request.data['id']) if not user.is_admin: return jsonify({'message': 'only a admin can access this route'}) data = request.json category = Category.query.filter_by(name=data['category_name']).first() if not category: return jsonify({'message': 'Please enter a valid category'}) newProduct = Product(product_name=data['product_name'], description=data['description'], brand=data['brand'], discount=data['discount'], regular_price=data['regular_price'], category_id=category.id, image=data['image']) db.session.add(newProduct) db.session.commit() if (data['details']): print('gonna start entering the product details') for detail in data['details']: prod_details = ProductDetails(product_id=newProduct.id, size=detail['size'], quantity=detail['quantity']) db.session.add(prod_details) db.session.commit() return jsonify(product_schema.dump(newProduct))
def create_product(): name = request.json['name'] description = request.json['description'] price = request.json['price'] qty = request.json['qty'] new_product = Product(name, description, price, qty) db.session.add(new_product) db.session.commit() return product_schema.dump(new_product)
def getProduct(category_id, product_id): category = Category.query.get(category_id) if not category: return jsonify({'message': 'no such category'}) category_result = category_schema.dump(category) product = Product.query.get(product_id) if not product: return jsonify({'message': 'no such product with this product id'}) product_result = product_schema.dump(product) return jsonify({'category': category_result, 'product': product_result})
def update_product(id): product = Product.query.get(id) new_name = request.json['name'] new_description = request.json['description'] new_price = request.json['price'] new_qty = request.json['qty'] product.name = new_name product.description = new_description product.price = new_price product.qty = new_qty db.session.commit() return product_schema.dump(product)
def delete_product(id): product = Product.query.get(id) db.session.delete(product) db.session.commit() return product_schema.dump(product)
def fetch_product(id): product = Product.query.get(id) return product_schema.dump(product)