public async Task <Result <BankAccountViewModel> > CreateAccount(BankAccountDTO account) { var newAccount = new BankAccount() { AccountName = account.AccountName, IBAN = account.IBAN, ContactIds = new List <int>() }; if (account.Contacts != null) { foreach (var contact in account.Contacts) { var result = await _contactsService.CreateContact(contact); if (!result.Success) { return(Result.Fail <BankAccountViewModel>(result.StatusCode, result.Error)); } newAccount.ContactIds.Add(contact.Id); } } _context.BankAccounts.Add(newAccount); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(Result.Fail <BankAccountViewModel>(HttpStatusCode.InternalServerError, "Unexpected server error while saving new account")); } var viewModel = new BankAccountViewModel(newAccount); viewModel.Contacts = account.Contacts != null ? account.Contacts : new List <Contact>(); return(Result.Ok(viewModel)); }
public ActionResult Create([Bind(Include = "ContactID,ContactName,PhoneNumber,BirthDate,ContactType,Description")] ContactsViewModel contact) { if (ModelState.IsValid) { _contactsService.CreateContact(contact); return(RedirectToAction("Index")); } ViewBag.ContactType = new SelectList(_db.ContactTypes, "TypeID", "TypeName", contact.ContactType); return(View(contact)); }
public ActionResult ImportFile(IFormFile file) { try { if (file == null) { return(Json("No file detected.")); } List <string> csvContents = new List <string>(); using (var reader = new StreamReader(file.OpenReadStream())) { while (reader.Peek() >= 0) { csvContents.Add(reader.ReadLine()); } } csvContents.RemoveAll(i => String.IsNullOrEmpty(i)); // remove empty rows Regex csvSplit = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); List <string> headers = csvSplit.Split(csvContents[0]).Select(o => o.Trim('"')).ToList(); csvContents.RemoveAt(0); // remove header row List <string> rowDataSplit; List <Contact> importData = new List <Contact>(); while (csvContents.Count > 0) { rowDataSplit = csvSplit.Split(csvContents[0]).ToList(); // todo: make import support multiple email addresses Contact newImportContact = new Contact() { FirstName = rowDataSplit[headers.IndexOf("FirstName")].ToString().Trim('"'), LastName = rowDataSplit[headers.IndexOf("LastName")].ToString().Trim('"'), EmailAddresses = new List <Email> { new Email() { Address = rowDataSplit[headers.IndexOf("Email")].ToString().Trim('"'), Type = (Email.EmailAddressType)Enum.Parse(typeof(Email.EmailAddressType), rowDataSplit[headers.IndexOf("EmailType")].ToString().Trim('"')) } } }; importData.Add(newImportContact); csvContents.RemoveAt(0); } foreach (Contact contact in importData) { _contacts.CreateContact(contact); _contacts.UpdateDisplayName(contact); } return(Json(true)); } catch (Exception ex) { return(Json("Something went wrong. Check your data and try again.")); } }
public void ProcessInput(int input) { switch (input) { case 1: { PrintMessage(Constants.NameMessage); string name = Console.ReadLine(); PrintMessage(Constants.LastNameMessage); string lastName = Console.ReadLine(); List <string> phones = GetPhoneList(); PrintMessage(Constants.AddressMessage); string address = Console.ReadLine(); if (address.Equals(string.Empty)) { address = " "; } if (name.Equals(string.Empty) || lastName.Equals(string.Empty)) { PrintMessage(Constants.InvalidParameterMessage); break; } try { _contactsService.CreateContact(name, lastName, phones, address); } catch { Console.WriteLine(Constants.InvalidParameterMessage); } string data = _contactsService.GetContactsData(); _databaseService.UpdateFile(data); break; } case 2: { PrintMessage(Constants.UpdateMessage); int contactId = int.Parse(Console.ReadLine()); Contact contact = _contactsService.GetContactById(contactId); if (contact == null) { PrintMessage(Constants.IdNotFoundMessage); break; } PrintMessage(Constants.NameMessage); string name = Console.ReadLine(); PrintMessage(Constants.LastNameMessage); string lastName = Console.ReadLine(); if (name.Equals(string.Empty) || lastName.Equals(string.Empty)) { PrintMessage(Constants.InvalidParameterMessage); break; } List <string> phones = GetPhoneList(); PrintMessage(Constants.AddressMessage); string address = Console.ReadLine(); _contactsService.UpdateContact(contact, name, lastName, phones, address); string data = _contactsService.GetContactsData(); _databaseService.UpdateFile(data); break; } case 3: { PrintMessage(Constants.DeleteMessage); int contactId = int.Parse(Console.ReadLine()); Contact contact = _contactsService.GetContactById(contactId); if (contact == null) { PrintMessage(Constants.IdNotFoundMessage); break; } _contactsService.DeleteContact(contact); string data = _contactsService.GetContactsData(); _databaseService.UpdateFile(data); break; } case 4: { List <Contact> contacts = _contactsService.ContactList; PrintAllContacts(contacts); break; } } }