示例#1
0
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            EmployeeDto[] employeesDto = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString);
            var           sb           = new StringBuilder();

            var validEmployees = new List <Employee>();

            var positions = new HashSet <Position>();

            foreach (var employeeDto in employeesDto)
            {
                int? positionId   = context.Positions.SingleOrDefault(p => p.Name == employeeDto.Position)?.Id;
                bool isDuplicated = positions.Any(p => p.Name == employeeDto.Position);

                if (!IsValid(employeeDto))
                {
                    continue;
                }
                if (isDuplicated)
                {
                    continue;
                }
                if (positionId == null)
                {
                    var position = new Position()
                    {
                        Name = employeeDto.Position
                    };
                    positions.Add(position);
                }
            }
            context.AddRange(positions);
            context.SaveChanges();

            foreach (var employeeDto in employeesDto)
            {
                if (!IsValid(employeeDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }
                var employee = new Employee()
                {
                    Name     = employeeDto.Name,
                    Age      = employeeDto.Age,
                    Position = positions.FirstOrDefault(p => p.Name == employeeDto.Position)
                };
                validEmployees.Add(employee);
                sb.AppendLine(String.Format(SuccessMessage, employee.Name));
            }
            context.AddRange(validEmployees);
            context.SaveChanges();

            var result = sb.ToString().Trim();

            return(result);
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var itemDtos = JsonConvert.DeserializeObject <ItemImportDto[]>(jsonString);

            StringBuilder builder = new StringBuilder();

            List <Category> categoriesToAdd = new List <Category>();
            List <Item>     items           = new List <Item>();

            foreach (var itemDto in itemDtos)
            {
                bool itemIsValid = IsValid(itemDto);
                if (!itemIsValid)
                {
                    builder.AppendLine(FailureMessage);
                    continue;
                }

                bool itemExistsInDb           = context.Items.Any(i => i.Name == itemDto.Name);
                bool itemExistsInCurrentBatch = items.Any(i => i.Name == itemDto.Name);
                if (itemExistsInDb || itemExistsInCurrentBatch)
                {
                    builder.AppendLine(FailureMessage);
                    continue;
                }


                Category category = context.Categories.FirstOrDefault(c => c.Name == itemDto.Category);
                category = categoriesToAdd.FirstOrDefault(c => c.Name == itemDto.Category);

                if (category == null)
                {
                    category = new Category
                    {
                        Name = itemDto.Name
                    };

                    categoriesToAdd.Add(category);
                }

                var item = AutoMapper.Mapper.Map <Item>(itemDto);
                item.Category = category;

                items.Add(item);

                builder.AppendLine(String.Format(SuccessMessage, item.Name));
            }

            context.AddRange(categoriesToAdd);
            context.AddRange(items);
            context.SaveChanges();

            return(builder.ToString().TrimEnd());
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var itemDtos = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var sb           = new StringBuilder();
            var validObjects = new List <Item>();

            foreach (var itemDto in itemDtos)
            {
                var alreadyExists = validObjects.Any(i => i.Name == itemDto.Name);
                if (!IsValid(itemDto) || alreadyExists)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var item     = Mapper.Map <Item>(itemDto);
                var category = GetItemCategory(context, itemDto.Category);
                item.Category = category;

                validObjects.Add(item);
                sb.AppendLine(string.Format(SuccessMessage, item.Name));
            }

            context.AddRange(validObjects);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
示例#4
0
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserializedItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var validCategories = new List <Category>();

            var validItems = new List <Item>();

            foreach (var itemDto in deserializedItems)
            {
                if (!IsValid(itemDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var categoryExists = validCategories.Any(c => c.Name == itemDto.Category);

                var itemExists = validItems.Any(i => i.Name == itemDto.Name);

                var category = new Category();

                if (!categoryExists)
                {
                    category.Name = itemDto.Category;
                    validCategories.Add(category);
                }
                else
                {
                    category = validCategories.FirstOrDefault(c => c.Name == itemDto.Category);
                }

                if (itemExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }
                var item = new Item()
                {
                    Name     = itemDto.Name,
                    Price    = itemDto.Price,
                    Category = category
                };
                validItems.Add(item);
                sb.AppendLine(string.Format(SuccessMessage, itemDto.Name));
            }
            if (validCategories.Count > 0)
            {
                context.Categories.AddRange(validCategories);
            }
            context.AddRange(validItems);

            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
示例#5
0
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var desirializedItems = JsonConvert.DeserializeObject <ItemDTO[]>(jsonString);

            StringBuilder sb = new StringBuilder();

            List <Item> items = new List <Item>();

            foreach (ItemDTO itemDTO in desirializedItems)
            {
                if (!IsValid(itemDTO))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                if (items.Any(i => i.Name == itemDTO.Name))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Category category = context.Categories.FirstOrDefault(c => c.Name == itemDTO.Category);

                if (category == null)
                {
                    category = new Category
                    {
                        Name = itemDTO.Category
                    };

                    context.Categories.Add(category);
                    context.SaveChanges();
                }

                Item item = new Item
                {
                    Category = category,
                    Name     = itemDTO.Name,
                    Price    = itemDTO.Price
                };

                items.Add(item);

                sb.AppendLine(string.Format(SuccessMessage, item.Name));
            }

            context.AddRange(items);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var jsonItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var items = new List <Item>();

            foreach (var item in jsonItems)
            {
                if (!IsValid(item))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                string itemName = item.Name;

                if (items.Any(n => n.Name == itemName))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Category category = items.FirstOrDefault(c => c.Category.Name == item.Category)?.Category;

                if (category == null)
                {
                    category = new Category
                    {
                        Name = item.Category
                    };
                }

                Item currentItem = new Item
                {
                    Name     = itemName,
                    Price    = item.Price,
                    Category = category
                };

                items.Add(currentItem);
                sb.AppendLine(String.Format(SuccessMessage, currentItem.Name));
            }

            context.AddRange(items);
            context.SaveChanges();

            string result = sb.ToString();

            return(result);
        }
示例#7
0
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            StringBuilder sb = new StringBuilder();

            ItemDto[] jsonItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            List <Item> items = new List <Item>();

            foreach (var itemDto in jsonItems)
            {
                if (!IsValid(itemDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                bool isValidItemName = items.Any(x => x.Name == itemDto.Name);

                if (isValidItemName)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Category category = context.Categories.SingleOrDefault(x => x.Name == itemDto.Category);

                if (category == null)
                {
                    category = new Category()
                    {
                        Name = itemDto.Category,
                    };

                    context.Add(category);
                    context.SaveChanges();
                }

                var employee = new Item()
                {
                    Name     = itemDto.Name,
                    Price    = itemDto.Price,
                    Category = category,
                };

                items.Add(employee);
                sb.AppendLine(string.Format(SuccessMessage, itemDto.Name));
            }

            context.AddRange(items);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            var employeesJson = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString);

            var str       = new StringBuilder();
            var employees = new List <Employee>();

            foreach (var e in employeesJson)
            {
                if (e.Age == null)
                {
                    str.AppendLine(FailureMessage);
                    continue;
                }
                var parsedAge = int.Parse(e.Age);

                if (e.Name.Length < 3 || e.Name.Length > 30 || parsedAge > 80 || parsedAge < 15 ||
                    e.Position.Length > 30 || e.Position.Length < 3 ||
                    e.Position == null || e.Name == null)
                {
                    str.AppendLine(FailureMessage);
                    continue;
                }
                var position = employees.FirstOrDefault(x => x.Position.Name == e.Position)?.Position;

                if (position == null)
                {
                    position = new Position()
                    {
                        Name = e.Position
                    };
                }

                var employee = new Employee()
                {
                    Name     = e.Name,
                    Age      = parsedAge,
                    Position = position
                };

                employees.Add(employee);
                str.AppendLine(string.Format(SuccessMessage, e.Name));
            }
            context.AddRange(employees);
            context.SaveChanges();

            return(str.ToString());
        }
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserializedEmployeeDTOs = JsonConvert.DeserializeObject <EmployeeDTO[]>(jsonString);

            var employees = new List <Employee>();

            foreach (var employeeDTO in deserializedEmployeeDTOs)
            {
                if (!IsValid(employeeDTO))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var position = context.Positions.SingleOrDefault(p => p.Name.Equals(employeeDTO.Position));

                if (position == null)
                {
                    position = new Position
                    {
                        Name = employeeDTO.Position,
                    };

                    context.Positions.Add(position);
                    context.SaveChanges();
                }

                var employee = new Employee
                {
                    Name     = employeeDTO.Name,
                    Age      = employeeDTO.Age,
                    Position = position
                };

                employees.Add(employee);

                sb.AppendLine(String.Format(SuccessMessage, employeeDTO.Name));
            }

            context.AddRange(employees);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            var employeesDto = JsonConvert.DeserializeObject <List <EmployeeDto> >(jsonString);

            StringBuilder sb = new StringBuilder();

            List <Employee> employees = new List <Employee>();
            List <Position> positions = new List <Position>();

            foreach (var e in employeesDto)
            {
                if (!IsValid(e))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var position = context.Positions.FirstOrDefault(x => x.Name == e.Position);

                if (position == null)
                {
                    if (positions.Any(x => x.Name == e.Position))
                    {
                        position = positions.FirstOrDefault(x => x.Name == e.Position);
                    }
                    else
                    {
                        position = new Position {
                            Name = e.Position
                        };
                        positions.Add(position);
                    }
                }

                var employee = new Employee {
                    Name = e.Name, Age = e.Age, Position = position
                };

                employees.Add(employee);
                sb.AppendLine($"Record {e.Name} successfully imported.");
            }

            context.AddRange(employees);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
示例#11
0
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            var desirializedEmployees = JsonConvert.DeserializeObject <EmployeeDTO[]>(jsonString);

            StringBuilder sb = new StringBuilder();

            List <Employee> employees = new List <Employee>();

            foreach (EmployeeDTO employeeDTO in desirializedEmployees)
            {
                if (!IsValid(employeeDTO))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Position position = context.Positions.FirstOrDefault(p => p.Name == employeeDTO.Position);

                if (position == null)
                {
                    position = new Position
                    {
                        Name = employeeDTO.Position
                    };

                    context.Positions.Add(position);
                    context.SaveChanges();
                }

                Employee employee = new Employee
                {
                    Name     = employeeDTO.Name,
                    Age      = employeeDTO.Age,
                    Position = position
                };

                employees.Add(employee);

                sb.AppendLine(string.Format(SuccessMessage, employee.Name));
            }

            context.AddRange(employees);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserialisedObjects = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var validItems = new List <Item>();

            foreach (var obj in deserialisedObjects)
            {
                if (!IsValid(obj) || validItems.Any(p => p.Name == obj.Name))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var category = context.Categories.SingleOrDefault(p => p.Name == obj.Category);

                if (category == null)
                {
                    category = new Category()
                    {
                        Name = obj.Category
                    };

                    context.Categories.Add(category);

                    context.SaveChanges();
                }

                var item = new Item()
                {
                    Name     = obj.Name,
                    Price    = obj.Price,
                    Category = category
                };

                validItems.Add(item);
                sb.AppendLine(String.Format(SuccessMessage, item.Name));
            }

            context.AddRange(validItems);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var deserializeItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);
            var validItems       = new List <Item>();
            var sb = new StringBuilder();

            foreach (var itemDto in deserializeItems)
            {
                var isValidItem      = IsValid(itemDto);
                var itemAlreadyExist = validItems.Any(i => i.Name == itemDto.Name);

                if (!isValidItem || itemAlreadyExist)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var category = validItems
                               .FirstOrDefault(c => c.Category.Name == itemDto.Category)
                               ?.Category;

                if (category == null)
                {
                    category = new Category {
                        Name = itemDto.Category
                    };
                }

                var currentItem = new Item
                {
                    Name     = itemDto.Name,
                    Price    = itemDto.Price,
                    Category = category
                };

                validItems.Add(currentItem);
                sb.AppendLine(string.Format(SuccessMessage, currentItem.Name));
            }

            context.AddRange(validItems);
            context.SaveChanges();

            var result = sb.ToString().TrimEnd();

            return(result);
        }
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var deserialisedEmployees = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString);

            var validEmployees = new List <Employee>();

            foreach (var deserialised in deserialisedEmployees)
            {
                if (!IsValid(deserialised))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var position = context.Positions.Where(p => p.Name == deserialised.Position).FirstOrDefault();

                if (position == null)
                {
                    position = new Position()
                    {
                        Name = deserialised.Position
                    };
                    context.Positions.Add(position);
                    context.SaveChanges();
                }

                var employee = new Employee()
                {
                    Name     = deserialised.Name,
                    Age      = deserialised.Age,
                    Position = position
                };

                validEmployees.Add(employee);

                sb.AppendLine(String.Format(SuccessMessage, deserialised.Name));
            }

            context.AddRange(validEmployees);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
示例#15
0
        public static string ImportEmployees(FastFoodDbContext context, string jsonString)
        {
            StringBuilder sb = new StringBuilder();

            EmployeeDto[] jsonEmployees = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString);

            List <Employee> employees = new List <Employee>();

            foreach (var employeeDto in jsonEmployees)
            {
                if (!IsValid(employeeDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Position position = context.Positions.SingleOrDefault(x => x.Name == employeeDto.Position);

                if (position == null)
                {
                    position = new Position()
                    {
                        Name = employeeDto.Position,
                    };

                    context.Add(position);
                    context.SaveChanges();
                }

                var employee = new Employee()
                {
                    Name     = employeeDto.Name,
                    Age      = employeeDto.Age,
                    Position = position,
                };

                employees.Add(employee);
                sb.AppendLine(string.Format(SuccessMessage, employeeDto.Name));
            }

            context.AddRange(employees);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
示例#16
0
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var itemsJson = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var sb = new StringBuilder();

            var validItems = new HashSet <Item>();

            foreach (var itemDto in itemsJson)
            {
                if (!IsValid(itemDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var itemExist = validItems.Any(i => i.Name == itemDto.Name);

                if (itemExist == true)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Category category = GetCategory(context, itemDto.Category);

                var item = new Item
                {
                    Name     = itemDto.Name,
                    Category = category,
                    Price    = itemDto.Price
                };

                validItems.Add(item);
                sb.AppendLine(string.Format(SuccessMessage, item.Name));
            }

            context.AddRange(validItems);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var deserializedItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            List <Item> items = new List <Item>();

            var sb = new StringBuilder();

            foreach (var item in deserializedItems)
            {
                if (!IsValid(item))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                if (items.Any(i => i.Name == item.Name))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                Category category = GetOrCreateCategory(context, item.Category);

                var newItem = new Item()
                {
                    Name     = item.Name,
                    Price    = item.Price,
                    Category = category
                };

                items.Add(newItem);

                sb.AppendLine(string.Format(SuccessMessage, newItem.Name));
            }

            context.AddRange(items);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportOrders(FastFoodDbContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(OrderDto[]), new XmlRootAttribute("Orders"));
            var orderDtos  = (OrderDto[])serializer.Deserialize(new StringReader(xmlString));

            var sb           = new StringBuilder();
            var validObjects = new List <Order>();

            foreach (var orderDto in orderDtos)
            {
                var orderItemsAreValid = orderDto.Items.All(IsValid);

                if (!IsValid(orderDto) || !orderItemsAreValid)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var employee = context.Employees.SingleOrDefault(e => e.Name == orderDto.Employee);
                if (employee == null)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var orderItemNames = orderDto.Items
                                     .Select(i => i.Name)
                                     .ToArray();

                var validItemNames = context.Items
                                     .Where(i => orderItemNames.Contains(i.Name))
                                     .Select(i => i.Name)
                                     .ToArray();

                if (orderItemNames.Length < validItemNames.Length)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var order = Mapper.Map <Order>(orderDto);

                var orderItems = new List <OrderItem>();
                foreach (var orderItemDto in orderDto.Items)
                {
                    var item      = context.Items.SingleOrDefault(i => i.Name == orderItemDto.Name);
                    var orderItem = new OrderItem
                    {
                        Item     = item,
                        Quantity = orderItemDto.Quantity,
                        Order    = order,
                    };
                    orderItems.Add(orderItem);
                }

                order.Employee   = employee;
                order.OrderItems = orderItems;

                validObjects.Add(order);
                sb.AppendLine($"Order for {order.Customer} on {order.DateTime.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture)} added");
            }

            context.AddRange(validObjects);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
        public static string ImportOrders(FastFoodDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary <string, Employee> employees = context.Employees.ToDictionary(e => e.Name);
            Dictionary <string, Item>     items     = context.Items.ToDictionary(e => e.Name);

            XDocument xmlOrdersDoc         = XDocument.Parse(xmlString);
            IEnumerable <XElement> xOrders = xmlOrdersDoc.Root.Elements();

            foreach (XElement xOrder in xOrders)
            {
                string employeeName = xOrder.Element("Employee")?.Value;
                string customerName = xOrder.Element("Customer")?.Value;

                if (string.IsNullOrWhiteSpace(customerName))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                if (string.IsNullOrWhiteSpace(employeeName))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                if (!employees.ContainsKey(employeeName))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                string dateTime = xOrder.Element("DateTime")?.Value;
                if (!DateTime.TryParseExact(dateTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime orderDateTime))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                string price = xOrder.Element("TotalPrice")?.Value;

                if (!string.IsNullOrWhiteSpace(price))
                {
                    bool parsed = decimal.TryParse(price, out decimal parsedPrice);

                    if (!parsed)
                    {
                        sb.AppendLine(FailureMessage);
                        continue;
                    }

                    if (parsedPrice < 0.01m)
                    {
                        sb.AppendLine(FailureMessage);
                        continue;
                    }
                }

                var XorderItems = xOrder.Element("Items")?.Elements();

                if (XorderItems == null)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                bool                   nonExistantOrInvalidItem = false;
                List <Item>            orderItems     = new List <Item>();
                Dictionary <Item, int> itemQuantities = new Dictionary <Item, int>();

                foreach (var item in XorderItems)
                {
                    string itemName     = item.Element("Name")?.Value;
                    string itemQuantity = item.Element("Quantity")?.Value;

                    if (string.IsNullOrWhiteSpace(itemName) || string.IsNullOrWhiteSpace(itemQuantity))
                    {
                        nonExistantOrInvalidItem = true;
                        break;
                    }

                    if (!items.ContainsKey(itemName))
                    {
                        nonExistantOrInvalidItem = true;
                        break;
                    }

                    if (!int.TryParse(itemQuantity, out int parsedQuantity))
                    {
                        nonExistantOrInvalidItem = true;
                        break;
                    }

                    if (parsedQuantity < 1)
                    {
                        nonExistantOrInvalidItem = true;
                        break;
                    }

                    orderItems.Add(items[itemName]);

                    itemQuantities.Add(items[itemName], parsedQuantity);
                }

                if (nonExistantOrInvalidItem)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                string orderTypeString = xOrder.Element("Type")?.Value;

                OrderType orderType = OrderType.ForHere;
                Enum.TryParse(orderTypeString, out orderType);

                Employee currentEmployee = employees[employeeName];

                Order newOrder = new Order()
                {
                    Customer = customerName,
                    Employee = currentEmployee,
                    DateTime = orderDateTime,
                    Type     = orderType
                };

                List <OrderItem> orderItemsList = new List <OrderItem>();
                foreach (var item in orderItems)
                {
                    OrderItem orderItem = new OrderItem()
                    {
                        Item     = item,
                        Order    = newOrder,
                        Quantity = itemQuantities[item]
                    };

                    orderItemsList.Add(orderItem);
                }

                context.Orders.Add(newOrder);
                context.SaveChanges();
                context.AddRange(orderItemsList);

                string dateOutput = newOrder.DateTime.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                sb.AppendLine($"Order for {newOrder.Customer} on {dateOutput} added");
                // Order for Garry on 21/08/2017 13:22 added
            }

            context.SaveChanges();

            string result = sb.ToString();

            return(result);
        }
示例#20
0
        public static string ImportOrders(FastFoodDbContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(OrderDto[]), new XmlRootAttribute("Orders"));

            var deserializer = (OrderDto[])serializer.Deserialize(new StringReader(xmlString));

            var sb = new StringBuilder();

            var validOrders = new HashSet <Order>();
            var orderItems  = new HashSet <OrderItem>();

            foreach (var dto in deserializer)
            {
                bool isValidItem = true;

                if (!IsValid(dto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                foreach (var item in dto.ItemOrderDto)
                {
                    if (!IsValid(item))
                    {
                        sb.AppendLine(FailureMessage);
                        isValidItem = false;
                        break;
                    }
                }

                if (!isValidItem)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var employee = context.Employees.FirstOrDefault(n => n.Name == dto.Employee);

                if (employee == null)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var areItemValid = AreItemValid(context, dto.ItemOrderDto);

                if (!areItemValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var date = DateTime.ParseExact(dto.DateTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                var type = Enum.Parse <OrderType>(dto.Type);

                var order = new Order
                {
                    Customer = dto.Customer,
                    Employee = employee,
                    DateTime = date,
                    Type     = type
                };

                validOrders.Add(order);

                foreach (var itemDto in dto.ItemOrderDto)
                {
                    var item = context.Items.FirstOrDefault(n => n.Name == itemDto.Name);

                    var orderItem = new OrderItem
                    {
                        Order    = order,
                        Item     = item,
                        Quantity = itemDto.Quantity
                    };

                    orderItems.Add(orderItem);
                }
                sb.AppendLine($"Order for {dto.Customer} on {date.ToString("dd/MM/yyyy HH:mm",CultureInfo.InvariantCulture)} added");
            }
            context.AddRange(validOrders);
            context.SaveChanges();

            context.AddRange(orderItems);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
示例#21
0
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var sb = new StringBuilder();

            ItemDto[] itemsDtos = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);

            var items      = new List <Item>();
            var categories = new HashSet <Category>();

            foreach (var itemsDto in itemsDtos)
            {
                int? categoryId   = context.Categories.SingleOrDefault(p => p.Name == itemsDto.Category)?.Id;
                bool isDuplicated = categories.Any(p => p.Name == itemsDto.Category);

                if (!IsValid(itemsDto))
                {
                    continue;
                }
                if (isDuplicated)
                {
                    continue;
                }
                if (categoryId == null)
                {
                    var category = new Category()
                    {
                        Name = itemsDto.Category
                    };
                    categories.Add(category);
                }
            }
            context.AddRange(categories);
            context.SaveChanges();

            foreach (var itemsDto in itemsDtos)
            {
                if (!IsValid(itemsDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var isExist = items.Any(i => i.Name == itemsDto.Name);

                if (isExist)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var item = new Item()
                {
                    Name     = itemsDto.Name,
                    Price    = itemsDto.Price,
                    Category = categories.FirstOrDefault(p => p.Name == itemsDto.Category)
                };
                items.Add(item);
                sb.AppendLine(String.Format(SuccessMessage, item.Name));
            }
            context.AddRange(items);
            context.SaveChanges();
            var result = sb.ToString().Trim();

            return(result);
        }
        public static string ImportItems(FastFoodDbContext context, string jsonString)
        {
            var itemsJson = JsonConvert.DeserializeObject <ItemDto[]>(jsonString);
            var str       = new StringBuilder();
            var items     = new List <Item>();

            foreach (var i in itemsJson)
            {
                if (string.IsNullOrEmpty(i.Name) || string.IsNullOrEmpty(i.Category) || string.IsNullOrEmpty(i.Price))
                {
                    str.AppendLine(FailureMessage);
                    continue;
                }

                var parsedPrice = decimal.Parse(i.Price);
                if (parsedPrice < 0.01m || i.Name.Length < 3 || i.Name.Length > 30 ||
                    i.Category.Length > 30 || i.Category.Length < 3 ||
                    context.Items.Any(x => x.Name == i.Name) ||
                    items.Any(x => x.Name == i.Name))
                {
                    str.AppendLine(FailureMessage);
                    continue;
                }
                Category categorie = new Category();

                if (context.Categories.Any(x => x.Name == i.Category))
                {
                    categorie = context.Categories.Single(x => x.Name == i.Category);
                }
                else if (items.Any(x => x.Category.Name == i.Category))
                {
                    foreach (var a in items)
                    {
                        if (a.Category.Name == i.Category)
                        {
                            categorie.Name = i.Category;
                            categorie      = a.Category;
                            break;
                        }
                    }
                }

                else if (!context.Categories.Any(x => x.Name == i.Category) &&
                         !items.Any(x => x.Name == i.Category))
                {
                    categorie = new Category()
                    {
                        Name = i.Category
                    };
                }


                var item = new Item()
                {
                    Name     = i.Name,
                    Price    = parsedPrice,
                    Category = categorie
                };

                items.Add(item);
                str.AppendLine(string.Format(SuccessMessage, i.Name));
            }

            context.AddRange(items);
            context.SaveChanges();

            return(str.ToString());
        }
示例#23
0
        public static string ImportOrders(FastFoodDbContext context, string xmlString)
        {
            var serializer         = new XmlSerializer(typeof(OrderDto[]), new XmlRootAttribute("Orders"));
            var deserializedOrders = (OrderDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            var sb = new StringBuilder();

            List <Order> orders = new List <Order>();

            foreach (var orderDto in deserializedOrders)
            {
                if (!IsValid(orderDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                if (!orderDto.Items.All(IsValid))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                DateTime date = DateTime.ParseExact(orderDto.DateTime, @"dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

                var employee = context.Employees.SingleOrDefault(x => x.Name == orderDto.EmployeeName);

                if (employee == null)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                OrderType type = OrderType.ForHere;

                type = orderDto.Type;

                Order order = new Order()
                {
                    Customer = orderDto.Customer,
                    DateTime = date,
                    Type     = type,
                    Employee = employee
                };

                OrderItem[] orderItems = orderDto.Items.Select(x => new OrderItem
                {
                    Quantity = x.Quantity,
                    Item     = context.Items.SingleOrDefault(s => s.Name == x.Name),
                    Order    = order
                })
                                         .ToArray();

                var orderItemsAreValid = orderItems.All(oi => oi.Item != null);

                if (!orderItemsAreValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                order.OrderItems = orderItems;

                orders.Add(order);
                sb.AppendLine($"Order for {orderDto.Customer} on {order.DateTime.ToString("dd/MM/yyyy HH:mm")} added");
            }

            context.AddRange(orders);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }