Ejemplo n.º 1
0
        public ApiResultModel <dynamic> CreateCustomer(CustomerDetailsViewModel customer)
        {
            ApiResultModel <dynamic> apiResult;

            try
            {
                customer.CreatedDate  = DateTime.Now;
                customer.ModifiedDate = DateTime.Now;
                CustomerDetail customerDetails = Mapper.Map <CustomerDetail>(customer);
                db.CustomerDetails.Add(customerDetails);
                db.SaveChanges();
                apiResult = PrepareReturnResult(ApiConstants.CustomerCreatedSuccessfully, isSuccess: true);
            }
            catch (DbEntityValidationException)
            {
                apiResult = PrepareReturnResult(ApiConstants.InvalidInputs);
            }
            catch (Exception ex)
            {
                if (IsDuplicateEmailIdException(ex))
                {
                    apiResult = PrepareReturnResult(ApiConstants.DuplicateEmail);
                }
                else
                {
                    throw ex;
                }
            }
            return(apiResult);
        }
        /// <summary>
        /// To Delete Order
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public bool DeleteOrder(Guid OrderId)
        {
            try
            {
                using (var dbContext = new OrderManagementEntities())
                {
                    var orderInfo = dbContext.Orders.FirstOrDefault(x => x.UUID == OrderId);
                    if (orderInfo == null)
                    {
                        throw new Exception("Order Not found or Invalid Order");
                    }

                    orderInfo.IsDeleted = true;
                    dbContext.Orders.Add(orderInfo);
                    dbContext.Entry(orderInfo).State = EntityState.Modified;
                    dbContext.SaveChanges();

                    //Updating Available Quantity of products
                    UpdateCanceledProductQuantity(dbContext, orderInfo);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// To Update an Order
        /// </summary>
        /// <param name="UpdateOrderModel"></param>
        /// <returns></returns>
        public bool UpdateOrder(UpdateOrderModel UpdateOrderModel)
        {
            try
            {
                using (var dbContext = new OrderManagementEntities())
                {
                    var products            = new List <Product>();
                    var OrderStatusParentID = 1;
                    var userInfo            = ValidateUser(UpdateOrderModel.UserID, dbContext);
                    if (!userInfo.isBuyer)
                    {
                        throw new Exception("Invalid User");
                    }

                    var orderInfo = dbContext.Orders.FirstOrDefault(x => x.UUID == UpdateOrderModel.OrderId);
                    if (orderInfo == null)
                    {
                        throw new Exception("Order Not found or Invalid Order");
                    }

                    if (orderInfo.UserID != userInfo.Id)
                    {
                        throw new Exception("Orders are not associated with give user");
                    }

                    var orderState = GetLookupValues(OrderStatusParentID, dbContext)
                                     .FirstOrDefault(x => x.UUID == UpdateOrderModel.OrderState);
                    if (orderState == null)
                    {
                        throw new Exception("Invalid Order Status");
                    }

                    orderInfo.ShippingAddress = UpdateOrderModel.ShippingAddress;
                    orderInfo.Items           = xmlSerialize(UpdateOrderModel.Items, dbContext, ref products, orderInfo);
                    orderInfo.OrderState      = orderState.Id;
                    dbContext.Orders.Add(orderInfo);
                    dbContext.Entry(orderInfo).State = EntityState.Modified;
                    dbContext.SaveChanges();

                    //Updating Available Quantity of products
                    UpdateProductsAvailableQuantity(products);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// To Create an Order
        /// </summary>
        /// <param name="OrderModel"></param>
        /// <returns></returns>
        public bool AddOrder(OrderModel OrderModel)
        {
            try
            {
                using (var dbContext = new OrderManagementEntities())
                {
                    var OrderStatusParentID = 1;
                    var order    = new Order();
                    var items    = new List <Item>();
                    var products = new List <Product>();

                    var userInfo = ValidateUser(OrderModel.UserID, dbContext);
                    if (!userInfo.isBuyer)
                    {
                        throw new Exception("Invalid User");
                    }
                    order.UserID = userInfo.Id;

                    var orderState = GetLookupValues(OrderStatusParentID, dbContext).FirstOrDefault(x => x.UUID == OrderModel.OrderState);
                    if (orderState == null)
                    {
                        throw new Exception("Invalid Order Status");
                    }

                    order.OrderState      = orderState.Id;
                    order.ShippingAddress = OrderModel.ShippingAddress;
                    order.Items           = xmlSerialize(OrderModel.Items, dbContext, ref products);
                    order.UUID            = Guid.NewGuid();
                    dbContext.Orders.Add(order);
                    dbContext.SaveChanges();

                    //Sending Email To The User
                    SmtpHelper.SendEmail(userInfo.Email, userInfo.UserName);
                    //Updating Available Quantity of products
                    UpdateProductsAvailableQuantity(products);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 /// <summary>
 /// To Update Products Available Quantity
 /// </summary>
 /// <param name="Products"></param>
 private void UpdateProductsAvailableQuantity(List <Product> Products)
 {
     try
     {
         using (var dbContext = new OrderManagementEntities())
         {
             foreach (var pro in Products)
             {
                 dbContext.Products.Add(pro);
                 dbContext.Entry(pro).State = EntityState.Modified;
                 dbContext.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }