public async Task <IActionResult> Edit(string id, EditUrlModel model) { ShortenedUrlOutputDTO shortened = null; var user = await _userManager.GetUserAsync(User); using (var client = new HttpClient()) { Debug.WriteLine($"==> DESTINO: {Environment.GetEnvironmentVariable("ShortnrApiUrl")}api/v1/shorten/{user.Id}/{id}"); client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("ShortnrApiUrl")); model.Edit.UserId = user.Id; EditUrlInputDTO dto = model.Edit; var content = new StringContent(JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json"); var response = await client.PostAsync($"api/v1/shorten/{user.Id}/{id}", content); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return(RedirectToAction("NotFound", "Home")); } else { ModelState.AddModelError(string.Empty, "API error. An error occurred while editing your URL."); } return(View()); } else { shortened = await response.Content.ReadAsAsync <ShortenedUrlOutputDTO>(); return(RedirectToAction("Details", "Urls", new { @Id = shortened.ShortenedUrl })); } } }
public async Task <IActionResult> Edit(string id) { EditUrlModel shortenedModel = null; var user = await _userManager.GetUserAsync(User); using (var client = new HttpClient()) { client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("ShortnrApiUrl")); var response = await client.GetAsync($"api/v1/shorten/{user.Id}/{id}"); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return(RedirectToAction("NotFound", "Home")); } else { ModelState.AddModelError(string.Empty, "API error. This shortened URL was not found."); } } else { ShortenedUrlOutputDTO dto = await response.Content.ReadAsAsync <ShortenedUrlOutputDTO>(); shortenedModel = new EditUrlModel { Original = dto, Edit = new EditUrlInputDTO() { NewShortened = dto.ShortenedUrl, Expiration = dto.Expiration } }; } } return(View(shortenedModel)); }