示例#1
0
        public async Task <IActionResult> UpdateImage(Guid id, [FromBody] ImageForUpdateModel imageForUpdate)
        {
            if (imageForUpdate == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var imageFromRepo = await _imagesService.GetImageAsync(id);

            if (imageFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(imageForUpdate, imageFromRepo);
            await _imagesService.UpdateImageAsync(imageFromRepo);

            return(NoContent());
        }
        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"));
        }