Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
0
        private static Category GetCategory(FastFoodDbContext context, string categoryName)
        {
            var category = context.Categories.FirstOrDefault(n => n.Name == categoryName);

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

                context.Add(category);

                context.SaveChanges();
            }

            return(category);
        }
Ejemplo n.º 3
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());
        }
Ejemplo n.º 4
0
        private static Position GetPostion(FastFoodDbContext context, string position)
        {
            var pos = context.Positions.FirstOrDefault(n => n.Name == position);

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

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



            return(pos);
        }