public async Task <ActionResult> Update([FromBody] UpdateProduct data, string productSKU) { try { var checkData = MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product"); if (checkData != null) { var objectId = BsonSerializer.Deserialize <Product>(checkData).Id; var filter = Builders <BsonDocument> .Filter.Eq("_id", objectId); if (data.ProductFor != null) { productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU; var objectName = data.ProductFor + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4]; await MH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, data.ProductFor, "ProductFor", objectName); } if (data.ProductType != null) { productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU; var objectName = productSKU.Split('-')[0] + "-" + data.ProductType + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4]; await MH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, data.ProductType, "ProductType", objectName); } if (data.ProductDesign != null) { productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU; var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + data.ProductDesign + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4]; await MH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, data.ProductDesign, "ProductDesign", objectName); } if (data.ProductBrand != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductBrand", data.ProductBrand)); } if (data.ProductPrice > 0) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductPrice", data.ProductPrice)); double discountPercentage; if (data.ProductDiscount > 0) { discountPercentage = data.ProductDiscount; } else { discountPercentage = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product")).ProductDiscount; } var discountPrice = (data.ProductPrice - (data.ProductPrice * (discountPercentage / 100))); var update1 = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscountPrice", discountPrice)); } if (data.ProductDiscount > 0) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscount", data.ProductDiscount)); double price; if (data.ProductPrice > 0) { price = data.ProductPrice; } else { price = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product")).ProductPrice; } var discountPrice = (price - (price * (data.ProductDiscount / 100))); var update1 = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscountPrice", discountPrice)); } if (data.ProductStock > 0) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductStock", data.ProductStock)); } if (data.ProductSize != null) { productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU; var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + data.ProductSize; await MH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, data.ProductSize, "ProductSize", objectName); } if (data.ProductMaterial != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductMaterial", data.ProductMaterial)); } if (data.ProductColour != null) { productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU; var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + data.ProductColour + "-" + productSKU.Split('-')[4]; await MH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, data.ProductColour, "ProductColour", objectName); } if (data.RefundApplicable != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("RefundApplicable", data.RefundApplicable)); } if (data.ReplacementApplicable != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ReplacementApplicable", data.ReplacementApplicable)); } if (data.ProductDescription != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDescription", data.ProductDescription)); } var MinioObject_URl = "https://s3.ap-south-1.amazonaws.com/arthurclive-products/" + data.ProductFor + "-" + data.ProductType + "-" + data.ProductDesign + "-" + data.ProductColour + "-" + data.ProductSize + ".jpg"; var updateURL = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("MinioObject_URL", MinioObject_URl)); return(Ok(new ResponseData { Code = "200", Message = "Updated", Data = null })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Product not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("ProductController", "Update", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }