public ServicesController()
        {
            context = new DataAccess.Context();

            customerServices     = new CustomerServices();
            employeeServices     = new EmployeeServices();
            orderServices        = new OrderServices();
            orderDetailsServices = new OrderDetailsServices();
            productServices      = new ProductServices();
        }
Ejemplo n.º 2
0
        public ServicesController()
        {
            this.context = new DataAccess.Context();

            this.customerServices     = new CustomerServices();
            this.employeeServices     = new EmployeeServices();
            this.orderServices        = new OrderServices();
            this.orderDetailsServices = new OrderDetailsServices();
            this.productServices      = new ProductServices();
        }
        public void Remove(OrderDto orderDto, ServicesController services)
        {
            try
            {
                var deletedOrderId = orderDto.OrderID;

                var orderRemove = this.orderRepository.Set()
                                  .FirstOrDefault(x => x.OrderID == orderDto.OrderID);

                if (orderRemove == null)
                {
                    throw new Exception("La Orden no existe!");
                }

                using (var detailsServices = new OrderDetailsServices())
                {
                    if (orderRemove.Order_Details.Any())
                    {
                        var detailsRemove = detailsServices.GetAll()
                                            .Where(d => d.OrderID == orderRemove.OrderID)
                                            .Select(d => new Order_Detail
                        {
                            OrderID = d.OrderID,
                            Order   = new Order
                            {
                                OrderID = d.OrderID
                            },
                            Discount  = d.Discount,
                            Product   = services.productServices.GetProductByID(d.ProductID),
                            ProductID = d.ProductID,
                            Quantity  = d.Quantity,
                            UnitPrice = d.UnitPrice,
                        });

                        foreach (var detail in orderRemove.Order_Details)
                        {
                            var detailRemove = this.orderDetailsRepository.Set()
                                               .FirstOrDefault(x => x.OrderID == orderRemove.OrderID);

                            if (detailRemove == null)
                            {
                                throw new Exception("No hay Detalle de Orden.");
                            }

                            NewLine();
                            Console.WriteLine($"El Detalle con ID de Orden : {detail.OrderID}, Producto : {detail.Product.ProductName}, Cantidad : {detail.Quantity} será eliminado.");

                            this.orderDetailsRepository.Remove(detailRemove);
                            this.orderDetailsRepository.SaveChanges();
                        }

                        NewLine();
                        Console.WriteLine($"Detalles eliminados.");
                    }
                    else
                    {
                        NewLine();
                        Console.WriteLine($"La Orden no tenía Detalles asociados.");
                    }

                    var order = this.orderRepository.Set()
                                .FirstOrDefault(x => x.OrderID == deletedOrderId);

                    if (order == null)
                    {
                        throw new Exception("El cliente no existe");
                    }

                    this.orderRepository.Remove(order);
                    this.orderRepository.SaveChanges();

                    NewLine();
                    Console.WriteLine($"La Orden con ID : {deletedOrderId} ha sido eliminada con éxito.");
                }
            }
            catch
            {
                NewLine();
                Console.WriteLine($"Se produjo un ERROR al intentar Eliminar la Orden con ID: '{orderDto.OrderID}'.");

                return;
            }
        }