public async Task <ResponseModel <PhoneBookOutputModel> > Post([FromBody] PhoneBookInputModel phoneBookModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _logger.LogInformation($"Creating new phonebook: {phoneBookModel.Name}");
                    var phonebook = await _phoneBookService.Create(phoneBookModel);

                    return(phonebook);
                }
                else
                {
                    return(new ResponseModel <PhoneBookOutputModel>
                    {
                        IsSuccessful = false,
                        ResponseMessage = "Please make sure you have entered the correct details."
                    });
                }
            }
            catch (Exception exception)
            {
                _logger.LogError($"An exception occurred: {exception.ToString()}");
                return(new ResponseModel <PhoneBookOutputModel>
                {
                    ResponseMessage = "An unexpected error occcurred",
                    IsSuccessful = false
                });
            }
        }
        public async void ShouldCreatePhoneBook()
        {
            var newPhoneBook = await _phoneBookService.Create(new Models.PhoneBookInputModel {
                Name = "PhoneBook 3"
            });

            Assert.True(newPhoneBook.IsSuccessful);
        }
Exemplo n.º 3
0
        public ActionResult Add(PhonebookEditViewModel phonebookEditViewModel)
        {
            var contact = new PhoneBook()
            {
                Name        = phonebookEditViewModel.FirstName,
                Surname     = phonebookEditViewModel.LastName,
                CreatedDate = DateTime.Now
            };

            _PhoneBookService.Create(contact);

            var Id = contact.Id;

            if (Id == 0)
            {
                log.Error("Contact not saved. Please try again later!");
                Danger("Contact not saved. Please try again later!");
                return(View("PhoneBookView", phonebookEditViewModel));
            }

            if (phonebookEditViewModel.Numbers.Count() > 0)
            {
                foreach (var entry in phonebookEditViewModel.Numbers)
                {
                    var phonebookEntry = new PhoneBookEntry()
                    {
                        PhoneBookId     = Id,
                        PhoneBookTypeId = Convert.ToInt32(entry.PhoneBookTypeId),
                        Number          = entry.Number
                    };

                    _PhoneBookEntryService.Create(phonebookEntry);
                    Id = phonebookEntry.Id;

                    if (Id == 0)
                    {
                        log.Error($"Contact number {entry.Number} not saved {phonebookEditViewModel.FirstName}!");

                        Danger($"Contact number {entry.Number} not saved {phonebookEditViewModel.FirstName}!");

                        return(View("PhoneBookView", phonebookEditViewModel));
                    }
                }
            }

            Success(string.Format("<b>{0}</b> was successfully saved to the Phonebook.", phonebookEditViewModel.FirstName), true);

            PhonebookHub.BroadcastData();

            return(RedirectToAction("Index", "Phonebook"));
        }