Пример #1
0
        public async Task <IActionResult> Edit(string id, GymInputModel gymInput)
        {
            var existsGym = this.gymsService
                            .Exists(x => x.Id == id);

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

            if (!this.ModelState.IsValid)
            {
                var gym = new GymEditViewModel()
                {
                    Id       = id,
                    GymInput = gymInput,
                };

                this.SetCountrySelectListItems(gym.GymInput);
                this.SetCitySelectListItems(gym.GymInput);

                return(this.View(gym));
            }

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

            await this.gymsService.EditAsync(id, gymInput, image);

            var encodedGymName = HttpUtility.HtmlEncode(gymInput.Name);

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

            return(this.RedirectToAction("Details", "Gyms", new { area = "Places", id }));
        }
Пример #2
0
        public IActionResult Create()
        {
            var gym = new GymInputModel();

            this.SetCountrySelectListItems(gym);
            return(this.View(gym));
        }
Пример #3
0
 private void SetCitySelectListItems(GymInputModel gym)
 {
     gym.CitiesSelectListItems = this.citiesService
                                 .GetMany <CityViewModel>(x => x.CountryId == gym.CountryId)
                                 .Select(x => new SelectListItem(x.Name, x.Id))
                                 .ToList();
 }
Пример #4
0
 private void SetCountrySelectListItems(GymInputModel gym)
 {
     gym.CountriesSelectListItems = this.countriesService
                                    .GetMany <CountryViewModel>(x => x.Cities.Any(), x => x.Name)
                                    .Select(x => new SelectListItem(x.Name, x.Id))
                                    .ToList();
 }
Пример #5
0
        public async Task AddAsync(GymInputModel gymInput, ImageInputModel imageInput)
        {
            this.NullCheck(gymInput, nameof(gymInput));

            var country = this.mapper.Map <Gym>(gymInput);

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

            await this.gymsRepository.AddAsync(country);

            await this.gymsRepository.SaveChangesAsync();
        }
Пример #6
0
        public async Task EditAsync(string id, GymInputModel gymInput, ImageInputModel imageInput)
        {
            this.NullCheck(id, nameof(id));
            this.NullCheck(gymInput, nameof(gymInput));

            var gym = this.gymsRepository
                      .All()
                      .FirstOrDefault(x => x.Id == id);

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

            gym.Name        = gymInput.Name;
            gym.Description = gymInput.Description;
            gym.CityId      = gymInput.CityId;

            await this.gymsRepository.SaveChangesAsync();
        }
Пример #7
0
        public async Task <IActionResult> Create(GymInputModel gymInput)
        {
            var cityExists = this.citiesService.Exists(x => x.Id == gymInput.CityId);

            if (!this.ModelState.IsValid || !cityExists)
            {
                this.SetCountrySelectListItems(gymInput);

                return(this.View(gymInput));
            }

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

            await this.gymsService.AddAsync(gymInput, image);

            var encodedGymName = HttpUtility.HtmlEncode(gymInput.Name);

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

            return(this.RedirectToAction("Index", "Gyms", new { area = "Places" }));
        }
Пример #8
0
        public async Task AddMapsTheInputModelAndImageAndAddsThem(
            string name,
            string description,
            string cityId,
            bool nullImage,
            string imageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved    = true;
            var gymsList = new List <Gym>();

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

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <Gym>()))
            .Callback((Gym gym) =>
            {
                gymsList.Add(gym);
            });

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

            var gymViewModel = new GymInputModel()
            {
                Name        = name,
                CityId      = cityId,
                Description = description,
            };

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

            if (nullImage)
            {
                imageInputModel = null;
            }

            var gymsService = new GymsService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            // Act
            await gymsService.AddAsync(gymViewModel, imageInputModel);

            // Assert
            var actualGym = gymsList[0];

            Assert.True(saved);
            Assert.Equal(name, actualGym.Name);
            Assert.Equal(cityId, actualGym.CityId);
            Assert.Equal(description, actualGym.Description);

            var actualImage = actualGym.Image;

            if (nullImage)
            {
                Assert.Null(actualImage);
            }
            else
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
        }
Пример #9
0
        public async Task EditAsyncEditsThePropertiesAndSavesTheChanges(
            string name,
            string description,
            string cityId,
            string newName,
            string newDescription,
            string newCityId,
            bool imageNull,
            string imageSource,
            string newImageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);
            var saved = true;

            var gym = new Gym()
            {
                Name        = name,
                Description = description,
                CityId      = cityId,
            };

            var gymsList = new List <Gym>()
            {
                new Gym(),
                gym,
                new Gym(),
                new Gym(),
            };

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

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

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

            var gymEditModel = new GymInputModel()
            {
                Name        = newName,
                Description = newDescription,
                CityId      = newCityId,
            };

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

            if (imageNull)
            {
                imageEditModel = null;
            }

            var gymsService = new GymsService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            // Act
            await gymsService.EditAsync(gym.Id, gymEditModel, imageEditModel);

            // Assert
            Assert.True(saved);

            Assert.Equal(newName, gym.Name);
            Assert.Equal(newDescription, gym.Description);
            Assert.Equal(newCityId, gym.CityId);

            var actualImage = gym.Image;

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