public async Task EditAsyncEditsDestinationWhenImageStaysTheSame()
        {
            await this.AddTestingCountryToDb();

            await this.AddTestingDestinationToDb();

            this.DbContext.Countries.Add(new Country()
            {
                Id = 2, Name = "TestEditLocation"
            });
            await this.DbContext.SaveChangesAsync();

            var newName      = SecondTestDestinationName;
            var newCountryId = 2;

            Assert.NotEqual(newName, this.DbContext.Destinations.Find(TestDestinationId).Name);
            Assert.NotEqual(newCountryId, this.DbContext.Destinations.Find(TestDestinationId).CountryId);

            var destinationEditViewModel = new DestinationEditViewModel()
            {
                Id        = TestDestinationId,
                Name      = newName,
                CountryId = newCountryId,
                NewImage  = null,
            };

            await this.DestinationsServiceMock.EditAsync(destinationEditViewModel);

            Assert.Equal(newName, this.DbContext.Destinations.Find(TestDestinationId).Name);
            Assert.Equal(newCountryId, this.DbContext.Destinations.Find(TestDestinationId).CountryId);
        }
        public async Task EditAsync(DestinationEditViewModel destinationEditViewModel)
        {
            var destination = this.destinationsRepository.All()
                              .FirstOrDefault(d => d.Id == destinationEditViewModel.Id);

            if (destination == null)
            {
                throw new NullReferenceException(string.Format(ServicesDataConstants.NullReferenceDestinationId, destinationEditViewModel.Id));
            }

            var country = this.countriesRepository.All()
                          .FirstOrDefault(c => c.Id == destinationEditViewModel.CountryId);

            if (country == null)
            {
                throw new NullReferenceException(string.Format(ServicesDataConstants.NullReferenceCountryId, destinationEditViewModel.CountryId));
            }

            if (destinationEditViewModel.NewImage != null)
            {
                var newImageUrl = await ApplicationCloudinary.UploadImage(this.cloudinary, destinationEditViewModel.NewImage, destinationEditViewModel.Name);

                destination.ImageUrl = newImageUrl;
            }

            destination.Name        = destinationEditViewModel.Name;
            destination.Country     = country;
            destination.Information = destinationEditViewModel.Information;

            this.destinationsRepository.Update(destination);
            await this.destinationsRepository.SaveChangesAsync();
        }
        public async Task <IActionResult> Edit(DestinationEditViewModel destinationEditViewModel)
        {
            if (destinationEditViewModel.NewImage != null)
            {
                var fileType = destinationEditViewModel.NewImage.ContentType.Split('/')[1];
                if (!this.IsImageTypeValid(fileType))
                {
                    return(this.View(destinationEditViewModel));
                }
            }

            await this.destinationsService.EditAsync(destinationEditViewModel);

            return(this.RedirectToAction("Details", "Destinations", new { area = "", id = destinationEditViewModel.Id }));
        }
        public async Task EditAsyncThrowsNullReferenceExceptionIfCountryNotFound()
        {
            await this.AddTestingDestinationToDb();

            var invalidDestinationEditViewModel = new DestinationEditViewModel()
            {
                Id        = TestDestinationId,
                Name      = TestDestinationName,
                CountryId = TestCountryId,
            };

            var exception = await Assert.ThrowsAsync <NullReferenceException>(() =>
                                                                              this.DestinationsServiceMock.EditAsync(invalidDestinationEditViewModel));

            Assert.Equal(string.Format(ServicesDataConstants.NullReferenceCountryId, invalidDestinationEditViewModel.CountryId), exception.Message);
        }
        public async Task EditAsyncEditsDestinationsImage()
        {
            await this.AddTestingCountryToDb();

            this.DbContext.Destinations.Add(new Destination
            {
                Id        = TestDestinationId,
                Name      = TestDestinationName,
                CountryId = TestCountryId,
                ImageUrl  = TestImageUrl,
            });
            await this.DbContext.SaveChangesAsync();

            using (var stream = File.OpenRead(TestImagePath))
            {
                var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = TestImageContentType,
                };

                var destinationEditViewModel = new DestinationEditViewModel()
                {
                    Id        = TestDestinationId,
                    Name      = TestDestinationName,
                    CountryId = TestCountryId,
                    NewImage  = file,
                };

                await this.DestinationsServiceMock.EditAsync(destinationEditViewModel);

                ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), destinationEditViewModel.Name);
            }

            Assert.NotEqual(TestImageUrl, this.DbContext.Destinations.Find(TestDestinationId).ImageUrl);
        }