public void UpdateDBEntry(PhonebookEntry entry, PhonebookEntryViewModel entryViewModel)
 {
     entry.FirstName = entryViewModel.FirstName;
     entry.LastName  = entryViewModel.LastName;
     entry.Type      = entryViewModel.Type;
     this.data.PhonebookEntries.Update(entry);
     this.data.SaveChanges();
 }
예제 #2
0
        public IHttpActionResult Create(PhonebookEntryViewModel entryViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }
            var entry = Mapper.Map <PhonebookEntry>(entryViewModel);

            services.CreateDBEntry(entry);
            return(Ok(entryViewModel));
        }
예제 #3
0
        public IHttpActionResult Delete(PhonebookEntryViewModel entryViewModel)
        {
            var entry = this.data.PhonebookEntries.All().FirstOrDefault(p => p.Number == entryViewModel.Number);

            if (entry == null)
            {
                return(BadRequest("No such entry found!"));
            }
            services.DeleteDBEntry(entry);

            return(Ok("Entry deleted!"));
        }
예제 #4
0
        public void Add_InvalidObjectPassed_ReturnsBadRequest()
        {
            // Arrange
            var nameMissingItem = new PhonebookEntryViewModel()
            {
                Name   = "Test",
                Number = "12"
            };

            _controller.ModelState.AddModelError("Name", "Required");

            // Act
            var badResponse = _controller.Post(nameMissingItem);

            // Assert
            Assert.IsType <BadRequestObjectResult>(badResponse.Result);
        }
예제 #5
0
        public IHttpActionResult Update(string number, PhonebookEntryViewModel entryViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            var entry = this.data.PhonebookEntries.All().FirstOrDefault(p => p.Number == number);

            if (entry == null)
            {
                return(BadRequest("No such entry found!"));
            }

            services.UpdateDBEntry(entry, entryViewModel);

            return(Ok("Entry updated!"));
        }
예제 #6
0
        public void Add_ValidObjectPassed_ReturnedResponseHasCreatedItem()
        {
            // Arrange
            var testItem = new PhonebookEntryViewModel()
            {
                Name    = "John",
                Surname = "Black",
                Number  = "22"
            };

            // Act
            var createdResponse = _controller.Post(testItem);

            // Assert
            // var result = createdResponse.Result as OkObjectResult;
            Assert.IsType <OkObjectResult>(createdResponse.Result);
            //Assert.IsType<PhonebookEntryViewModel>(result);
        }
예제 #7
0
        public async Task <ActionResult> EditPhonebookEntry(int phonebookId, int entryId = 0)
        {
            PhonebookEntryViewModel model = new PhonebookEntryViewModel();

            try
            {
                IRestResponse <PhoneBookModel> phonebookResponse = await RestHelper.Get <PhoneBookModel>($"{Settings.Default.PhoneBookApiUrl}/phonebook/GetPhoneBookById",
                                                                                                         new List <KeyValuePair <string, object> >() { new KeyValuePair <string, object>("id", phonebookId) });


                if (phonebookResponse.IsSuccessful)
                {
                    model.PhoneBook = phonebookResponse.Data;

                    if (entryId > 0)
                    {
                        //get child value first
                        IRestResponse <EntryModel> entryResponse = await RestHelper.Get <EntryModel>($"{_apiUrl}/phonebook/GetPhoneBookEntryById",
                                                                                                     new List <KeyValuePair <string, object> >() { new KeyValuePair <string, object>("id", entryId) });

                        if (entryResponse.StatusCode == System.Net.HttpStatusCode.OK)//can use isSuccessful as well
                        {
                            model.Entry = entryResponse.Data;
                        }
                    }
                    else
                    {
                        model.Entry = new EntryModel()
                        {
                            PhoneBookId = phonebookId
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                //log error(no logging framework yet?????)
            }

            return(View(model));
        }
예제 #8
0
        public async Task <ActionResult> Post([FromBody] PhonebookEntryViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var request = _mapper.Map <PhonebookEntryRequestDto>(model);
                    await _repo.AddPhoneBookEntry(request);

                    return(Ok("Inserted successfully!"));
                }
                else
                {
                    return(BadRequest("Failed to insert new phone entry!"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
예제 #9
0
        public async Task <ActionResult> EditPhonebookEntry(PhonebookEntryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    IRestResponse <EntryModel> entitySaveResponse = await RestHelper.Save <EntryModel>($"{_apiUrl}/phonebook/SavePhoneBookEntry", viewModel.Entry);

                    //save the data to the database
                    //add message to success TempData
                    return(RedirectToAction("EditPhonebook", new { id = entitySaveResponse.Data.PhoneBookId }));
                }
                catch (Exception ex)
                {
                    //log the error
                    //issue saving the value
                    return(View(viewModel));
                }
            }
            else
            {
                return(View(viewModel));
            }
        }
 public PhonebookEntryPage(PhoneBookEntry entry)
 {
     InitializeComponent();
     BindingContext = new PhonebookEntryViewModel(entry, Navigation);
 }