/// <summary>Remove product in a particular cart</summary> /// <param name="orderId"></param> /// <param name="userName"></param> /// <param name="email"></param> public async static Task <string> RemoveCartItems(long orderId, string userName, string email) { try { IAsyncCursor <Cart> cartCursor = await _client.GetDatabase("UserInfo").GetCollection <Cart>("Cart").FindAsync(Builders <Cart> .Filter.Eq("UserName", userName)); var cartDatas = cartCursor.ToList(); foreach (var cart in cartDatas) { foreach (var product in GetProducts(cart.ProductSKU, _client.GetDatabase("ProductDB")).Result) { long updateQuantity = product.ProductStock - cart.ProductQuantity; if (product.ProductStock - cart.ProductQuantity < 0) { updateQuantity = 0; var emailResponce = EmailHelper.SendEmailToAdmin(userName.ToString(), email.ToString(), cart.ProductSKU, cart.ProductQuantity, product.ProductStock, orderId).Result; } var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", cart.ProductSKU), "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductStock", updateQuantity)).Result; } } var removeCartItems = _client.GetDatabase("UserInfo").GetCollection <Cart>("Cart").DeleteMany(Builders <Cart> .Filter.Eq("UserName", userName)); return("Success"); } catch (Exception ex) { LoggerDataAccess.CreateLog("MongoHelper", "RemoveCartItems", ex.Message); return(null); } }
/// <summary></summary> /// <param name="objectId"></param> /// <param name="updateData"></param> /// <param name="updateField"></param> /// <param name="objectName"></param> public async static Task <bool?> UpdateProductDetails(dynamic objectId, dynamic updateData, string updateField, string objectName) { try { var update = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Product", Builders <BsonDocument> .Update.Set(updateField, updateData)); string MinioObject_URL; //MinioObject_URL = WH.GetMinioObject("arthurclive-products", objectName).Result; //MinioObject_URL = AH.GetAmazonS3Object("arthurclive-products", objectName); MinioObject_URL = AH.GetS3Object("arthurclive-products", objectName); var update1 = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductSKU", objectName)); var update2 = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Product", Builders <BsonDocument> .Update.Set("MinioObject_URL", MinioObject_URL)); if (update1 == true & update2 == true) { return(true); } else { return(false); } } catch (Exception ex) { LoggerDataAccess.CreateLog("MongoHelper", "UpdateProductDetails", ex.Message); return(null); } }
/// <summary>Update order details</summary> /// <param name="orderId"></param> public async static Task <string> UpdatePaymentDetails(long orderId) { try { PaymentMethod paymentDetails = new PaymentMethod(); List <StatusCode> statusCodeList = new List <StatusCode>(); var orderData = BsonSerializer.Deserialize <OrderInfo>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", orderId), "OrderDB", "OrderInfo").Result); foreach (var detail in orderData.PaymentDetails.Status) { statusCodeList.Add(detail); } statusCodeList.Add(new StatusCode { StatusId = 2, Description = "Payment Received", Date = DateTime.UtcNow }); paymentDetails.Status = statusCodeList; var updatePaymentDetails = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", orderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("PaymentDetails", paymentDetails)); return("Success"); } catch (Exception ex) { LoggerDataAccess.CreateLog("MongoHelper", "UpdatePaymentDetails", ex.Message); return(null); } }
public ActionResult UpdateCoupon([FromBody] UpdateCoupon data, string code) { try { var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon"); if (checkData != null) { var result = BsonSerializer.Deserialize <Coupon>(checkData); var filter = Builders <BsonDocument> .Filter.Eq("Code", code); if (data.ApplicableFor != null) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("ApplicableFor", data.ApplicableFor)); } if (data.ExpiryTime != null) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("ExpiryTime", data.ExpiryTime)); } if (data.Value > 0) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Value", data.Value)); } if (data.Percentage != null) { var updateResult = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Percentage", data.Percentage)); } if (data.UsageCount > 0) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", data.UsageCount)); } return(Ok(new ResponseData { Code = "200", Message = "Coupon updated successfully", Data = null })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Coupon not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("CouponController", "UpdateCoupon", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> UpdateCategory([FromBody] Category data, string productFor, string productType) { try { var checkData = MH.CheckForDatas("ProductFor", productFor, "ProductType", productType, "ProductDB", "Category"); if (checkData != null) { var objectId = BsonSerializer.Deserialize <Category>(checkData).Id; var filter = Builders <BsonDocument> .Filter.Eq("_id", objectId); if (data.ProductFor != null) { var value = BsonSerializer.Deserialize <Category>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Category")).ProductType; var objectName = data.ProductFor + "-" + value; GH.UpdateCategoryDetails(BsonSerializer.Deserialize <Category>(checkData).Id, productFor, productType, data.ProductFor, "ProductFor", objectName); } if (data.ProductType != null) { var value = BsonSerializer.Deserialize <Category>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Category")).ProductFor; var objectName = value + "-" + data.ProductType; GH.UpdateCategoryDetails(BsonSerializer.Deserialize <Category>(checkData).Id, productFor, productType, data.ProductType, "ProductType", objectName); } if (data.Description != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Category", Builders <BsonDocument> .Update.Set("Description", data.Description)); } return(Ok(new ResponseData { Code = "200", Message = "Updated", Data = null })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Category not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("CategoryController", "UpdateCategory", "UpdateCategory", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
/// <summary></summary> /// <param name="objectId"></param> /// <param name="productFor"></param> /// <param name="productType"></param> /// <param name="updateData"></param> /// <param name="updateField"></param> /// <param name="objectName"></param> public async static void UpdateCategoryDetails(dynamic objectId, string productFor, string productType, dynamic updateData, string updateField, string objectName) { try { var update = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Category", Builders <BsonDocument> .Update.Set(updateField, updateData)); string MinioObject_URL; //MinioObject_URL = WH.GetMinioObject("products", objectName).Result; //MinioObject_URL = AH.GetAmazonS3Object("product-category", objectName); MinioObject_URL = AH.GetS3Object("product-category", objectName); var update1 = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Category", Builders <BsonDocument> .Update.Set("MinioObject_URL", MinioObject_URL)); } catch (Exception ex) { LoggerDataAccess.CreateLog("GlobalHelper", "UpdateCategoryDetails", "UpdateCategoryDetails", ex.Message); } }
public async Task <ActionResult> UpdateProduct([FromBody] Product 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]; GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, 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]; GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, 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]; GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, 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; GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, 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.ProductRating > 0) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductRating", data.ProductRating)); } if (data.ProductReviews != null) { var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", data.ProductReviews)); } 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]; GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, 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)); } 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", "UpdateProduct", "UpdateProduct", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> UpdateReview([FromBody] UpdateReview 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); var reviews = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews; List <Review> reviewList = new List <Review>(); Review[] reviewArray = new Review[reviews.Length]; int i = 0; if (reviews.Length == 0) { return(BadRequest(new ResponseData { Code = "401", Message = "No reviews found" })); } else { foreach (var review in reviews) { if (review.Id == data.Id) { review.Approved = data.Approved; } reviewArray[i] = review; i++; reviewList.Add(review); } } var result = reviewList.FindAll(x => x.Id == data.Id); if (result.Count == 0) { return(BadRequest(new ResponseData { Code = "402", Message = "No reviews with id '" + data.Id + "' is found" })); } var updateReview = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", reviewArray)); var updatedReviews = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews; double numberOfReviews = 0; double totalRating = 0; foreach (var review in updatedReviews) { if (review.Approved == true) { numberOfReviews += 1; totalRating += review.Rating; } } double overallRating; if (totalRating > 0) { overallRating = totalRating / numberOfReviews; } else { overallRating = 0; } var updateRating = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductRating", overallRating)); return(Ok(new ResponseData { Code = "200", Message = "Updated" })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Product not found" })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("ProductController", "InsertReview", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> InsertReview([FromBody] Review data, string productSKU) { try { var checkData = MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product"); if (checkData != null) { var objectId = BsonSerializer.Deserialize <Product>(checkData).Id; if (data != null) { var reviews = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews; Review[] reviewArray = new Review[reviews.Length + 1]; data.Id = reviews.Length + 1; data.Approved = false; int i = 0; if (reviews.Length != 0) { foreach (var review in reviews) { reviewArray[i] = review; i++; } } reviewArray[i] = data; var updateReview = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", reviewArray)); var checkOrder = MH.CheckForDatas("OrderId", data.OrderId, null, null, "OrderDB", "OrderInfo"); if (checkOrder == null) { return(BadRequest(new ResponseData { Code = "402", Message = "No orders with id '" + data.OrderId + "' is found", Data = null })); } var orderInfo = BsonSerializer.Deserialize <OrderInfo>(checkOrder); List <ProductDetails> productDetails = new List <ProductDetails>(); foreach (var product in orderInfo.ProductDetails) { if (product.ProductSKU == productSKU) { product.Reviewed = true; } productDetails.Add(product); } var updateOrderInfo = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("ProductDetails", productDetails)); return(Ok(new ResponseData { Code = "200", Message = "Inserted", Data = null })); } else { return(BadRequest(new ResponseData { Code = "401", Message = "Review data from body is empty", Data = null })); } } else { return(BadRequest(new ResponseData { Code = "404", Message = "Product not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("ProductController", "InsertReview", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> UpdateOrder([FromBody] StatusUpdate data, string username, string productSKU) { try { if (data.Status == null) { return(BadRequest(new ResponseData { Code = "401", Message = "Status cannot be empty", Data = null })); } IAsyncCursor <OrderInfo> orderCursor = await order_db.GetCollection <OrderInfo>("OrderInfo").FindAsync(Builders <OrderInfo> .Filter.Eq("UserName", username) & Builders <OrderInfo> .Filter.Eq("OrderId", data.OrderId)); var order = orderCursor.FirstOrDefaultAsync().Result; if (order == null) { return(BadRequest(new ResponseData { Code = "404", Message = "Order Not Found", Data = null })); } PaymentMethod paymentMethod = new PaymentMethod(); List <StatusCode> statusList = new List <StatusCode>(); List <ProductDetails> productList = new List <ProductDetails>(); if (data.Status == "Delivered") { paymentMethod.Method = data.Status; int i = 1; foreach (var status in order.PaymentDetails.Status) { statusList.Add(status); i++; } if (order.PaymentDetails.Method == "Cash On Delivery") { statusList.Add(new StatusCode { StatusId = i, Date = DateTime.UtcNow, Description = "Payment Received" }); } paymentMethod.Status = statusList; foreach (var product in order.ProductDetails) { if (product.ProductSKU == productSKU) { ProductDetails details = new ProductDetails(); details.ProductSKU = productSKU; details.Status = order.PaymentDetails.Method; List <StatusCode> productStatusList = new List <StatusCode>(); int j = 1; foreach (var status in product.StatusCode) { productStatusList.Add(status); j++; } StatusCode productStatus = new StatusCode { StatusId = j, Date = DateTime.UtcNow, Description = data.Status }; productStatusList.Add(productStatus); details.StatusCode = productStatusList; details.ProductInCart = product.ProductInCart; productList.Add(details); } else { productList.Add(product); } } } else if (data.Status == "Refunded" || data.Status == "Replaced" || data.Status == "Refund Failed" || data.Status == "Replacement Failed" || data.Status == "Refund Cancle" || data.Status == "Replacement Cancled") { paymentMethod.Method = data.Status; int i = 1; foreach (var status in order.PaymentDetails.Status) { statusList.Add(status); i++; } if (order.PaymentDetails.Method == "Cash On Delivery") { if (data.Status == "Refunded") { statusList.Add(new StatusCode { StatusId = i, Date = DateTime.UtcNow, Description = "Payment Refund Initiated" }); } } paymentMethod.Status = statusList; foreach (var product in order.ProductDetails) { if (product.ProductSKU == productSKU) { ProductDetails details = new ProductDetails(); details.ProductSKU = productSKU; details.Status = order.PaymentDetails.Method; List <StatusCode> productStatusList = new List <StatusCode>(); int j = 1; foreach (var status in product.StatusCode) { productStatusList.Add(status); j++; } StatusCode productStatus = new StatusCode { StatusId = j, Date = DateTime.UtcNow, Description = data.Status }; productStatusList.Add(productStatus); details.StatusCode = productStatusList; details.ProductInCart = product.ProductInCart; productList.Add(details); } else { productList.Add(product); } } } var paymentUpdate = Builders <BsonDocument> .Update.Set("PaymentDetails", paymentMethod); var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", paymentUpdate).Result; var productUpdate = Builders <BsonDocument> .Update.Set("ProductDetails", productList); var responce = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", productUpdate).Result; return(Ok(new ResponseData { Code = "200", Message = "Success", Data = null })); } catch (Exception ex) { LoggerDataAccess.CreateLog("OrderController", "UpdateOrder", "UpdateOrder", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> ReturnProduct([FromBody] OrderInfo data, string request, string username, string productSKU) { try { if (request == "refund" || request == "replace") { var orders = GH.GetOrders(username, order_db).Result; if (orders == null) { return(BadRequest(new ResponseData { Code = "404", Message = "No orders found", Data = null })); } else { IAsyncCursor <OrderInfo> orderCursor = await order_db.GetCollection <OrderInfo>("OrderInfo").FindAsync(Builders <OrderInfo> .Filter.Eq("UserName", username) & Builders <OrderInfo> .Filter.Eq("OrderId", data.OrderId)); var order = orderCursor.FirstOrDefaultAsync().Result; if (order == null) { return(BadRequest(new ResponseData { Code = "404", Message = "Order Not Found", Data = null })); } foreach (var productDetails in order.ProductDetails) { if (productDetails.ProductSKU == productSKU) { var productData = BsonSerializer.Deserialize <Product>(MH.GetSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", productSKU), "ProductDB", "Product").Result); if (productDetails.Status != "Delivered") { return(BadRequest(new ResponseData { Code = "401", Message = "Order Return Request Failed", Data = null })); } else { if (request == "refund") { if (productData.RefundApplicable == false) { return(BadRequest(new ResponseData { Code = "402", Message = "Refund not applicable for this product", Data = null })); } } if (request == "replace") { if (productData.ReplacementApplicable == false) { return(BadRequest(new ResponseData { Code = "403", Message = "Replacement not applicable for this product", Data = null })); } } PaymentMethod paymentMethod = new PaymentMethod(); paymentMethod.Method = order.PaymentMethod; List <StatusCode> paymentList = new List <StatusCode>(); int i = 1; foreach (var status in order.PaymentDetails.Status) { paymentList.Add(status); i++; } StatusCode paymentStatus = new StatusCode(); paymentStatus.StatusId = i; paymentStatus.Date = DateTime.UtcNow; if (request == "refund") { paymentStatus.Description = "Refund Initiated"; } if (request == "replace") { paymentStatus.Description = "Replacement Initiated"; } paymentList.Add(paymentStatus); paymentMethod.Status = paymentList; var paymentUpdate = Builders <BsonDocument> .Update.Set("PaymentDetails", paymentMethod); var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", paymentUpdate).Result; List <ProductDetails> productDetailsList = new List <ProductDetails>(); foreach (var product in order.ProductDetails) { if (product.ProductSKU == productSKU) { ProductDetails details = new ProductDetails(); details.ProductSKU = productSKU; details.Status = "Refund Initiated"; List <StatusCode> productStatusList = new List <StatusCode>(); int j = 1; foreach (var status in product.StatusCode) { productStatusList.Add(status); j++; } StatusCode productStatus = new StatusCode(); productStatus.StatusId = j; productStatus.Date = DateTime.UtcNow; if (request == "refund") { productStatus.Description = "Refund Initiated"; } if (request == "replace") { productStatus.Description = "Replacement Initiated"; } productStatusList.Add(productStatus); details.StatusCode = productStatusList; details.ProductInCart = product.ProductInCart; productDetailsList.Add(details); } else { productDetailsList.Add(product); } } var productUpdate = Builders <BsonDocument> .Update.Set("ProductDetails", productDetailsList); var responce = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", productUpdate).Result; } } } return(Ok(new ResponseData { Code = "200", Message = "Success", Data = null })); } } else { return(BadRequest(new ResponseData { Code = "405", Message = "Invalid Request", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("OrderController", "ReturnProduct", "ReturnProduct", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> PlaceOrder([FromBody] OrderInfo data, string username) { try { if (data.PaymentMethod != null) { IAsyncCursor <Address> userCursor = await _db.GetCollection <Address>("UserInfo").FindAsync(Builders <Address> .Filter.Eq("UserName", username)); var users = userCursor.ToList(); if (users.Count > 0) { IAsyncCursor <Cart> cartCursor = await _db.GetCollection <Cart>("Cart").FindAsync(Builders <Cart> .Filter.Eq("UserName", username)); var cartDatas = cartCursor.ToList(); if (cartDatas.Count > 0) { var orders = await GH.GetOrders(username, order_db); if (orders == null) { data.OrderId = 1; } else { data.OrderId = orders.Count + 1; } data.UserName = username; double totalPrice = 0; foreach (var product in cartDatas) { totalPrice = totalPrice + product.ProductPrice; } data.TotalAmount = totalPrice; PaymentMethod paymentMethod = new PaymentMethod(); paymentMethod.Method = data.PaymentMethod; List <StatusCode> paymentStatus = new List <StatusCode>(); if (data.PaymentMethod == "Cash On Delivery") { paymentStatus.Add(new StatusCode { StatusId = 1, Date = DateTime.UtcNow, Description = "Payment Pending" }); } else { paymentStatus.Add(new StatusCode { StatusId = 1, Date = DateTime.UtcNow, Description = "Payment Service Initiated" }); } paymentMethod.Status = paymentStatus; data.PaymentDetails = paymentMethod; List <Address> addressList = new List <Address>(); foreach (var address in users) { if (address.DefaultAddress == true) { addressList.Add(address); } } data.Address = addressList; if (data.Address.Count == 0) { return(BadRequest(new ResponseData { Code = "405", Message = "No default address found", Data = null })); } List <ProductDetails> productList = new List <ProductDetails>(); foreach (var cart in cartDatas) { foreach (var product in GH.GetProducts(cart.ProductSKU, product_db).Result) { if (product.ProductStock < cart.ProductQuantity) { return(BadRequest(new ResponseData { Code = "403", Message = "Order quantity is higher than the product stock.", Data = null })); } ProductDetails productDetails = new ProductDetails(); productDetails.ProductSKU = cart.ProductSKU; productDetails.Status = "Order Placed"; List <StatusCode> productStatus = new List <StatusCode>(); productStatus.Add(new StatusCode { StatusId = 1, Date = DateTime.UtcNow, Description = "OrderPlaced" }); productDetails.StatusCode = productStatus; productDetails.ProductInCart = cart; productList.Add(productDetails); } } data.ProductDetails = productList; await order_db.GetCollection <OrderInfo>("OrderInfo").InsertOneAsync(data); foreach (var cart in cartDatas) { foreach (var product in GH.GetProducts(cart.ProductSKU, product_db).Result) { var update = Builders <BsonDocument> .Update.Set("ProductStock", product.ProductStock - cart.ProductQuantity); var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", cart.ProductSKU), "ProductDB", "Product", update).Result; } var response = MH.DeleteSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", cart.ProductSKU), "UserInfo", "Cart"); } return(Ok(new ResponseData { Code = "200", Message = "Order Placed", Data = null })); } else { return(BadRequest(new ResponseData { Code = "402", Message = "Cart not found", Data = null })); } } else { return(BadRequest(new ResponseData { Code = "401", Message = "UserInfo not found", Data = null })); } } else { return(BadRequest(new ResponseData { Code = "404", Message = "Provide a payment method", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("OrderController", "PlaceOrder", "PlaceOrder", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public async Task <ActionResult> CancelOrder([FromBody] OrderInfo data, string username, string productSKU) { try { var orders = GH.GetOrders(username, order_db).Result; if (orders == null) { return(BadRequest(new ResponseData { Code = "404", Message = "No orders found", Data = null })); } else { IAsyncCursor <OrderInfo> orderCursor = await order_db.GetCollection <OrderInfo>("OrderInfo").FindAsync(Builders <OrderInfo> .Filter.Eq("UserName", username) & Builders <OrderInfo> .Filter.Eq("OrderId", data.OrderId)); var order = orderCursor.FirstOrDefaultAsync().Result; if (order == null) { return(BadRequest(new ResponseData { Code = "404", Message = "Order Not Found", Data = null })); } foreach (var productDetails in order.ProductDetails) { if (productDetails.Status == "Cancled" || productDetails.Status == "Refunded" || productDetails.Status == "Replaced" || productDetails.Status == "Delivered") { return(BadRequest(new ResponseData { Code = "401", Message = "Order Cancle Request Failed", Data = null })); } else { PaymentMethod paymentMethod = new PaymentMethod(); paymentMethod.Method = order.PaymentMethod; List <StatusCode> paymentList = new List <StatusCode>(); int i = 1; foreach (var status in order.PaymentDetails.Status) { paymentList.Add(status); i++; } StatusCode paymentStatus = new StatusCode(); paymentStatus.StatusId = i; paymentStatus.Date = DateTime.UtcNow; if (data.PaymentMethod == "Cash On Delivery") { paymentStatus.Description = "Payment Cancled"; } else { paymentStatus.Description = "Refund Initiated"; } paymentList.Add(paymentStatus); paymentMethod.Status = paymentList; var paymentUpdate = Builders <BsonDocument> .Update.Set("PaymentDetails", paymentMethod); var result = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", paymentUpdate).Result; List <ProductDetails> productDetailsList = new List <ProductDetails>(); foreach (var product in order.ProductDetails) { if (product.ProductSKU == productSKU) { ProductDetails details = new ProductDetails(); details.ProductSKU = productSKU; details.Status = "Cancled"; List <StatusCode> productStatusList = new List <StatusCode>(); int j = 1; foreach (var status in product.StatusCode) { productStatusList.Add(status); j++; } StatusCode productStatus = new StatusCode { StatusId = j, Date = DateTime.UtcNow, Description = "Cancled" }; productStatusList.Add(productStatus); details.StatusCode = productStatusList; details.ProductInCart = product.ProductInCart; productDetailsList.Add(details); } else { productDetailsList.Add(product); } } var productUpdate = Builders <BsonDocument> .Update.Set("ProductDetails", productDetailsList); var responce = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("UserName", username) & Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", productUpdate).Result; } } IAsyncCursor <OrderInfo> orderCursorAfterUpdate = await order_db.GetCollection <OrderInfo>("OrderInfo").FindAsync(Builders <OrderInfo> .Filter.Eq("UserName", username) & Builders <OrderInfo> .Filter.Eq("OrderId", data.OrderId)); var orderAfterUpdate = orderCursorAfterUpdate.FirstOrDefaultAsync().Result; foreach (var product in orderAfterUpdate.ProductDetails) { if (product.ProductSKU == productSKU) { IAsyncCursor <Product> productCursor = await product_db.GetCollection <Product>("Product").FindAsync(Builders <Product> .Filter.Eq("ProductSKU", productSKU)); var productData = productCursor.FirstOrDefaultAsync().Result; var productUpdate = Builders <BsonDocument> .Update.Set("ProductStock", productData.ProductStock + product.ProductInCart.ProductQuantity); var responce = MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("ProductSKU", productSKU), "ProductDB", "Product", productUpdate).Result; } } return(Ok(new ResponseData { Code = "200", Message = "Success", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("OrderController", "CancleOrder", "CancleOrder", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }
public ActionResult UseCoupon([FromBody] UseCoupon data, string code) { try { var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon"); if (checkData != null) { var result = BsonSerializer.Deserialize <Coupon>(checkData); var filter = Builders <BsonDocument> .Filter.Eq("Code", code); if (data.UsageCount > 0) { var coupon = BsonSerializer.Deserialize <Coupon>(MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon")); if (result.Percentage == true) { var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", result.UsageCount - data.UsageCount)); } else { if (data.Amount > coupon.Value) { return(Ok(new ResponseData { Code = "401", Message = "Amount is higher than the coupon value", Data = null })); } else { var balance = coupon.Value - data.Amount; var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Value", balance)); if (balance == 0) { var updateUsageCount = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", 0)); } } } } return(Ok(new ResponseData { Code = "200", Message = "Coupon updated successfully", Data = null })); } else { return(BadRequest(new ResponseData { Code = "404", Message = "Coupon not found", Data = null })); } } catch (Exception ex) { LoggerDataAccess.CreateLog("CouponController", "UseCoupon", ex.Message); return(BadRequest(new ResponseData { Code = "400", Message = "Failed", Data = ex.Message })); } }