internal static void Seed(BillsPaymentSystemContext context, int count) { for (int i = 0; i < count; i++) { var firstName = TextGenerator.FirstName(); var user = new User() { FirstName = firstName, LastName = TextGenerator.LastName(), Email = EmailGenerator.NewEmail(firstName), Password = TextGenerator.Password(12) }; var result = new List <ValidationResult>(); if (AttributeValidator.IsValid(user, result)) { context.Users.Add(user); } else { Console.WriteLine(string.Join(Environment.NewLine, result)); } } context.SaveChanges(); }
internal static void Seed(BillsPaymentSystemContext context, int count, List <User> users) { for (int i = 0; i < count; i++) { var payment = new PaymentMethod() { User = users[IntGenerator.GenerateInt(0, users.Count - 1)], Type = PaymentType.BankAccount, BankAccount = new BankAccount() { BankAccountId = i, //Balance = PriceGenerator.GeneratePrice(), BankName = TextGenerator.FirstName() + "\'s Bank", SwiftCode = TextGenerator.Password(10) }, BankAccountId = i }; payment.BankAccount.Deposit(PriceGenerator.GeneratePrice()); var result = new List <ValidationResult>(); if (AttributeValidator.IsValid(payment, result)) { context.PaymentMethods.Add(payment); } else { Console.WriteLine(string.Join(Environment.NewLine, result)); } payment = new PaymentMethod() { User = users[IntGenerator.GenerateInt(0, users.Count - 1)], Type = PaymentType.CreditCard, CreditCard = new CreditCard() { CreditCardId = i, ExpirationDate = DateGenerator.FutureDate(), Limit = PriceGenerator.GeneratePrice(), //MoneyOwed = PriceGenerator.GeneratePrice() }, CreditCardId = i }; payment.CreditCard.Withdraw(PriceGenerator.GeneratePrice()); result = new List <ValidationResult>(); if (AttributeValidator.IsValid(payment, result)) { context.PaymentMethods.Add(payment); } else { Console.WriteLine(string.Join(Environment.NewLine, result)); } } context.SaveChanges(); }
public static string ImportOrders(FastFoodDbContext context, string xmlString) { var serializer = new XmlSerializer(typeof(dto_order_Xml[]), new XmlRootAttribute("Orders")); var dtos = (dto_order_Xml[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder sb = new StringBuilder(); List <Order> ordersToBeAdded = new List <Order>(); Item[] menuItems = context.Items.ToArray(); foreach (var dto in dtos) { var newOrder = new Order(); try { newOrder.DateTime = DateTime.ParseExact(dto.DateTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); newOrder.Customer = dto.CustomerName; newOrder.Employee = context.Employees.FirstOrDefault(x => x.Name == dto.EmployeeName); newOrder.Type = Enum.Parse <OrderType>(dto.Type); foreach (var itemDto in dto.Items) { var item = menuItems.FirstOrDefault(x => x.Name == itemDto.Name); if (item is null) { throw new ArgumentException("Item not found!"); } newOrder.OrderItems.Add(new OrderItem() { Quantity = int.Parse(itemDto.QuantityINT), Item = item }); } if (newOrder.Employee is null || !AttributeValidator.IsValid(newOrder) || !newOrder.OrderItems.All(x => AttributeValidator.IsValid(x))) { throw new ArgumentException("Invalid DataMember!"); } ordersToBeAdded.Add(newOrder); sb.AppendLine(string.Format("Order for {0} on {1} added", dto.CustomerName, dto.DateTime)); } catch (Exception) { sb.AppendLine(FailureMessage); } } context.Orders.AddRange(ordersToBeAdded); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportItems(FastFoodDbContext context, string jsonString) { return(""); dto_item_Json[] resultsDTOs = JsonConvert.DeserializeObject <dto_item_Json[]>(jsonString); StringBuilder sb = new StringBuilder(); List <Item> itemsToBeAdded = new List <Item>(); foreach (var dto in resultsDTOs) { var newItem = new Item(); try { newItem.Name = dto.Name; newItem.Price = decimal.Parse(dto.Price); newItem.Category = context.Categories.FirstOrDefault(x => x.Name == dto.Category) ?? itemsToBeAdded.Select(x => x.Category).FirstOrDefault(x => x.Name == dto.Category); if (newItem.Category is null) { newItem.Category = new Category() { Name = dto.Category }; } bool ItemNameFree = context.Items.All(x => x.Name != dto.Name) && itemsToBeAdded.All(x => x.Name != dto.Name); if (!ItemNameFree || !AttributeValidator.IsValid(newItem, newItem.Category)) { throw new ArgumentException("Invalid DataMember!"); } itemsToBeAdded.Add(newItem); sb.AppendLine(string.Format(SuccessMessage, newItem.Name)); } catch (Exception) { sb.AppendLine(FailureMessage); } } context.Items.AddRange(itemsToBeAdded); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportEmployees(FastFoodDbContext context, string jsonString) { dto_employee_Json[] resultsDTOs = JsonConvert.DeserializeObject <dto_employee_Json[]>(jsonString); StringBuilder sb = new StringBuilder(); List <Employee> empsToBeAdded = new List <Employee>(); foreach (var dto in resultsDTOs) { Employee newEmp = new Employee(); try { newEmp.Age = int.Parse(dto.Age); newEmp.Name = dto.Name; newEmp.Position = context.Positions.FirstOrDefault(x => x.Name == dto.Position) ?? empsToBeAdded.Select(x => x.Position).FirstOrDefault(x => x.Name == dto.Position); if (newEmp.Position is null) { newEmp.Position = new Position() { Name = dto.Position }; } if (!AttributeValidator.IsValid(newEmp, newEmp.Position)) { throw new ArgumentException("Invalid DataMember!"); } empsToBeAdded.Add(newEmp); sb.AppendLine(string.Format(SuccessMessage, newEmp.Name)); } catch (Exception) { sb.AppendLine(FailureMessage); } } context.Employees.AddRange(empsToBeAdded); context.SaveChanges(); return(sb.ToString().Trim()); }