public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForUpdate instance var imageForUpdate = new ImageForUpdate() { Title = editImageViewModel.Title }; // serialize it var serializedImageForUpdate = JsonConvert.SerializeObject(imageForUpdate); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PutAsync( $"api/images/{editImageViewModel.Id}", new StringContent(serializedImageForUpdate, System.Text.Encoding.Unicode, "application/json")) .ConfigureAwait(false); return(HandleApiResponse(response, () => RedirectToAction("Index"))); }
public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForUpdate instance var imageForUpdate = new ImageForUpdate() { Title = editImageViewModel.Title }; // serialize it var serializedImageForUpdate = JsonConvert.SerializeObject(imageForUpdate); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PutAsync( $"api/images/{editImageViewModel.Id}", new StringContent(serializedImageForUpdate, 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 <IActionResult> EditImage(Guid id) { var httpClient = _httpClientFactory.CreateClient("APIClient"); var request = new HttpRequestMessage( HttpMethod.Get, $"/api/images/{id}"); var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); response.EnsureSuccessStatusCode(); using (var responseStream = await response.Content.ReadAsStreamAsync()) { var deserializedImage = await JsonSerializer.DeserializeAsync <Image>(responseStream); var editImageViewModel = new EditImageViewModel() { Id = deserializedImage.Id, Title = deserializedImage.Title }; return(View(editImageViewModel)); } }
/* Edit the uploaded image */ public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction("Page404", "Home")); } var dbImage = await db.Images .SingleOrDefaultAsync(i => i.ID == id); if (dbImage == null) { return(RedirectToAction("Page404", "Home")); } // ViewModel used to change only specific details and file is not required var editImage = new EditImageViewModel { ID = dbImage.ID, Name = dbImage.Name, Category = dbImage.Category }; ViewData["Categories"] = new SelectList(GetCatEnumList(), "Value", "Text"); return(View(editImage)); }
public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForUpdate instance var imageForUpdate = new ImageForUpdate() { Title = editImageViewModel.Title }; // serialize it var serializedImageForUpdate = JsonSerializer.Serialize(imageForUpdate); var httpClient = _httpClientFactory.CreateClient("APIClient"); var request = new HttpRequestMessage( HttpMethod.Put, $"/api/images/{editImageViewModel.Id}"); request.Content = new StringContent( serializedImageForUpdate, System.Text.Encoding.Unicode, "application/json"); var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
public string EditCarImage(EditImageViewModel editImageView) { if (CarImageExists(editImageView.CarImageId)) { string imagePath = _imgService.EditImage(editImageView.File, editImageView.ImageToRemove, AppConstant.ImageFolder, _env); CarImage carImageToEdit = _ctx.CarImages.Where(cI => cI.CarImageId == editImageView.CarImageId).Select(cI => new CarImage { Description = editImageView.Description ?? cI.Description, View = editImageView.View ?? cI.View, CarId = cI.CarId, CarImageId = cI.CarImageId, CarImagePath = imagePath ?? cI.CarImagePath }).SingleOrDefault(); try { _ctx.CarImages.Update(carImageToEdit); _ctx.SaveChanges(); } catch (DbUpdateException ex) { _logger.LogInformation($"Could not update file because of {ex.Message}"); } return(imagePath); } return(null); }
public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { var updateImageModel = new UpdateImageModel { Title = editImageViewModel.Title }; var serializedImage = JsonSerializer.Serialize(updateImageModel); var httpClient = _clientFactory.CreateClient("Api"); var request = new HttpRequestMessage( HttpMethod.Put, $"/images/{editImageViewModel.Id}") { Content = new StringContent( serializedImage, Encoding.UTF8, MediaTypeNames.Application.Json) }; var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
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 }; return(View(editImageViewModel)); } else if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (ModelState.IsValid == false) { return(View()); } // create an ImageForUpdate instatnce var imageForUpdate = new ImageForUpdate { Title = editImageViewModel.Title, }; var serializedImageForUpdate = JsonConvert.SerializeObject(imageForUpdate); // call the API var httpClient = imageGalleryHttpClient.GetClient(); var requestUri = $"api/images/{editImageViewModel.Id}"; var content = new StringContent(serializedImageForUpdate, Encoding.Unicode, ApplicationJsonMediaType); var response = await httpClient.PutAsync(requestUri, content); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } throw new Exception($"A problem happend while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> EditImage(Guid id) { var image = await _imageGalleryApiClient.GetImage(id).ConfigureAwait(false); var editImageViewModel = new EditImageViewModel { Id = image.Id, Title = image.Title }; return(View(editImageViewModel)); }
public ActionResult EditImage(string imgUrl, string title) { Image img = ImagesContext.Images.Where(i => i.ImageUrl == imgUrl).FirstOrDefault(); EditImageViewModel edit = new EditImageViewModel(); edit.Title = img.Title; edit.ImageId = img.ImageID; ViewBag.ImageUrl = imgUrl; ViewBag.Title = title; return(PartialView(edit)); }
public void ShouldBeAbleToCreateNewInstance() { // Act var newInstance = new EditImageViewModel { Title = "SomeTitle", Id = Guid.NewGuid(), }; // Assert Assert.NotNull(newInstance); Assert.IsType <EditImageViewModel>(newInstance); }
public ActionResult EditImage(EditImageViewModel edited) { Image img = ImagesContext.Images.Where(i => i.ImageID == edited.ImageId).FirstOrDefault(); if (TryUpdateModel(img)) { img.Title = edited.Title; ImagesContext.SaveChanges(); } return(RedirectToAction("Index")); }
public async void ShouldGetExceptionWhenEditImagePostActionFails() { // Arrange var client = GetMockOfIImageGalleryHttpClient(HttpStatusCode.BadRequest); var controller = new GalleryController(client.Object); var editImageViewModel = new EditImageViewModel(); // Action var result = controller.EditImage(editImageViewModel); // Assert var exception = await Assert.ThrowsAsync <Exception>(() => result); Assert.Equal("A problem happend while calling the API: Because this client's handler always fails", exception.Message); }
public ActionResult EditImage() { var selectedUser = _userRepository.GetBy(u => u.Id == WebUser.Id, "Person"); var vm = new EditImageViewModel { UserId = WebUser.Id }; if (selectedUser != null) { vm.PhotoPath = selectedUser.Person.PhotoPath; } return(View(vm)); }
public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForUpdate instance var imageForUpdate = new ImageForUpdate { Title = editImageViewModel.Title }; await _imageGalleryApiClient.UpdateImage(editImageViewModel.Id, imageForUpdate) .ConfigureAwait(false); return(RedirectToAction("Index")); }
public JsonResult UploadProfileImage(EditImageViewModel vm) { var apiResult = TryExecute(() => { var imageViewModel = new ImageViewModel(); if (vm.Photo != null) { var siteSettings = _settingsService.GetSiteSettings(); var blobUploadService = new BlobUploadService(siteSettings.BlobSettings); var blobPath = blobUploadService.UploadProfileImage(vm.Photo); imageViewModel.SavedName = blobPath; imageViewModel.OriginalName = vm.FileName; } return(imageViewModel); }, "Image uploaded sucessfully"); return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
public ActionResult EditImage(EditImageViewModel vm) { if (vm.Photo != null) { var selectedUser = _userRepository.GetBy(u => u.Id == WebUser.Id, "Person"); if (selectedUser != null) { var siteSettings = _settingsService.GetSiteSettings(); var blobUploadService = new BlobUploadService(siteSettings.BlobSettings); var blobPath = blobUploadService.UploadProfileImage(vm.Photo); selectedUser.Person.PhotoPath = blobPath; _userRepository.Update(selectedUser); _unitOfWork.Commit(); } } return(RedirectToAction("Index")); }
public async void ShouldGetEmptyViewModelWhenModelStateInvalid() { // Arrange var client = GetMockOfIImageGalleryHttpClient(HttpStatusCode.OK); var controller = new GalleryController(client.Object); // Testing controller logic in ASP.NET Core // https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/testing controller.ModelState.AddModelError("Title", "Required"); var editImageViewModel = new EditImageViewModel(); // Act var result = await controller.EditImage(editImageViewModel); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.Model); }
public async Task <IActionResult> EditImage(Guid id) { var httpClient = await _imageGalleryHttpClient.GetHttpClientAsync(); var response = await httpClient.GetAsync($"api/images/{id}"); response.EnsureSuccessStatusCode(); var imageAsString = await response.Content.ReadAsStringAsync(); var deserializedImage = JsonConvert.DeserializeObject <ImageModel>(imageAsString); var editImageViewModel = new EditImageViewModel { Id = deserializedImage.Id, Title = deserializedImage.Title }; return(View(editImageViewModel)); }
public async void ShouldGetRedirectToActionResultWhenEditImagePostHttpMethodSucceeds() { // Arrange var client = GetMockOfIImageGalleryHttpClient(HttpStatusCode.OK); var controller = new GalleryController(client.Object); var editImageViewModel = new EditImageViewModel { Id = Guid.NewGuid(), Title = "New Image Title", }; // Act var response = await controller.EditImage(editImageViewModel); // Assert var result = Assert.IsType <RedirectToActionResult>(response); Assert.Equal("Index", result.ActionName); }
public async Task <IActionResult> EditImage(Guid id) { // call the API var httpClient = imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync($"api/images/{id}"); if (response.IsSuccessStatusCode) { var imageAsString = await response.Content.ReadAsStringAsync(); var image = JsonConvert.DeserializeObject <Image>(imageAsString); var editImageViewModel = new EditImageViewModel { Id = image.Id, Title = image.Title, }; return(View(editImageViewModel)); } throw new Exception($"A problem happend while calling the API: {response.ReasonPhrase}"); }
public ActionResult DeleteCarImage(EditImageViewModel editImageView) { if (ModelState.IsValid) { try { bool imageRemoved = _carImgService.DeleteCarImage(editImageView); if (imageRemoved) { return(Ok(new { Value = "Record and image deleted" })); } return(Ok(new { Value = "Record deleted" })); } catch { return(View()); } } return(View()); }
public IActionResult EditCarImage(EditImageViewModel editImageView) { if (ModelState.IsValid) { try { string imagePath = _carImgService.EditCarImage(editImageView); if (!string.IsNullOrEmpty(imagePath)) { return(Ok(new { Path = imagePath })); } return(Ok(new { Path = "" })); } catch { return(View()); } } return(View()); }
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 <IActionResult> EditImage(EditImageViewModel editImageViewModel) { if (!ModelState.IsValid) { return(View()); } var imageForUpdate = new ImageForUpdateModel { Title = editImageViewModel.Title }; var serializedImageForUpdate = JsonConvert.SerializeObject(imageForUpdate); var httpClient = await _imageGalleryHttpClient.GetHttpClientAsync(); var response = await httpClient.PutAsync( $"api/images/{editImageViewModel.Id}", new StringContent(serializedImageForUpdate, System.Text.Encoding.Unicode, "application/json")); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
public bool DeleteCarImage(EditImageViewModel editImageView) { bool removed = false; if (CarImageExists(editImageView.CarImageId)) { removed = _imgService.DeleteImage(editImageView.ImageToRemove, _env); CarImage carImageToEdit = _ctx.CarImages.Where(cI => cI.CarImageId == editImageView.CarImageId).SingleOrDefault(); try { _ctx.CarImages.Remove(carImageToEdit); _ctx.SaveChanges(); } catch (DbUpdateException ex) { _logger.LogInformation($"Could not update file because of {ex.Message}"); } } return(removed); }
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); return(await HandleApiResponse(response, async() => { var imageAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var deserializedImage = JsonConvert.DeserializeObject <Image>(imageAsString); var editImageViewModel = new EditImageViewModel() { Id = deserializedImage.Id, Title = deserializedImage.Title }; return View(editImageViewModel); })); }
public async Task <IActionResult> EditImage(Guid id) { // call the API var imagesRoute = $"{InternalImagesRoute}/{id}"; var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.GetAsync(imagesRoute).ConfigureAwait(false); _logger.LogInformation($"Call {imagesRoute} return {response.StatusCode}."); 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(Ok(editImageViewModel)); } 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> EditImage(Guid id) { // call the API var httpClient = HttpClientFactory.CreateClient("APIClient"); 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 }; return(View(editImageViewModel)); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }