public async Task <IActionResult> UpdateAddress([FromBody] ViewModels.PreviousAddress item, string id) { if (id != null && item.id != null && id != item.id) { return(BadRequest()); } // get the Address MicrosoftDynamicsCRMadoxioPreviousaddress Address = await _dynamicsClient.GetPreviousAddressById(id); if (Address == null) { return(new NotFoundResult()); } MicrosoftDynamicsCRMadoxioPreviousaddress patchAddress = new MicrosoftDynamicsCRMadoxioPreviousaddress(); patchAddress.CopyValues(item); try { await _dynamicsClient.Previousaddresses.UpdateAsync(id, patchAddress); } catch (OdataerrorException odee) { _logger.LogError("Error updating Address"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } Address = await _dynamicsClient.GetPreviousAddressById(id); return(Json(Address.ToViewModel())); }
public async Task <IActionResult> CreateAddress([FromBody] ViewModels.PreviousAddress item) { // for association with current user string userJson = _httpContextAccessor.HttpContext.Session.GetString("UserSettings"); UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(userJson); MicrosoftDynamicsCRMadoxioPreviousaddress address = new MicrosoftDynamicsCRMadoxioPreviousaddress(); // copy received values to Dynamics Application address.CopyValues(item); try { address = _dynamicsClient.Previousaddresses.Create(address); } catch (OdataerrorException odee) { _logger.LogError("Error creating application"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); // fail if we can't create. throw (odee); } MicrosoftDynamicsCRMadoxioPreviousaddress patchAddress = new MicrosoftDynamicsCRMadoxioPreviousaddress(); // set contact and worker associations try { var contact = _dynamicsClient.GetContactById(Guid.Parse(item.contactId)); patchAddress.ContactIdODataBind = _dynamicsClient.GetEntityURI("contacts", item.contactId); var worker = _dynamicsClient.GetWorkerById(Guid.Parse(item.workerId)); patchAddress.WorkerIdODataBind = _dynamicsClient.GetEntityURI("adoxio_workers", item.workerId); await _dynamicsClient.Previousaddresses.UpdateAsync(address.AdoxioPreviousaddressid, patchAddress); } catch (OdataerrorException odee) { _logger.LogError("Error updating application"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); // fail if we can't create. throw (odee); } return(Json(address.ToViewModel())); }
public async Task <IActionResult> CreateAddress([FromBody] ViewModels.PreviousAddress item) { // get the current user. UserSettings userSettings = UserSettings.CreateFromHttpContext(_httpContextAccessor); MicrosoftDynamicsCRMadoxioPreviousaddress address = new MicrosoftDynamicsCRMadoxioPreviousaddress(); // copy received values to Dynamics Application address.CopyValues(item); try { address = _dynamicsClient.Previousaddresses.Create(address); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error creating application"); // fail if we can't create. throw (httpOperationException); } MicrosoftDynamicsCRMadoxioPreviousaddress patchAddress = new MicrosoftDynamicsCRMadoxioPreviousaddress(); // set contact and worker associations try { var contact = _dynamicsClient.GetContactById(Guid.Parse(item.contactId)); patchAddress.ContactIdODataBind = _dynamicsClient.GetEntityURI("contacts", item.contactId); if (item.workerId != null) { var worker = _dynamicsClient.GetWorkerById(Guid.Parse(item.workerId)); patchAddress.WorkerIdODataBind = _dynamicsClient.GetEntityURI("adoxio_workers", item.workerId); } await _dynamicsClient.Previousaddresses.UpdateAsync(address.AdoxioPreviousaddressid, patchAddress); } catch (HttpOperationException httpOperationException) { _logger.LogError(httpOperationException, "Error updating application"); // fail if we can't create. throw (httpOperationException); } return(new JsonResult(address.ToViewModel())); }
/// <summary> /// Convert a given voteQuestion to a ViewModel /// </summary> public static ViewModels.PreviousAddress ToViewModel(this MicrosoftDynamicsCRMadoxioPreviousaddress address) { ViewModels.PreviousAddress result = null; if (address != null) { result = new ViewModels.PreviousAddress(); if (address.AdoxioPreviousaddressid != null) { result.id = address.AdoxioPreviousaddressid; } result.name = address.AdoxioName; result.streetaddress = address.AdoxioStreetaddress; result.city = address.AdoxioCity; result.provstate = address.AdoxioProvstate; result.country = address.AdoxioCountry; result.postalcode = address.AdoxioPostalcode; result.fromdate = address.AdoxioFromdate; result.todate = address.AdoxioTodate; } return(result); }
public static void CopyValues(this MicrosoftDynamicsCRMadoxioPreviousaddress to, ViewModels.PreviousAddress from) { to.AdoxioName = from.name; to.AdoxioStreetaddress = from.streetaddress; to.AdoxioCity = from.city; to.AdoxioProvstate = from.provstate; to.AdoxioCountry = from.country; to.AdoxioPostalcode = from.postalcode; to.AdoxioFromdate = from.fromdate; to.AdoxioTodate = from.todate; }
public async System.Threading.Tasks.Task TestCRUD() { string initialAddress = "645 Tyee Road"; string changedAddress = "123 ChangedAddress Ave"; string service = "contact"; // register and login as our first user var loginUser1 = randomNewUserName("TestServiceCardUser", 6); await ServiceCardLogin(loginUser1, loginUser1); // C - Create //First create the contact var request = new HttpRequestMessage(HttpMethod.Post, $"/api/{service}/worker"); ViewModels.Contact contactVM = new ViewModels.Contact() { firstname = "TestFirst", middlename = "TestMiddle", lastname = "TestLst" }; string jsonString = JsonConvert.SerializeObject(contactVM); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); jsonString = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); contactVM = JsonConvert.DeserializeObject <ViewModels.Contact>(jsonString); // Get the worker request = new HttpRequestMessage(HttpMethod.Get, $"/api/worker/contact/{contactVM.id}"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); var workerVM = JsonConvert.DeserializeObject <List <ViewModels.Worker> >(jsonString).FirstOrDefault(); var addressVM = new ViewModels.PreviousAddress() { streetaddress = initialAddress, contactId = contactVM.id, workerId = workerVM.id }; request = new HttpRequestMessage(HttpMethod.Post, $"/api/PreviousAddress"); jsonString = JsonConvert.SerializeObject(addressVM); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); response = await _client.SendAsync(request); jsonString = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); addressVM = JsonConvert.DeserializeObject <ViewModels.PreviousAddress>(jsonString); // R - Read request = new HttpRequestMessage(HttpMethod.Get, "/api/PreviousAddress/by-contactid/" + contactVM.id); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); var address2 = (JsonConvert.DeserializeObject <List <ViewModels.PreviousAddress> >(jsonString)).FirstOrDefault();; Assert.Equal(address2.id, addressVM.id); // U - Update address2.streetaddress = changedAddress; request = new HttpRequestMessage(HttpMethod.Put, "/api/PreviousAddress/" + address2.id) { Content = new StringContent(JsonConvert.SerializeObject(address2), Encoding.UTF8, "application/json") }; response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // verify that the update persisted. request = new HttpRequestMessage(HttpMethod.Get, "/api/PreviousAddress/by-contactid/" + contactVM.id); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); var address3 = (JsonConvert.DeserializeObject <List <ViewModels.PreviousAddress> >(jsonString)).FirstOrDefault();; Assert.Equal(changedAddress, address3.streetaddress); // D - Delete request = new HttpRequestMessage(HttpMethod.Post, "/api/PreviousAddress/" + addressVM.id + "/delete"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // second delete should return a 404. request = new HttpRequestMessage(HttpMethod.Post, "/api/PreviousAddress/" + addressVM.id + "/delete"); response = await _client.SendAsync(request); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); // should get a 404 if we try a get now. request = new HttpRequestMessage(HttpMethod.Get, "/api/PreviousAddress/by-contactid/" + contactVM.id); response = await _client.SendAsync(request); jsonString = await response.Content.ReadAsStringAsync(); var address4 = (JsonConvert.DeserializeObject <List <ViewModels.PreviousAddress> >(jsonString)).FirstOrDefault(); Assert.Null(address4); await Logout(); }