예제 #1
0
        public async Task <Domain.Models.Contact> SaveContactAsync(Domain.Models.Contact contact)
        {
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var url         = $"{_settings.ApiRootUri}contacts/";
            var jsonRequest = JsonSerializer.Serialize(contact);
            var jsonContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(url, jsonContent);

            if (response.StatusCode != HttpStatusCode.Created)
            {
                throw new HttpRequestException(
                          $"Invalid status code in the HttpResponseMessage: {response.StatusCode}.");
            }

            var content = await response.Content.ReadAsStringAsync();

            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true,
            };

            contact = JsonSerializer.Deserialize <Domain.Models.Contact>(content, options);
            return(contact);
        }
예제 #2
0
 public async Task <ActionResult <bool> > UpdateContactAsync(Domain.Models.Contact contact)
 {
     try
     {
         Log.Information($"{nameof(ContactController)} - {nameof(UpdateContactAsync)} - Contact: {Newtonsoft.Json.JsonConvert.SerializeObject(contact)}");
         return(await _contactEngine.UpdateContactAsync(contact).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         Log.Error($"Exception occured while updating contact- Exception: {e.Message}");
         return(BadRequest($"Invalid request {e.Message}"));
     }
 }
예제 #3
0
        public async Task <RedirectToActionResult> Add(Domain.Models.Contact contact)
        {
            var savedContact = await _contactService.SaveContactAsync(contact);

            return(RedirectToAction("Details", new { id = savedContact.ContactId }));
        }
예제 #4
0
 public async Task <bool> DeleteContactAsync(Domain.Models.Contact contact)
 {
     return(await DeleteContactAsync(contact.ContactId));
 }