예제 #1
0
        public static string ExportOrdersByEmployee(FastFoodDbContext context, string employeeName, string orderType)
        {
            var employee       = context.Employees.FirstOrDefault(e => e.Name == employeeName);
            var type           = Enum.Parse <OrderType>(orderType);
            var totalMoneyMade = 0m;

            var orders = context.Orders
                         .Include(o => o.Employee)
                         .Include(o => o.OrderItems)
                         .Where(o => o.Type == type && o.Employee.Name == employeeName)
                         .ToArray();

            var dtosToExport = new List <OrderDto>();

            foreach (var order in orders)
            {
                var dto = new OrderDto
                {
                    Customer = order.Customer
                };

                var orderPrice = 0m;

                foreach (var oi in order.OrderItems)
                {
                    orderPrice += oi.Item.Price * oi.Quantity;
                    dto.Items.Add(new ItemDto
                    {
                        Name     = oi.Item.Name,
                        Price    = oi.Item.Price,
                        Quantity = oi.Quantity
                    });
                }

                dto.TotalPrice  = orderPrice;
                totalMoneyMade += orderPrice;

                dtosToExport.Add(dto);
            }

            var orderedDtos = dtosToExport
                              .OrderByDescending(d => d.TotalPrice)
                              .ThenByDescending(d => d.Items.Count)
                              .ToList();

            var result = new
            {
                Name   = employeeName,
                Orders = orderedDtos.Select(d => new
                {
                    Customer = d.Customer,
                    Items    = d.Items.Select(i => new
                    {
                        Name     = i.Name,
                        Price    = i.Price,
                        Quantity = i.Quantity
                    }).ToArray(),
                    TotalPrice = d.TotalPrice
                }).ToArray(),
                TotalMade = totalMoneyMade
            };

            string jsonString = JsonConvert.SerializeObject(result, Formatting.Indented);

            return(jsonString);
        }
예제 #2
0
        public static string ImportOrders(FastFoodDbContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var xml  = XDocument.Parse(xmlString);
            var root = xml.Root.Elements();

            foreach (var element in root)
            {
                string customer = element.Element("Customer")?.Value;
                string employee = element.Element("Employee")?.Value;
                var    dateTime = DateTime.ParseExact(element.Element("DateTime")?.Value, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                string type     = element.Element("Type")?.Value.ToString();

                var orderDto = new OrderDto()
                {
                    Customer = customer,
                    Employee = employee,
                    DateTime = dateTime.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture),
                    Type     = type
                };

                if (!IsValid(orderDto) || !context.Employees.Any(e => e.Name == employee))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var itemsElements = element.Element("Items")?.Elements().ToList();
                var items         = new List <OrderItem>();

                foreach (var item in itemsElements)
                {
                    var    quantity = item.Element("Quantity")?.Value ?? "0";
                    string name     = item.Element("Name")?.Value;

                    var orderItem = new OrderItemDto()
                    {
                        Name     = name,
                        Quantity = int.Parse(quantity)
                    };

                    if (!IsValid(orderItem) || !context.Items.Any(i => i.Name == name))
                    {
                        sb.AppendLine(FailureMessage);
                        continue;
                    }

                    var final = new OrderItem()
                    {
                        ItemId   = context.Items.SingleOrDefault(i => i.Name == name).Id,
                        Quantity = int.Parse(quantity)
                    };

                    items.Add(final);
                }

                orderDto.Items.AddRange(items);

                var order = new Order()
                {
                    Customer   = orderDto.Customer,
                    DateTime   = dateTime,
                    Type       = Enum.Parse <OrderType>(orderDto.Type),
                    EmployeeId = context.Employees.SingleOrDefault(e => e.Name == orderDto.Employee).Id,
                    OrderItems = new List <OrderItem>(orderDto.Items)
                };

                context.Orders.Add(order);
                context.SaveChanges();
                sb.AppendLine($"Order for {customer} on {dateTime.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)} added");
            }

            return(sb.ToString());
        }