public static string ImportItems(FastFoodDbContext context, string jsonString) { var deserializeItems = JsonConvert.DeserializeObject <ItemsDto[]>(jsonString); var sb = new StringBuilder(); List <Item> items = new List <Item>(); foreach (var itemDto in deserializeItems) { if (!IsValid(itemDto)) { sb.AppendLine(FailureMessage); continue; } bool itemExist = items.Any(x => x.Name == itemDto.Name); if (itemExist) { sb.AppendLine(FailureMessage); continue; } var caregory = context.Categories.FirstOrDefault(x => x.Name == itemDto.Category); if (caregory == null) { caregory = CreateCategory(context, itemDto.Category); } Item item = new Item() { Name = itemDto.Name, Price = itemDto.Price, Category = caregory }; items.Add(item); sb.AppendLine(String.Format(SuccessMessage, item.Name)); } context.Items.AddRange(items); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { var item = context.Items.SingleOrDefault(i => i.Name == itemName); if (item == null) { return($"Item {itemName} not found!"); } var oldPrice = item.Price; item.Price = newPrice; context.SaveChanges(); return($"{item.Name} Price updated from ${oldPrice:F2} to ${item.Price:F2}"); }
public static string ImportOrders(FastFoodDbContext context, string xmlString) { var serializer = new XmlSerializer(typeof(List <OrderDto>), new XmlRootAttribute("Orders")); var deserializedOrders = (List <OrderDto>)serializer.Deserialize(new StringReader(xmlString)); var sb = new StringBuilder(); var orders = new List <Order>(); foreach (var order in deserializedOrders) { var employeeExists = context.Employees.Any(e => e.Name == order.Employee); var itemExists = context.Items.Any(i => order.Items.Any(oi => oi.Name == i.Name)); var IsValidOrder = IsValid(order); var IsOrderTypeValid = Enum.TryParse(order.Type, out OrderType type); if (!IsValidOrder || !employeeExists || !itemExists || !IsOrderTypeValid) { sb.AppendLine(FailureMessage); continue; } var current = new Order { Customer = order.Customer, DateTime = DateTime.ParseExact(order.DateTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture), Type = (OrderType)Enum.Parse(typeof(OrderType), order.Type, true), Employee = context.Employees.FirstOrDefault(e => e.Name == order.Employee), OrderItems = order.Items.Select(i => new OrderItem { Item = context.Items.FirstOrDefault(it => it.Name == i.Name), Order = context.Orders.FirstOrDefault(o => o.Id == order.Id), Quantity = order.Items.Where(it => it.Name == context.Items.FirstOrDefault(x => x.Name == i.Name).Name).Sum(s => s.Quantity) }).ToList() }; orders.Add(current); sb.AppendLine($"Order for {order.Employee} on {current.DateTime} added"); } context.Orders.AddRange(orders); context.SaveChanges(); return(sb.ToString().Trim()); }
private static Position GetPosition(FastFoodDbContext context, string positionName) { var position = context.Positions.FirstOrDefault(x => x.Name == positionName); if (position == null) { position = new Position { Name = positionName }; context.Positions.Add(position); context.SaveChanges(); } return(position); }
private static Category FindOrCreateCategory(FastFoodDbContext context, ItemDto itemDto) { var category = context.Categories.SingleOrDefault(c => c.Name == itemDto.Category); if (category == null) { var categ = new Category { Name = itemDto.Category }; context.Categories.Add(categ); context.SaveChanges(); } return(category); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { var item = context.Items .FirstOrDefault(x => x.Name == itemName); if (item == null) { return(string.Format(FailUpdate, itemName)); } var oldPrice = item.Price; item.Price = newPrice; context.Items.Update(item); context.SaveChanges(); return(string.Format(SuccesUpdate, itemName, oldPrice, newPrice)); }
private static Position GetEmployeePosition(FastFoodDbContext context, string positinoName) { Position position = context.Positions.SingleOrDefault(p => p.Name == positinoName); if (position == null) { position = new Position { Name = positinoName, }; context.Positions.Add(position); context.SaveChanges(); } return(position); }
private static Category GetItemCategory(FastFoodDbContext context, string categoryName) { Category category = context.Categories.SingleOrDefault(c => c.Name == categoryName); if (category == null) { category = new Category { Name = categoryName, }; context.Categories.Add(category); context.SaveChanges(); } return(category); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var deserializedEmployees = JsonConvert.DeserializeObject <EmployeesDto[]>(jsonString); var sb = new StringBuilder(); var employees = new List <Employee>(); var positions = new List <Position>(); foreach (var dto in deserializedEmployees) { if (!IsValid(dto)) { sb.AppendLine(FailureMessage); continue; } var position = positions.FirstOrDefault(e => e.Name == dto.Position); if (position == null) { position = new Position { Name = dto.Position }; } var employee = new Employee { Name = dto.Name, Age = dto.Age, Position = position }; positions.Add(position); employees.Add(employee); sb.AppendLine(String.Format(SuccessMessage, dto.Name)); } context.Positions.AddRange(positions); context.Employees.AddRange(employees); context.SaveChanges(); var result = sb.ToString(); return(result); }
private static Category GetCategory(FastFoodDbContext context, string categoryName) { var category = context.Categories.FirstOrDefault(x => x.Name == categoryName); if (category == null) { category = new Category { Name = categoryName }; context.Categories.Add(category); context.SaveChanges(); } return(category); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var positions = new List <Position>(); var employees = new List <Employee>(); var result = new StringBuilder(); var objEmployees = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString); foreach (var objEmployee in objEmployees) { if (!IsValid(objEmployee) || objEmployee.Position == "Invalid") { result.AppendLine(FailureMessage); continue; } Position position = null; if (positions.All(p => p.Name != objEmployee.Position)) { position = new Position { Name = objEmployee.Position }; positions.Add(position); } else { position = positions.First(p => p.Name == objEmployee.Position); } var employee = new Employee { Name = objEmployee.Name, Age = objEmployee.Age, Position = position }; employees.Add(employee); result.AppendLine(string.Format(SuccessMessage, employee.Name)); } context.Employees.AddRange(employees); context.SaveChanges(); return(result.ToString().Trim()); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { Item item = context.Items.SingleOrDefault(i => i.Name == itemName); if (item == null) { return(string.Format(FailureMessage, itemName)); } decimal oldPrice = item.Price; item.Price = newPrice; context.SaveChanges(); var result = $"{itemName} Price updated from ${oldPrice:F2} to ${newPrice:F2}"; return(result); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { var exactItem = context.Items.FirstOrDefault(i => i.Name == itemName); if (exactItem == null) { return($"Item {itemName} not found!"); } decimal oldPrice = exactItem.Price; exactItem.Price = newPrice; context.SaveChanges(); string result = $"{itemName} Price updated from ${oldPrice:F2} to ${newPrice:F2}"; return(result); }
private static Category GetCategory(FastFoodDbContext context, ItemDto itemDto) { var searchedCategory = context.Categories.FirstOrDefault(c => c.Name == itemDto.Category); if (searchedCategory == null) { var category = new Category { Name = itemDto.Category }; context.Categories.Add(category); context.SaveChanges(); searchedCategory = category; } return(searchedCategory); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var employeeDtos = JsonConvert.DeserializeObject <EmployeeImportDto[]>(jsonString); StringBuilder builder = new StringBuilder(); List <Employee> employees = new List <Employee>(); HashSet <Position> positionsToAdd = new HashSet <Position>(); foreach (var eDto in employeeDtos) { if (!IsValid(eDto)) { builder.AppendLine(FailureMessage); continue; } Position position = context.Positions.FirstOrDefault(p => p.Name == eDto.Position); position = positionsToAdd.FirstOrDefault(p => p.Name == eDto.Position); if (position == null) { position = new Position { Name = eDto.Position }; positionsToAdd.Add(position); } var employee = AutoMapper.Mapper.Map <Employee>(eDto); employee.Position = position; employees.Add(employee); builder.AppendLine(String.Format(SuccessMessage, employee.Name)); } context.Positions.AddRange(positionsToAdd); context.Employees.AddRange(employees); context.SaveChanges(); return(builder.ToString().TrimEnd()); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var employeeDtos = JsonConvert.DeserializeObject <List <EmployeeImportDto> >(jsonString); var employeesToAdd = new List <Employee>(); var sb = new StringBuilder(); foreach (var employeeDto in employeeDtos) { if (!IsValid(employeeDto)) { sb.AppendLine(FailureMessage); continue; } var position = employeesToAdd .Select(e => e.Position) .FirstOrDefault(p => p.Name == employeeDto.Position); if (position == null) { position = new Position { Name = employeeDto.Position }; } var employee = new Employee { Name = employeeDto.Name, Age = employeeDto.Age, Position = position }; employeesToAdd.Add(employee); sb.AppendLine(string.Format(SuccessMessage, employee.Name)); } context.Employees.AddRange(employeesToAdd); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportItems(FastFoodDbContext context, string jsonString) { var sb = new StringBuilder(); var deserializedItems = JsonConvert.DeserializeObject <ItemDto[]>(jsonString); var items = new List <Item>(); foreach (var itemDto in deserializedItems) { if (!IsValid(itemDto)) { sb.AppendLine(FailureMessage); continue; } var itemExists = items.Any(n => n.Name == itemDto.Name); if (itemExists) { sb.AppendLine(FailureMessage); continue; } Category category = GetCategory(context, itemDto.Category); var item = new Item { Name = itemDto.Name, Price = itemDto.Price, Category = category }; items.Add(item); sb.AppendLine(string.Format(SuccessMessage, item.Name)); } context.Items.AddRange(items); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var employeesDto = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString); var sb = new StringBuilder(); var employeesToAdd = new List <Employee>(); var existingPositions = context.Positions.ToHashSet(); foreach (var dto in employeesDto) { if (!IsValid(dto)) { sb.AppendLine(FailureMessage); continue; } Position position = existingPositions.FirstOrDefault(x => x.Name == dto.Position); if (position == null) { position = new Position() { Name = dto.Position }; } var employee = new Employee() { Name = dto.Name, Age = dto.Age, Position = position }; existingPositions.Add(employee.Position); employeesToAdd.Add(employee); sb.AppendLine(string.Format(SuccessMessage, employee.Name)); } context.Employees.AddRange(employeesToAdd); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var desEmpl = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString); var sb = new StringBuilder(); var validEmployees = new List <Employee>(); var validPositions = new List <Position>(); foreach (var employee in desEmpl) { if (!IsValid(employee)) { sb.AppendLine(FailureMessage); continue; } var entry = new Employee { Age = employee.Age, Name = employee.Name }; var position = validPositions.FirstOrDefault(x => x.Name == employee.Position); if (position == null) { var newPosition = new Position { Name = employee.Position }; validPositions.Add(newPosition); position = newPosition; } entry.Position = position; validEmployees.Add(entry); sb.AppendLine(string.Format(SuccessMessage, employee.Name)); } context.Employees.AddRange(validEmployees); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { var item = context.Items.FirstOrDefault(i => i.Name == itemName); if (item == null) { string errorMsg = $"Item {itemName} not found!"; return(errorMsg); } var oldPrice = item.Price; item.Price = newPrice; context.SaveChanges(); string successMsg = $"{item.Name} Price updated from ${oldPrice:F2} to ${newPrice:F2}"; return(successMsg); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { string result; var item = context.Items.FirstOrDefault(e => e.Name == itemName); var oldPrice = 0m; if (item == null) { result = $"Item {itemName} not found!"; } else { oldPrice = item.Price; item.Price = newPrice; context.SaveChanges(); result = $"{itemName} Price updated from ${oldPrice:F2} to ${newPrice:F2}"; } return(result); }
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); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var deserializedEmployees = JsonConvert.DeserializeObject <EmployeeDto[]>(jsonString); StringBuilder sb = new StringBuilder(); var positions = new HashSet <Position>(); var employees = new List <Employee>(); foreach (var dto in deserializedEmployees) { if (!IsValid(dto)) { sb.AppendLine(FailureMessage); continue; } var position = positions.FirstOrDefault(p => p.Name.Equals(dto.Position, StringComparison.OrdinalIgnoreCase)); if (position == null) { position = new Position(dto.Position); positions.Add(position); } Employee employee = new Employee() { Name = dto.Name, Age = dto.Age, Position = position }; sb.AppendLine(string.Format(SuccessMessage, employee.Name)); employees.Add(employee); } context.Employees.AddRange(employees); context.SaveChanges(); string result = sb.ToString(); return(result); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { StringBuilder sb = new StringBuilder(); var item = context.Items.Where(i => i.Name == itemName).SingleOrDefault(); bool itemExists = context.Items.Any(e => e.Name == itemName); if (!itemExists) { sb.AppendLine($"Item { item.Name} not found!"); } var oldPrice = item.Price; item.Price = newPrice; context.SaveChanges(); sb.AppendLine($"“{item.Name} Price updated from ${oldPrice:F2} to ${newPrice:F2}"); return(sb.ToString()); throw new NotImplementedException(); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var sb = new StringBuilder(); var importEmployeeDtos = JsonConvert.DeserializeObject <ImportEmployeeDto[]>(jsonString); var employees = new List <Employee>(); var positions = new HashSet <Position>(); foreach (var employeeDto in importEmployeeDtos) { var employeeDtoIsValid = IsValid(employeeDto); if (!employeeDtoIsValid) { sb.AppendLine(FailureMessage); continue; } var position = positions.Any(p => p.Name == employeeDto.Position) ? positions.First(p => p.Name == employeeDto.Position) : new Position { Name = employeeDto.Position }; positions.Add(position); var employee = new Employee { Name = employeeDto.Name, Age = employeeDto.Age, Position = position }; sb.AppendLine(String.Format(SuccessMessage, employee.Name)); employees.Add(employee); } context.Employees.AddRange(employees); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportItems(FastFoodDbContext context, string jsonString) { var sb = new StringBuilder(); var items = JsonConvert.DeserializeObject <ItemDtoImport[]>(jsonString); var itemsList = new List <Item>(); var categoriesList = new List <Category>(); foreach (var item in items) { if (!IsValid(item) || itemsList.Any(x => x.Name == item.Name)) { sb.AppendLine(FailureMessage); continue; } var category = categoriesList.SingleOrDefault(x => x.Name == item.Category); if (category == null) { category = new Category() { Name = item.Category }; categoriesList.Add(category); } var newItem = new Item() { Name = item.Name, Price = item.Price, Category = category }; itemsList.Add(newItem); sb.AppendLine(string.Format(SuccessMessage, item.Name)); } context.Items.AddRange(itemsList); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string UpdatePrice(FastFoodDbContext context, string itemName, decimal newPrice) { var result = string.Empty; if (!context.Items.Any(e => e.Name == itemName)) { return(result = $"Item {itemName} not found!"); } var item = context.Items.FirstOrDefault(e => e.Name == itemName); var oldPrice = item.Price; item.Price = newPrice; context.Items.Update(item); context.SaveChanges(); result = $"{itemName} Price updated from ${oldPrice:f2} to ${newPrice}"; return(result); }
private static Position FindOrCreatePosition(FastFoodDbContext context, string employeeDtoPosition) { var position = context .Positions .SingleOrDefault(e => e.Name == employeeDtoPosition); if (position == null) { position = new Position { Name = employeeDtoPosition }; context.Positions.Add(position); context.SaveChanges(); } return(position); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { var sb = new StringBuilder(); var employees = JsonConvert.DeserializeObject <EmployeeDtoImport[]>(jsonString); var employeesList = new List <Employee>(); var positionList = new List <Position>(); foreach (var employee in employees) { if (!IsValid(employee) || employeesList.Any(x => x.Name == employee.Name)) { sb.AppendLine(FailureMessage); continue; } var position = positionList.SingleOrDefault(x => x.Name == employee.Position); if (position == null) { position = new Position() { Name = employee.Position }; positionList.Add(position); } var newEmployee = new Employee() { Name = employee.Name, Age = employee.Age, Position = position }; employeesList.Add(newEmployee); sb.AppendLine(String.Format(SuccessMessage, employee.Name)); } context.Employees.AddRange(employeesList); context.SaveChanges(); var result = sb.ToString(); return(result); }
private static Position GetPosition(string positionName, FastFoodDbContext context) { var position = context.Positions .Where(x => x.Name == positionName).FirstOrDefault(); if (position == null) { var newPosition = new Position() { Name = positionName }; context.Positions.Add(newPosition); context.SaveChanges(); return(newPosition); } return(position); }