public ActionResult DeleteContact(string id, string username)
        {
            var addressBook = new RealAddressBook(username);

            addressBook.Delete(id);

            return RedirectToAction("DisplayAddressBook");
        }
        public ActionResult DisplayAddressBook()
        {
            var username = Request.Cookies["UserId"].Value;

            var addressBook = new RealAddressBook(username);

            var contacts = addressBook.Contacts;

            return View(contacts);
        }
        public void ShouldBeAbleToSaveContactToAddressBook()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567892" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var previousContact = addressBook.Contacts.Last();

            var lastContact = addressBook.Save(customer, username);

            Assert.That(lastContact.FirstName, Is.EqualTo(firstName));
            Assert.That(lastContact.Surname, Is.EqualTo(surname));
            Assert.That(lastContact.TelephoneNumber, Is.EqualTo(telephoneNumber));
            Assert.That(lastContact.Id, Is.GreaterThan(previousContact.Id));
        }
        public void ShouldBeAbleToReadASpecificContactFromAddressBook()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567892" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var lastContact = addressBook.Save(customer, username);

            var expectedResult = addressBook.Read(lastContact.Id);

            Assert.That(expectedResult.FirstName, Is.EqualTo(firstName));
            Assert.That(expectedResult.Surname, Is.EqualTo(surname));
            Assert.That(expectedResult.TelephoneNumber, Is.EqualTo(telephoneNumber));
            Assert.That(expectedResult.Id, Is.EqualTo(lastContact.Id));
        }
        public void ShouldDeleteContactFromAddressBook()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567892" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var contact = addressBook.Save(customer, username);

            var id = contact.Id.ToString();

            addressBook.Delete(id);

            var expectedResult = addressBook.Read(contact.Id);

            Assert.That(expectedResult, Is.EqualTo(null));
        }
        public ActionResult SaveNewContact(string firstName, string surname, string telephoneNumber)
        {
            var username = Request.Cookies["UserId"].Value;

            var addressBook = new RealAddressBook(username);

            var newContact = new Contact(firstName, surname, telephoneNumber);

            var contact = addressBook.Save(newContact, username);

            var createdContactModel = new SavedContactModel(contact, "Contact successfully created!");

            return View("SavedContact", createdContactModel);
        }
        public ActionResult UpdateContact(int id)
        {
            var username = Request.Cookies["UserId"].Value;

            var addressBook = new RealAddressBook(username);

            var contact = addressBook.Read(id);

            return View(contact);
        }
        public ActionResult SearchResult(string searchText)
        {
            var username = Request.Cookies["UserId"].Value;

            var addressBook = new RealAddressBook(username);

            ViewBag.SearchText = searchText;

            var model = addressBook.Search(searchText);

            return View(model);
        }
        public void ShouldUpdateTelephoneNumberForContactInAddressBookWithIdEqualTo1()
        {
            const string username = "******";
            const string firstName = "Joe";
            const string surname = "Bloggs";
            const string telephoneNumber = "Joe";

            var addressBook = new RealAddressBook(username);
            var randomNumber = Guid.NewGuid();

            var contact = new Contact(firstName, surname, telephoneNumber);

            var savedContact = addressBook.Save(contact, username);

            var iD = savedContact.Id;

            var readContact = addressBook.Read(iD);
            readContact.TelephoneNumber = "01733777111" + randomNumber;

            addressBook.Save(readContact, username);

            var expectedResult = addressBook.Read(iD);

            Assert.That(expectedResult.TelephoneNumber, Is.EqualTo(readContact.TelephoneNumber));
        }
        public void ShouldUpdateContactTelephoneNumber()
        {
            const string username = "******";

            var homeController = new HomeController();
            _requestContext = A.Fake<HttpRequestBase>();
            _httpContext = A.Fake<HttpContextBase>();

            var cookie = new HttpCookie("UserId") { Value = username };
            var cookies = new HttpCookieCollection {cookie};

            A.CallTo(() => _httpContext.Request).Returns(_requestContext);
            A.CallTo(() => _requestContext.Cookies).Returns(cookies);

            homeController.ControllerContext =
                new ControllerContext(_httpContext, new RouteData(), homeController);

            const string firstName = "Dave";
            const string surname = "Thapa";
            const string telephoneNumber = "012345789642";

            const string contactsPreviousTelephoneNumber = telephoneNumber;

            var savedResult = (ViewResult)homeController.SaveNewContact(firstName, surname, telephoneNumber);

            var savedContactModel = (SavedContactModel) savedResult.Model;
            var id = savedContactModel.Contact.Id;

            const string newTelephoneNumber = "32145678954";
            homeController.SaveUpdatedContact(id, firstName, surname, newTelephoneNumber);

            var realAddressBook = new RealAddressBook(username);
            var readContact = realAddressBook.Read(id);

            Assert.That(readContact.TelephoneNumber, Is.Not.EqualTo(contactsPreviousTelephoneNumber));
            Assert.That(readContact.TelephoneNumber, Is.EqualTo(newTelephoneNumber));
        }
        public void ShouldReturnTwoContactsWhichFirstnameAndSurnameMatchSearchText()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var name = random.Next().ToString();

            var contact = new Contact(name, "Smith", "01733 333555" + random.Next());
            var contactTwo = new Contact("Joe", name, "01733 444888" + random.Next());

            addressBook.Save(contact, username);
            addressBook.Save(contactTwo, username);

            var searchText = name;
            var searchResult = addressBook.Search(searchText);

            Assert.That(searchResult.Count, Is.EqualTo(2));
        }
        public void ShouldReturnContactUsingSearchByTelephone()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567555" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var lastContact = addressBook.Save(customer, username);

            var expectedResult = addressBook.Read(lastContact.Id);

            var searchedPerson = addressBook.Search(telephoneNumber);

            Assert.That(searchedPerson.Count, Is.EqualTo(1));
            var contact = searchedPerson.Single();

            Assert.That(contact.Id, Is.EqualTo(expectedResult.Id));
            Assert.That(contact.FirstName, Is.EqualTo(expectedResult.FirstName));
            Assert.That(contact.Surname, Is.EqualTo(expectedResult.Surname));
            Assert.That(contact.TelephoneNumber, Is.EqualTo(expectedResult.TelephoneNumber));
        }
        public void ShouldReturnContactBySearchingOnlyTheFirstSetOfCharactersOfATelephoneNumber()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567892" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var lastContact = addressBook.Save(customer, username);

            var expectedResult = addressBook.Read(lastContact.Id);

            const string searchText = "01724";
            var searchResult = addressBook.Search(searchText);

            Assert.That(searchResult.Any(p => p.TelephoneNumber == expectedResult.TelephoneNumber && p.Surname == expectedResult.Surname && p.FirstName == expectedResult.FirstName),Is.True);
        }
        public void ShouldFindContactUsingSearchWithAtLeastThreeCharacters()
        {
            const string username = "******";
            var addressBook = new RealAddressBook(username);

            var random = new Random();

            var firstName = "Amanda" + random.Next();
            var surname = "Jones" + random.Next();
            var telephoneNumber = "01724 567892" + random.Next();

            var customer = new Contact(firstName, surname, telephoneNumber);

            var lastContact = addressBook.Save(customer, username);

            var expectedResult = addressBook.Read(lastContact.Id);

            const string searchText = "ama";
            var searchResult = addressBook.Search(searchText);

            Assert.That(
                searchResult.Any(p => p.Surname == expectedResult.Surname && p.FirstName == expectedResult.FirstName),
                Is.True);
        }