public Dictionary <int, string> AddCustomer(string name, string surname, string personalCode, string email) { Dictionary <int, string> response = new Dictionary <int, string>(); List <Customer> customers; using (var context = new OrderManagerDbContext()) { customers = context.Customers.Select(x => x).ToList(); } bool contains = false; foreach (var customer in customers) //check if collection of customers contains object { //with user typed personal code if (customer.PersonalCode.Equals(personalCode)) { contains = true; } } if (contains) { Console.WriteLine("Error. Customer with this personal code already exists"); response.Add(0, "Customer with this personal code already exists"); return(response); } else //if not, create new customer and add to collection { try { var address = new MailAddress(email); var customer = new Customer(name, surname, personalCode, email); context.Customers.Add(customer); context.SaveChanges(); response.Add(1, "Customer successfully added"); return(response); } catch (FormatException) { Console.WriteLine("Email address is invalid. Try again"); response.Add(0, "Email address is invalid"); return(response); } catch (DbUpdateException) { Console.WriteLine("Email address is invalid. Try again"); response.Add(0, "Error occurred when saving data in database"); return(response); } catch (Exception) { response.Add(0, "Error occurred. Try again later"); return(response); } } }
public static ApplicationManager Instance() { if (applicationManager == null) { applicationManager = new ApplicationManager(); context = new OrderManagerDbContext(); } return(applicationManager); }