[Authorize(Roles = "PayingUser")] /* TEST FREE USER VALIDATION */ public async Task <IActionResult> EditImage([FromBody] EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // create an ImageForUpdate instance var imageForUpdate = new ImageForUpdate { Title = editImageViewModel.Title, Category = editImageViewModel.Category, }; // serialize it var serializedImageForUpdate = JsonConvert.SerializeObject(imageForUpdate); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PutAsync( $"{InternalImagesRoute}/{editImageViewModel.Id}", new StringContent(serializedImageForUpdate, System.Text.Encoding.Unicode, "application/json")) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { return(Ok()); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> Index() { await WriteOutIdentityInformation(); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("api/images").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var galleryIndexViewModel = new GalleryIndexViewModel( JsonConvert.DeserializeObject <IList <Image> >(imagesAsString).ToList()); return(View(galleryIndexViewModel)); } if (response.StatusCode == HttpStatusCode.Unauthorized) { return(RedirectToAction("AccessDenied", "Authorization")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> Index() { if (HttpContext.User == User) { var claims = User.Claims.Select(c => c.Type + ":::" + c.Value).ToList(); var claimsString = string.Join(',', claims); } // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("api/images").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var galleryIndexViewModel = new GalleryIndexViewModel( JsonConvert.DeserializeObject <IList <Image> >(imagesAsString).ToList()); return(View(galleryIndexViewModel)); } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> EditImage(Guid id) { // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync($"api/images/{id}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imageAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var deserializedImage = JsonConvert.DeserializeObject <Image>(imageAsString); var editImageViewModel = new EditImageViewModel() { Id = deserializedImage.Id, Title = deserializedImage.Title, Category = deserializedImage.Category, }; return(View(editImageViewModel)); } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <ActionResult> GalleryIndexViewModel() { await WriteOutIdentityInformation(); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync(InternalImagesRoute).ConfigureAwait(false); _logger.LogInformation($"Call {InternalImagesRoute} return {response.StatusCode}."); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var galleryIndexViewModel = new GalleryIndexViewModel( JsonConvert.DeserializeObject <IList <Image> >(imagesAsString).ToList(), ApplicationSettings.ImagesUri); return(Ok(galleryIndexViewModel)); } switch (response.StatusCode) { case System.Net.HttpStatusCode.Unauthorized: return(Unauthorized()); case System.Net.HttpStatusCode.Forbidden: return(new ForbidResult()); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> Images([FromServices] IImageGalleryHttpClient imageGalleryHttpClient, [FromServices] ImageGalleryConfig config) { //for test await WriteOutIdentityInformation(); var imagesViewModel = new ImagesViewModel(); // call the API var httpClient = await imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("Api/Image/GetImages").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var imageItems = JsonConvert.DeserializeObject <IList <ImageItem> >(imagesAsString).ToList(); foreach (var imageItem in imageItems) { imageItem.Url = config.BaseUri + imageItem.Url; } imagesViewModel.Images = imageItems; } return(View(imagesViewModel)); }
public async Task <IActionResult> Index() { // call the API var httpClient = imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("api/images"); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync(); var images = JsonConvert.DeserializeObject <IList <Image> >(imagesAsString); var galleryIndexViewModel = new GalleryIndexViewModel(images); return(View(galleryIndexViewModel)); } throw new Exception($"A problem happend while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> Index() { await WriteOutIdentityInformation(); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("api/images").ConfigureAwait(false); return(await HandleApiResponse(response, async() => { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var galleryIndexViewModel = new GalleryIndexViewModel( JsonConvert.DeserializeObject <IList <Image> >(imagesAsString).ToList()); return View(galleryIndexViewModel); })); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForCreation instance var imageForCreation = new ImageForCreation() { Title = addImageViewModel.Title }; // take the first (only) file in the Files list var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } // serialize it var serializedImageForCreation = JsonConvert.SerializeObject(imageForCreation); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PostAsync( $"api/images", new StringContent(serializedImageForCreation, System.Text.Encoding.Unicode, "application/json")) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task OnGetAsync(Guid id) { var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync($"api/images/{id}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imageAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var deserializedImage = JsonConvert.DeserializeObject <Image>(imageAsString); this.Edit = new EditImageViewModel(); this.Edit.Id = deserializedImage.Id; this.Edit.Title = deserializedImage.Title; } else { throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); } }
public async Task OnGetAsync() { await WriteOutIdentityInformation(); var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync("api/images").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var imagesAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); Images = JsonConvert.DeserializeObject <IList <Image> >(imagesAsString).ToList(); } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden) { RedirectToPage("AccessDenied"); } else { throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); } }