public async Task <ActionResult> AmigosAdicionados(Guid id, CriarAmigosDoAmigoViewModels viewModel) { try { await _amigoApi.PostAmigosDoAmigoAsync(id, viewModel); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <CriarAmigosDoAmigoViewModels> PostAmigosDoAmigoAsync(Guid id, CriarAmigosDoAmigoViewModels criarAmigosDoAmigoViewModel) { var criarAmigosDoAmigoViewModelJson = JsonConvert.SerializeObject(criarAmigosDoAmigoViewModel); var conteudo = new StringContent(criarAmigosDoAmigoViewModelJson, Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync($"https://localhost:44334/api/amigos/{id}/amigosdoamigos", conteudo); if (response.IsSuccessStatusCode) { return(criarAmigosDoAmigoViewModel); } else if (response.StatusCode == HttpStatusCode.UnprocessableEntity) { var responseContent = await response.Content.ReadAsStringAsync(); var listErro = JsonConvert.DeserializeObject <List <string> >(responseContent); criarAmigosDoAmigoViewModel.Errors = listErro; return(criarAmigosDoAmigoViewModel); } return(criarAmigosDoAmigoViewModel); }