public OrderFormResponse CreateOrder(OrderForm orderForm)
        {
            OrderFormResponse orderFormResponse = new OrderFormResponse()
            {
                StatusCode = 200
            };

            try
            {
                if (orderForm == null)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm),
                            Message   = "Invalid Request Data"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderItems == null || orderForm.OrderItems.Count == 0)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.OrderItems),
                            Message   = "Invalid OrderItems -: Should be atleast one"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.CustomerAddress == null)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress),
                            Message   = "Invalid CustomerAddress"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.CustomerId <= 0)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerId),
                            Message   = "Invalid CustomerId"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.TotalItem <= 0 || orderForm.TotalItem != orderForm.OrderItems.Count)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.TotalItem),
                            Message   = "Invalid TotalItem , it is equal to OrderItems."
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderTotal <= 0)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.OrderTotal),
                            Message   = "Invalid OrderTotal"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.PaymentMode))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.PaymentMode),
                            Message   = "Invalid PaymentMode"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.CustomerAddress.Address1))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.Address1),
                            Message   = "Invalid Address1"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.CustomerAddress.Address2))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.Address2),
                            Message   = "Invalid Address2"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.CustomerAddress.City))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.City),
                            Message   = "Invalid City"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.CustomerAddress.State))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.State),
                            Message   = "Invalid State"
                        }
                    };
                    return(orderFormResponse);
                }

                if (string.IsNullOrWhiteSpace(orderForm.CustomerAddress.Phone))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.Phone),
                            Message   = "Invalid Phone"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.CustomerAddress.Pincode <= 0)
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = nameof(orderForm.CustomerAddress.Pincode),
                            Message   = "Invalid Pincode"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderItems.Any(x => x.ItemId <= 0))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = "ItemId",
                            Message   = "OrderItems Contain Invalid ItemId"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderItems.Any(x => x.ListPrice <= 0))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = "ListPrice",
                            Message   = "OrderItems Contain Invalid ListPrice"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderItems.Any(x => x.Qty <= 0))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = "Qty",
                            Message   = "OrderItems Contain Invalid Qty"
                        }
                    };
                    return(orderFormResponse);
                }

                if (orderForm.OrderItems.Any(x => string.IsNullOrWhiteSpace(x.ItemName)))
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code      = 1001,
                            FieldName = "ItemName",
                            Message   = "OrderItems Contain Invalid ItemName"
                        }
                    };
                    return(orderFormResponse);
                }

                bool isOrderCreated = _orderLayer.CreateOrder(orderForm);
                if (isOrderCreated)
                {
                    orderFormResponse.StatusCode = 200;
                    orderFormResponse.Message    = "Order Created Successfully";
                }
                else
                {
                    orderFormResponse.StatusCode = 400;
                    orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                    {
                        new OrderFormValidationResponse()
                        {
                            Code    = 1001,
                            Message = "Error While Creating Order"
                        }
                    };
                    return(orderFormResponse);
                }
            }
            catch (Exception ex)
            {
                orderFormResponse.StatusCode = 400;
                orderFormResponse.ErrorList  = new List <OrderFormValidationResponse>()
                {
                    new OrderFormValidationResponse()
                    {
                        Code    = 1001,
                        Message = "Error While Creating Order" + ex.Message
                    }
                };
            }

            return(orderFormResponse);
        }
示例#2
0
        public ActionResult <OrderFormResponse> Post([FromBody] OrderForm orderForm)
        {
            OrderFormResponse orderResponse = _orderBiz.CreateOrder(orderForm);

            return(Ok(orderResponse));
        }