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 Dictionary <int, string> AddEmployee(string name, string surname, string personalCode, string email) { Dictionary <int, string> response = new Dictionary <int, string>(); bool contains = false; foreach (Employee employee in getEmployees()) //check if collection of employees contains object { //with user typed personal code if (employee.PersonalCode.Equals(personalCode)) { contains = true; } } if (contains) { Console.WriteLine("Error. Employee with this personal code already exists"); response.Add(0, "Employee with that personal code already exists"); return(response); } else //if not, create new employee and add to collection { try { var address = new MailAddress(email); var employee = new Employee(name, surname, personalCode, email); context.Employees.Add(employee); context.SaveChanges(); response.Add(1, "Employee 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); } } }