public IActionResult UpdateImage(Guid id,
                                         [FromBody] ImageForUpdate imageForUpdate)
        {
            var imageFromRepo = _galleryRepository.GetImage(id);

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

            _mapper.Map(imageForUpdate, imageFromRepo);

            _galleryRepository.UpdateImage(imageFromRepo);

            _galleryRepository.Save();

            return(NoContent());
        }
        public IActionResult UpdateImage(Guid id, [FromBody] ImageForUpdate imageForUpdate)
        {
            // This is replaced with: [Authorize("MustOwnImage")]
            //var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            //if (!_galleryRepository.IsImageOwner(id, ownerId))
            //{
            //    return StatusCode(403); // Forbidden
            //}

            if (imageForUpdate == null)
            {
                return(BadRequest());
            }

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

            var imageFromRepo = _galleryRepository.GetImage(id);

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

            Mapper.Map(imageForUpdate, imageFromRepo);

            _galleryRepository.UpdateImage(imageFromRepo);

            if (!_galleryRepository.Save())
            {
                throw new Exception($"Updating image with {id} failed on save.");
            }

            return(NoContent());
        }
        public IActionResult UpdateImage(Guid id,
                                         [FromBody] ImageForUpdate imageForUpdate)
        {
            //var ownerId = User.Claims?.FirstOrDefault(_ => _.Type == "sub")?.Value;

            //if (!_galleryRepository.IsImageOwner(id, ownerId))
            //    return StatusCode(403);

            if (imageForUpdate == null)
            {
                return(BadRequest());
            }

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

            var imageFromRepo = _galleryRepository.GetImage(id);

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

            _mapper.Map(imageForUpdate, imageFromRepo);

            _galleryRepository.UpdateImage(imageFromRepo);

            if (!_galleryRepository.Save())
            {
                throw new Exception($"Updating image with {id} failed on save.");
            }

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

            if (response.StatusCode == System.Net.HttpStatusCode.Forbidden ||
                response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                return(RedirectToAction("AccessDenied", "Authorization"));
            }

            throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}");
        }
 public void UpdateImage(ImageForUpdate value)
 {
     throw new NotImplementedException();
 }