public CustomerDto GetCustomerDtoByID(string customerId, ServicesController services) { try { var customer = services.customerServices.customerRepository.Set().ToList() .FirstOrDefault(c => c.CustomerID == customerId); if (customer == null) { NewLine(); Console.WriteLine("No existe el Cliente!"); return(null); } var customerDto = new CustomerDto() { CustomerID = customer.CustomerID, ContactName = customer.ContactName, CompanyName = customer.CompanyName, }; return(customerDto); } catch { Console.WriteLine($"Se produjo un ERROR al intentar recuperar el Cliente con ID : '{customerId}'..."); return(null); } }
public List <BestSellerProductDto> GetBestSellProduct(ServicesController services) { try { var customers = services.customerServices.GetAll(); var bestSellerProducts = customers .Where(c => (c.CustomerID != null && c.Country != null)) .GroupBy(c => c.Country) .Select(k => new BestSellerProductDto { Country = k.Key, Name = k .SelectMany(p => p.Orders) .SelectMany(d => d.Order_Details) .GroupBy(d => d.ProductID) .OrderByDescending(d => d.Count()) .FirstOrDefault() .Select(d => d.Product.ProductName) .FirstOrDefault() }).ToList(); return(bestSellerProducts); } catch { NewLine(); Console.WriteLine($"Se produjo un ERROR al intentar obtener el Producto más vendido por País."); return(null); } }
public Employee GetEmployeeByID(Nullable <int> employeeId, ServicesController services) { try { var employee = services.employeeServices.employeeRepository.Set().ToList() .FirstOrDefault(e => e.EmployeeID == employeeId); if (employee == null) { NewLine(); Console.WriteLine("No existe el Empleado!"); return(null); } return(employee); } catch { NewLine(); Console.WriteLine($"Se produjo un ERROR al intentar obtener el Empleado con ID : '{employeeId}'."); return(null); } }
public void Update(OrderDto orderDto, ServicesController services) { try { var order = this.orderRepository.Set() .FirstOrDefault(x => x.OrderID == orderDto.OrderID); if (order == null) { //throw new Exception("La orden no existe"); NewLine(); Console.WriteLine($"La Orden : {orderDto.OrderID} NO ha sido encontrada!."); } order.CustomerID = orderDto.CustomerID; order.EmployeeID = orderDto.EmployeeID; order.Freight = orderDto.Freight; order.OrderDate = orderDto.OrderDate; order.RequiredDate = orderDto.RequiredDate; order.ShipAddress = orderDto.ShipAddress; order.ShipCity = orderDto.ShipCity; order.ShipCountry = orderDto.ShipCountry; order.ShipName = orderDto.ShipName; order.ShippedDate = orderDto.ShippedDate; order.ShipPostalCode = orderDto.ShipPostalCode; order.ShipRegion = orderDto.ShipRegion; order.ShipVia = orderDto.ShipVia; //order.Order_Details = orderDto.Order_Details; NewLine(); Console.WriteLine($"La Orden con ID : '{orderDto.OrderID}' ha sido modificada."); this.orderRepository.Update(order); this.orderRepository.SaveChanges(); } catch { NewLine(); Console.WriteLine($"Se produjo un ERROR al intentar Modificar la Orden con ID : '{orderDto.OrderID}'."); return; } }
public Customer GetCustomerByID(string customerId, ServicesController services) { var customer = services.customerServices.customerRepository.Set().ToList() .FirstOrDefault(c => c.CustomerID == customerId); if (customer == null) { Console.WriteLine("No existe el Cliente!"); return(null); } var customerByID = new Customer() { CustomerID = customer.CustomerID, ContactName = customer.ContactName, CompanyName = customer.CompanyName, }; return(customerByID); }
public List <BestCustomerDto> GetBestCostumer(ServicesController services) { try { var customers = services.customerServices.GetAll(); var bestCostumers = customers .Where(c => c.Country != null) .GroupBy(c => c.Country) .Select(k => new BestCustomerDto { Country = k.Key, Name = k .OrderByDescending(c => c.Orders .Sum(o => o.Order_Details .Sum(d => d.Quantity * d.Product.UnitPrice))) .Select(c => c.ContactName) .FirstOrDefault(), TotalPurchased = k.Select(v => v.Orders .Where(c => c.CustomerID == k .OrderByDescending(b => b.Orders .Sum(o => o.Order_Details .Sum(d => d.Quantity * d.Product.UnitPrice))) .Select(b => b.CustomerID) .FirstOrDefault()) .Sum(g => g.Order_Details .Sum(d => d.Quantity * d.Product.UnitPrice))) .Sum() }).ToList(); return(bestCostumers); } catch { Console.WriteLine("Se produjo un ERROR al intentar obtener el mejor Cliente por País..."); return(null); } }
public ICollection <OrderDto> GetAll(ServicesController services) { try { return(this.orderRepository.Set() .ToList() .Select(o => new OrderDto { OrderID = o.OrderID, CustomerID = o.CustomerID, EmployeeID = o.EmployeeID, ShipName = o.ShipName, }).ToList()); } catch { Console.WriteLine("Se produjo un ERROR al intentar obtener todas las Ordenes..."); return(null); } }
//public EmployeeDto GetEmployeeByID(Nullable<int> employeeId) public EmployeeDto GetEmployeeDtoByID(Nullable <int> employeeId, ServicesController services) { var employee = services.employeeServices.employeeRepository.Set().ToList() .FirstOrDefault(e => e.EmployeeID == employeeId); if (employee == null) { Console.WriteLine("No existe el Empleado!"); return(null); } //var employeeDto = new EmployeeDto() var employeeDto = new EmployeeDto() { EmployeeID = employee.EmployeeID, //ContactName = employee.ContactName, //CompanyName = employee.CompanyName, }; return(employeeDto); }
public ICollection <Order> GetAllByIDOfCustomer(string customerID, ServicesController services) { try { return(this.orderRepository.Set() .Where(o => o.CustomerID == customerID) .Select(o => new Order { OrderID = o.OrderID, CustomerID = o.CustomerID, ShipCountry = o.ShipCountry, Order_Details = o.Order_Details, }).ToList()); } catch { NewLine(); Console.WriteLine("Se produjo un ERROR al intentar obtener todas las "); Console.WriteLine($"ordenes del Cliente con ID: '{customerID}'."); return(null); } }
public Order GetOrderByID(int orderId, ServicesController services) { try { var order = this.orderRepository.Set().ToList() .FirstOrDefault(c => c.OrderID == orderId); if (order == null) { NewLine(); Console.WriteLine("No existe la orden!"); return(null); } var orderDetails = new HashSet <Order_Detail>(); foreach (var orderDetail in order.Order_Details) { orderDetails.Add(new Order_Detail() { Discount = orderDetail.Discount, OrderID = orderDetail.OrderID, ProductID = orderDetail.ProductID, Quantity = orderDetail.Quantity, UnitPrice = orderDetail.UnitPrice, }); } var orderByID = new Order() { OrderID = order.OrderID, CustomerID = order.CustomerID, EmployeeID = order.EmployeeID, OrderDate = order.OrderDate, RequiredDate = order.RequiredDate, ShippedDate = order.ShippedDate, Freight = order.Freight, ShipName = order.ShipName, ShipCity = order.ShipCity, ShipRegion = order.ShipRegion, ShipPostalCode = order.ShipPostalCode, ShipCountry = order.ShipCountry, //Customer Customer = services.customerServices.GetCustomerByID(order.CustomerID, services), //Employee Employee = services.employeeServices.GetEmployeeByID(order.EmployeeID, services), Order_Details = orderDetails, //Shipper }; return(orderByID); } catch { Console.WriteLine($"Se produjo un ERROR al intentar obtener la Orden con ID : '{orderId}'..."); return(null); } }
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; } }
public void Create(OrderDto orderDto, ServicesController services) { var OrderDetails = new HashSet <Order_Detail>(); try { foreach (var orderDetail in orderDto.Order_Details) { OrderDetails.Add(new Order_Detail { Discount = orderDetail.Discount, OrderID = orderDetail.OrderID, ProductID = orderDetail.ProductID, Product = services.productServices.GetProductByID(orderDetail.ProductID), Quantity = orderDetail.Quantity, UnitPrice = orderDetail.UnitPrice, }); } } catch { NewLine(); Console.WriteLine("Se produjo un ERROR al agregar los Detalles de Orden "); Console.WriteLine("al intentar Crear una nueva Orden..."); return; } try { this.orderRepository.Persist(new Order { OrderID = orderDto.OrderID, CustomerID = orderDto.CustomerID, EmployeeID = orderDto.EmployeeID, OrderDate = orderDto.OrderDate, RequiredDate = orderDto.RequiredDate, ShippedDate = orderDto.ShippedDate, Freight = orderDto.Freight, ShipName = orderDto.ShipName, ShipCity = orderDto.ShipCity, ShipRegion = orderDto.ShipRegion, ShipPostalCode = orderDto.ShipPostalCode, ShipAddress = orderDto.ShipAddress, ShipCountry = orderDto.ShipCountry, ShipVia = orderDto.ShipVia, //Customer //Customer = services.customerServices.GetCustomerByID(orderDto.CustomerID,services), //Employee = orderDto.Employee, //Employee = services.employeeServices.GetEmployeeByID(orderDto.EmployeeID,services), //Shipper Order_Details = OrderDetails, }); this.orderRepository.SaveChanges(); } catch { NewLine(); Console.WriteLine($"Se produjo un ERROR de guardado al intentar Crear la nueva Orden."); return; } }
public Order GetOrderByID(int orderId, ServicesController services) { var order = orderRepository.Set().ToList() .FirstOrDefault(c => c.OrderID == orderId); if (order == null) { Console.WriteLine("No existe la orden!"); return(null); } //CARGO LAS ORDENES EN UNA LISTA var orderDetails = new HashSet <Order_Detail>(); foreach (var orderDetail in order.Order_Details) { orderDetails.Add(new Order_Detail() { Discount = orderDetail.Discount, //Order = orderDetail.Order, OrderID = orderDetail.OrderID, //Product = orderDetail.Product, ProductID = orderDetail.ProductID, Quantity = orderDetail.Quantity, UnitPrice = orderDetail.UnitPrice, }); } var orderByID = new Order() { OrderID = order.OrderID, CustomerID = order.CustomerID, EmployeeID = order.EmployeeID, OrderDate = order.OrderDate, RequiredDate = order.RequiredDate, ShippedDate = order.ShippedDate, Freight = order.Freight, ShipName = order.ShipName, ShipCity = order.ShipCity, ShipRegion = order.ShipRegion, ShipPostalCode = order.ShipPostalCode, ShipCountry = order.ShipCountry, //Customer Customer = services.customerServices.GetCustomerByID(order.CustomerID, services), //Employee = orderDto.Employee, Employee = services.employeeServices.GetEmployeeByID(order.EmployeeID, services), Order_Details = orderDetails, /*Order_Details = new HashSet<Order_Detail>() * { * foreach (var order in orderDto.Order_Details) * { * new Order_Detail() * { * Discount = order.Discount, * //Order = order.Order, * OrderID = order.OrderID, * //Product = order.Product, * ProductID = order.ProductID, * Quantity = order.Quantity, * UnitPrice = order.UnitPrice, * }; * } }); */ //Shipper }; return(orderByID); }
public void Create(OrderDto orderDto, ServicesController services) { //CARGO LAS ORDENES EN UNA LISTA var OrderDetails = new HashSet <Order_Detail>(); foreach (var orderDetail in orderDto.Order_Details) { OrderDetails.Add(new Order_Detail { Discount = orderDetail.Discount, //Order = orderDetail.Order, Order = services.orderServices.GetOrderByID(orderDetail.OrderID, services), OrderID = orderDetail.OrderID, //Product = orderDetail.Product, ProductID = orderDetail.ProductID, Quantity = orderDetail.Quantity, UnitPrice = orderDetail.UnitPrice, }); } orderRepository.Persist(new Order { OrderID = orderDto.OrderID, CustomerID = orderDto.CustomerID, EmployeeID = orderDto.EmployeeID, OrderDate = orderDto.OrderDate, RequiredDate = orderDto.RequiredDate, ShippedDate = orderDto.ShippedDate, Freight = orderDto.Freight, ShipName = orderDto.ShipName, ShipCity = orderDto.ShipCity, ShipRegion = orderDto.ShipRegion, ShipPostalCode = orderDto.ShipPostalCode, ShipAddress = orderDto.ShipAddress, ShipCountry = orderDto.ShipCountry, ShipVia = orderDto.ShipVia, //Customer //Customer = services.customerServices.GetCustomerByID(orderDto.CustomerID,services), //Employee = orderDto.Employee, //Employee = services.employeeServices.GetEmployeeByID(orderDto.EmployeeID,services), //Shipper Order_Details = OrderDetails, /*Order_Details = new HashSet<Order_Detail>() * { * foreach (var order in orderDto.Order_Details) * { * new Order_Detail() * { * Discount = order.Discount, * //Order = order.Order, * OrderID = order.OrderID, * //Product = order.Product, * ProductID = order.ProductID, * Quantity = order.Quantity, * UnitPrice = order.UnitPrice, * }; * } }); */ }); orderRepository.SaveChanges(); }
//public void ModifyOrder(string orderId) public void Modify(OrderDto orderDto, ServicesController services) { var order = orderRepository.Set() .FirstOrDefault(x => x.OrderID == orderDto.OrderID); if (order == null) { throw new Exception("La orden no existe"); } Console.WriteLine($"La Orden : {orderDto.OrderID} ha sido encontrada."); //Console.WriteLine($"Su Nombre de Contacto es : {orderDto.ContactName}."); //OrderID var orderId = 0; do { Console.WriteLine(""); Console.WriteLine("Ingrese el nuevo ID de la Orden:"); }while (int.TryParse(Console.ReadLine(), out orderId)); orderDto.OrderID = orderId; //ContactName Console.WriteLine(""); Console.WriteLine("Ingrese el País destino de la Orden:"); orderDto.ShipCountry = Console.ReadLine(); //Date DateTime orderDate; do { Console.WriteLine(""); Console.WriteLine("Ingrese la fecha de Orden (formato dd/MM/yyyy):"); }while (!(DateTime.TryParseExact(Console.ReadLine(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out orderDate))); /* * if (DateTime.TryParseExact(Console.ReadLine(), * "dd/MM/yyyy", * CultureInfo.InvariantCulture, * DateTimeStyles.None, * out dt)) * { * //valid date * * } * else * { * //invalid date * } */ orderRepository.Update(order); orderRepository.SaveChanges(); }