public async Task <ActionResult <GalleryDTO> > PutGallery(Guid galleryId, GalleryPutDTO galleryPutDTO)
        {
            Guid userId = new Guid(HttpContext.User.Identity.Name);

            if (await _galleryService.DoesGalleryExistAsync(galleryId) == false)
            {
                return(NotFound());
            }

            if (await _galleryService.IsGalleryOwnedByUserAsync(galleryId, userId) == false)
            {
                return(Unauthorized());
            }

            try
            {
                GalleryDTO galleryDto = await _galleryService.PutGalleryAsync(userId, galleryId, galleryPutDTO);

                return(CreatedAtAction(nameof(GetGallery), new { id = galleryDto.Id }, galleryDto));
            }
            catch (Exception ex)
            {
                var problemDetails = new ProblemDetails
                {
                    Title    = "An unexpected error occurred.",
                    Status   = StatusCodes.Status500InternalServerError,
                    Detail   = "Unable to update the gallery at this moment due to an error, the error has been logged and sent to the developers for fixing.",
                    Instance = HttpContext.TraceIdentifier,
                };
                return(StatusCode(StatusCodes.Status500InternalServerError, problemDetails));
            }
        }
        public async Task PutGallery_not_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            GalleryEntity newGalleryEntity = new GalleryEntity()
            {
                Id       = Guid.NewGuid(),
                Name     = "TestGalleryName",
                fk_owner = UserEntities[1].Id,
                owner    = UserEntities[1]
            };

            GalleryEntities.Add(newGalleryEntity);

            // Act
            GalleryPutDTO             galleryPutDto = new GalleryPutDTO("UpdatedTestGalleryName");
            ActionResult <GalleryDTO> response      = await controller.PutGallery(newGalleryEntity.Id, galleryPutDto);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(UnauthorizedResult));
            var result = response.Result as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
        }
        public async Task PutGallery_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            GalleryEntity newGalleryEntity = new GalleryEntity()
            {
                Id       = Guid.NewGuid(),
                Name     = "TestGalleryName",
                fk_owner = UserEntities[0].Id,
                owner    = UserEntities[0]
            };

            GalleryEntities.Add(newGalleryEntity);

            // Act
            GalleryPutDTO             galleryPutDto = new GalleryPutDTO("UpdatedTestGalleryName");
            ActionResult <GalleryDTO> response      = await controller.PutGallery(newGalleryEntity.Id, galleryPutDto);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(CreatedAtActionResult));
            var result = response.Result as CreatedAtActionResult;

            Assert.AreEqual(201, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(GalleryDTO));
            GalleryDTO retrievedItem = result.Value as GalleryDTO;

            Assert.AreEqual(retrievedItem.Id, newGalleryEntity.Id);
            Assert.AreEqual(retrievedItem.Name, "UpdatedTestGalleryName");
            GalleryEntities.Remove(newGalleryEntity);
        }
        public async Task PutGallery_not_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            // Act
            GalleryPutDTO             galleryPutDto = new GalleryPutDTO("UpdatedTestGalleryName");
            ActionResult <GalleryDTO> response      = await controller.PutGallery(Guid.NewGuid(), galleryPutDto);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(NotFoundResult));
            var result = response.Result as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
        }
        public async Task <GalleryDTO> PutGalleryAsync(Guid userId, Guid galleryId, GalleryPutDTO galleryPutDTO)
        {
            GalleryEntity galleryEntity = await _galleryRepository.GetGallery(galleryId);

            galleryPutDTO.ToGalleryEntity(ref galleryEntity);

            await _galleryRepository.PutGallery(galleryEntity);

            if (_galleryRepository.Save() == false)
            {
                throw new Exception();
            }

            int        numImagesInGallery = _imageRepository.GetNumberOfImagesInGallery(galleryEntity.Id);
            GalleryDTO dtoToReturn        = galleryEntity.ToGalleryDto(numImagesInGallery);

            return(dtoToReturn);
        }