public ActionResult Delete(string productFor, string productType)
        {
            try
            {
                var filter = Builders <BsonDocument> .Filter.Eq("ProductFor", productFor) & Builders <BsonDocument> .Filter.Eq("ProductType", productType);

                var product = MH.GetSingleObject(filter, "ProductDB", "Category").Result;
                if (product != null)
                {
                    var response = MH.DeleteSingleObject(filter, "ProductDB", "Category");
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Deleted",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Category Not Found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CategoryController", "Delete", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
 public ActionResult DeleteCoupon(string code)
 {
     try
     {
         var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon");
         if (checkData != null)
         {
             var delete = MH.DeleteSingleObject(Builders <BsonDocument> .Filter.Eq("Code", code), "CouponDB", "Coupon");
             return(Ok(new ResponseData
             {
                 Code = "200",
                 Message = "Coupon deleted 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> 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
                }));
            }
        }