public async Task <IActionResult> ShortenLink(OriginalUrlInputDTO model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.GetUserAsync(User); using (var client = new HttpClient()) { OriginalUrlInputDTO dto = new OriginalUrlInputDTO() { UserId = user.Id, OriginalUrl = model.OriginalUrl, Expiration = model.Expiration }; client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("ShortnrApiUrl")); var content = new StringContent(JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json"); var response = await client.PostAsync("api/v1/shorten", content); if (!response.IsSuccessStatusCode) { ModelState.AddModelError(string.Empty, "API error. An error occurred during URL shortening."); return(View()); } else { ShortenedUrlOutputDTO shortened = await response.Content.ReadAsAsync <ShortenedUrlOutputDTO>(); return(RedirectToAction("Details", "Urls", new { @Id = shortened.ShortenedUrl })); } } }
public async Task <IActionResult> Details(string id) { ShortenedUrlOutputDTO shortened = 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 { shortened = await response.Content.ReadAsAsync <ShortenedUrlOutputDTO>(); } } return(View(shortened)); }
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 })); } } }