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(); }
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)); }
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!")); }
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); }
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!")); }
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); }
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)); }
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)); } }
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); }