コード例 #1
0
        public async Task CreateAsyncReturnsExistingViewModelIfRestaurantExists()
        {
            await this.AddTestingDestinationToDb();

            await this.AddTestingCountryToDb();

            DestinationDetailsViewModel destinationDetailsViewModel;

            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 destinationCreateInputModel = new DestinationCreateInputModel()
                {
                    Name      = TestDestinationName,
                    CountryId = TestCountryId,
                    Image     = file,
                };

                destinationDetailsViewModel = await this.DestinationsServiceMock.CreateAsync(destinationCreateInputModel);
            }

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

            var destinationsDbSet = this.DbContext.Destinations.OrderBy(r => r.CreatedOn);

            Assert.Equal(destinationsDbSet.Last().Id, destinationDetailsViewModel.Id);
            Assert.Equal(destinationsDbSet.Last().Name, destinationDetailsViewModel.Name);
            Assert.Equal(destinationsDbSet.Last().Country.Name, destinationDetailsViewModel.CountryName);
        }
コード例 #2
0
        public async Task <IActionResult> Create(DestinationCreateInputModel destinationCreateInputModel)
        {
            var fileType = destinationCreateInputModel.Image.ContentType.Split('/')[1];

            if (!this.IsImageTypeValid(fileType))
            {
                return(this.View(destinationCreateInputModel));
            }

            var destination = await this.destinationsService.CreateAsync(destinationCreateInputModel);

            return(this.RedirectToAction("Details", "Destinations", new { area = "", id = destination.Id }));
        }
コード例 #3
0
        public async Task CreateAsyncThrowsNullReferenceExceptionIfLocationNotFound()
        {
            var invalidDestinationCreateInputModel = new DestinationCreateInputModel
            {
                Name      = TestDestinationName,
                CountryId = TestCountryId,
            };

            var exception = await Assert.ThrowsAsync <NullReferenceException>(() =>
                                                                              this.DestinationsServiceMock.CreateAsync(invalidDestinationCreateInputModel));

            Assert.Equal(string.Format(ServicesDataConstants.NullReferenceCountryId, invalidDestinationCreateInputModel.CountryId), exception.Message);
        }
コード例 #4
0
        public async Task <DestinationDetailsViewModel> CreateAsync(DestinationCreateInputModel destinationCreateInputModel)
        {
            var country = await this.countriesRepository.All().FirstOrDefaultAsync(c => c.Id == destinationCreateInputModel.CountryId);

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

            // If destination exists return existing view model
            var destinationExists = this.destinationsRepository.All().Any(d =>
                                                                          d.Name == destinationCreateInputModel.Name && d.CountryId == destinationCreateInputModel.CountryId);

            if (destinationExists)
            {
                return(AutoMapper.Mapper
                       .Map <DestinationDetailsViewModel>(this.destinationsRepository.All()
                                                          .First(d => d.Name == destinationCreateInputModel.Name &&
                                                                 d.CountryId == destinationCreateInputModel.CountryId)));
            }

            var imageUrl = await ApplicationCloudinary.UploadImage(this.cloudinary, destinationCreateInputModel.Image, destinationCreateInputModel.Name);

            var googleServiceInfo =
                DateTimeExtensions.GetGoogleServiceInfo(destinationCreateInputModel.Name, country.Name);

            var destination = new Destination
            {
                Name         = destinationCreateInputModel.Name,
                CountryId    = destinationCreateInputModel.CountryId,
                ImageUrl     = imageUrl,
                Information  = destinationCreateInputModel.Information,
                Latitude     = googleServiceInfo.Latitude,
                Longitude    = googleServiceInfo.Longitude,
                UtcRawOffset = googleServiceInfo.UtcRawOffset,
            };

            this.destinationsRepository.Add(destination);
            await this.destinationsRepository.SaveChangesAsync();

            var destinationDetailsViewModel = AutoMapper.Mapper.Map <DestinationDetailsViewModel>(destination);

            return(destinationDetailsViewModel);
        }