public async Task <IActionResult> Create(CountryInputModel countryInput)
        {
            var existsCountry = this.countriesService
                                .Exists(x => x.Name == countryInput.Name || x.CountryCode == countryInput.CountryCode);

            if (!this.ModelState.IsValid || existsCountry)
            {
                if (existsCountry)
                {
                    this.ModelState.AddModelError(MatchingCountryKey, MatchingCountryErrorMessage);
                }

                return(this.View(countryInput));
            }

            var image = await this.cloudinaryService.SaveImageAsync(countryInput.FormFile);

            await this.countriesService.AddAsync(countryInput, image);

            var countryNameEncoded = HttpUtility.HtmlEncode(countryInput.Name);

            this.TempData[GlobalConstants.MessageKey] = $"Successfully created country <strong>{countryNameEncoded}</strong>!";

            return(this.RedirectToAction("Index", "Countries", new { area = "Places" }));
        }
예제 #2
0
        public void AddCountry(CountryInputModel model)
        {
            var country = new Country
            {
                Name         = model.Country,
                ShippingCost = model.ShippingCost
            };

            _db.Countries.Add(country);
        }
예제 #3
0
        public async Task AddAsync(CountryInputModel countryInput, ImageInputModel imageInput = null)
        {
            this.NullCheck(countryInput, nameof(countryInput));

            var country = this.mapper.Map <Country>(countryInput);

            country.Image = this.mapper.Map <Image>(imageInput);

            await this.countriesRepository.AddAsync(country);

            await this.countriesRepository.SaveChangesAsync();
        }
예제 #4
0
        public ActionResult Create(CountryInputModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var country = Mapper.Map <Country>(model);
                this.Data.Countries.Add(country);
                this.Data.SaveChanges();

                return(this.RedirectToAction("Index", "Countries"));
            }

            return(this.View(model));
        }
예제 #5
0
        public async Task <IActionResult> Create(CountryInputModel countryInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(countryInputModel));
            }

            var countryServiceModel = countryInputModel.To <CountryServiceInputModel>();

            await this.countriesService.CreateAsync(countryServiceModel);

            return(this.Redirect("/Administration/Countries/All"));
        }
예제 #6
0
        public async Task <IActionResult> AddCountry(CountryInputModel countryInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(countryInputModel));
            }

            try
            {
                await this.countriesService.AddAsync(countryInputModel);
            }
            catch (Exception)
            {
                return(this.View("DuplicateValue", countryInputModel));
            }

            return(this.RedirectToAction("Index"));
        }
예제 #7
0
        public async Task AddAsync(CountryInputModel countryInputModel)
        {
            var country = new Country
            {
                Name = countryInputModel.Name,
            };

            bool doesCountryExist = await this.countriesRepository.All().AnyAsync(x => x.Name == country.Name);

            if (doesCountryExist)
            {
                throw new ArgumentException(string.Format(GlobalConstants.ErrorMessages.CountryNameAlreadyExists, country.Name));
            }

            await this.countriesRepository.AddAsync(country);

            await this.countriesRepository.SaveChangesAsync();
        }
예제 #8
0
        public async Task EditAsync(string id, CountryInputModel countryInput, ImageInputModel imageInput)
        {
            this.NullCheck(id, nameof(id));
            this.NullCheck(countryInput, nameof(countryInput));

            var country = this.countriesRepository
                          .All()
                          .FirstOrDefault(x => x.Id == id);

            if (imageInput != null)
            {
                country.Image = this.mapper.Map <Image>(imageInput);
            }

            country.Name        = countryInput.Name;
            country.CountryCode = countryInput.CountryCode;
            country.Description = countryInput.Description;

            await this.countriesRepository.SaveChangesAsync();
        }
        public async Task <IActionResult> Edit(string id, CountryInputModel countryInput)
        {
            var existsId = this.countriesService
                           .Exists(x => x.Id == id);

            if (!existsId)
            {
                return(this.NotFound());
            }

            var existsCountry = this.countriesService
                                .Exists(x => x.Id != id && (x.Name == countryInput.Name || x.CountryCode == countryInput.CountryCode));

            if (!this.ModelState.IsValid || existsCountry)
            {
                if (existsCountry)
                {
                    this.ModelState.AddModelError(MatchingCountryKey, MatchingCountryErrorMessage);
                }

                var country = new CountryEditViewModel()
                {
                    Id           = id,
                    CountryInput = countryInput,
                };

                return(this.View(country));
            }

            var image = await this.cloudinaryService.SaveImageAsync(countryInput.FormFile);

            await this.countriesService.EditAsync(id, countryInput, image);

            var countryNameEncoded = HttpUtility.HtmlEncode(countryInput.Name);

            this.TempData[GlobalConstants.MessageKey] = $"Successfully edited country <strong>{countryNameEncoded}</strong>!";

            return(this.RedirectToAction("Details", "Countries", new { area = "Places", id }));
        }
예제 #10
0
        public async Task AddMapsTheInputModelAndImageAndAddsThem(
            string name,
            string description,
            string countryCode,
            bool nullImage,
            string imageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved         = true;
            var countriesList = new List <Country>();

            var repositoryMock = new Mock <IDeletableEntityRepository <Country> >();

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <Country>()))
            .Callback((Country country) =>
            {
                countriesList.Add(country);
            });

            repositoryMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            var cityInputModel = new CountryInputModel()
            {
                Name        = name,
                CountryCode = countryCode,
                Description = description,
            };

            var imageInputModel = new ImageInputModel()
            {
                Source = imageSource,
            };

            if (nullImage)
            {
                imageInputModel = null;
            }

            var countriesService = new CountriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            // Act
            await countriesService.AddAsync(cityInputModel, imageInputModel);

            // Assert
            var actualCountry = countriesList[0];

            Assert.True(saved);
            Assert.Equal(name, actualCountry.Name);
            Assert.Equal(description, actualCountry.Description);
            Assert.Equal(countryCode, actualCountry.CountryCode);

            var actualImage = actualCountry.Image;

            if (nullImage)
            {
                Assert.Null(actualImage);
            }
            else
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
        }
예제 #11
0
        public async Task EditAsyncEditsTheCorrectPropertiesAndSavesTheResult(
            string countryName,
            string description,
            string countryCode,
            string newCountryName,
            string newDescription,
            string newCountryCode,
            bool imageNull,
            string imageSource,
            string newImageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);
            var saved = true;

            var country = new Country()
            {
                Name        = countryName,
                CountryCode = countryCode,
                Description = description,
                Image       = new Image()
                {
                    Source = imageSource,
                },
            };

            var countriesList = new List <Country>()
            {
                new Country(),
                new Country(),
                country,
                new Country(),
            };

            var repositoryMock = new Mock <IDeletableEntityRepository <Country> >();

            repositoryMock.Setup(x => x.All())
            .Returns(countriesList.AsQueryable());

            repositoryMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            var countriesService = new CountriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            var editInputModel = new CountryInputModel()
            {
                Name        = newCountryName,
                CountryCode = newCountryCode,
                Description = newDescription,
            };

            var imageInputModel = new ImageInputModel()
            {
                Source = newImageSource,
            };

            if (imageNull)
            {
                imageInputModel = null;
            }

            // Act
            await countriesService.EditAsync(country.Id, editInputModel, imageInputModel);

            // Assert
            Assert.True(saved);

            Assert.Equal(newCountryName, country.Name);
            Assert.Equal(newCountryCode, country.CountryCode);
            Assert.Equal(newDescription, country.Description);

            var actualImage = country.Image;

            if (imageNull)
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
            else
            {
                Assert.Equal(newImageSource, actualImage.Source);
            }
        }