コード例 #1
0
        public async Task AddAsync(CityInputModel cityInputModel)
        {
            var city = new City
            {
                Name      = cityInputModel.Name,
                CountryId = cityInputModel.CountryId,
            };

            bool doesCityExist = await this.citiesRepository.All().AnyAsync(x => x.Name == city.Name);

            bool doesCityWithCurrentCountryIdExist = await this.citiesRepository.All().AnyAsync(x => x.Name == city.Name && x.CountryId == city.CountryId);

            if (doesCityExist)
            {
                if (!doesCityWithCurrentCountryIdExist)
                {
                }
                else
                {
                    throw new ArgumentException(string.Format(GlobalConstants.ErrorMessages.CityNameAlreadyExists, city.Name));
                }
            }

            await this.citiesRepository.AddAsync(city);

            await this.citiesRepository.SaveChangesAsync();
        }
コード例 #2
0
        public ActionResult <CityResponseModel> GetCity([FromBody] CityInputModel data)
        {
            CityResponseModel res = new CityResponseModel();

            try
            {
                CityRepository repo = new CityRepository(db);

                var query = repo.GetCities(data.KodeProv);

                var res2 = (from y in query
                            select new CityOuputModel
                {
                    ProvinceID = y.ProvinceID,
                    CityID = y.ID,
                    Kode = y.Kode,
                    Kota = y.CityName
                }).OrderBy(x => x.Kota).ToList();

                res.data     = res2;
                res.Message  = "Success get data";
                res.Response = true;
                return(res);
            }
            catch (Exception ex)
            {
                res.Message  = ex.Message;
                res.Response = false;
                return(res);
            }
        }
コード例 #3
0
        public IActionResult GetCity(int kodeprov)
        {
            List <CityOutputModel> city = new List <CityOutputModel>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseAPI + "Base/");
                CityInputModel param = new CityInputModel();
                param.KodeProv = kodeprov;
                JsonConvert.SerializeObject(param);
                var cityTask = client.PostAsJsonAsync <CityInputModel>("GetCity", param);
                cityTask.Wait();
                var results = cityTask.Result;
                if (results.IsSuccessStatusCode)
                {
                    var content = results.Content.ReadAsStringAsync();
                    CityResponseModel provinceResponse = JsonConvert.DeserializeObject <CityResponseModel>(content.Result);
                    city = provinceResponse.data.OrderBy(x => x.Kota).ToList();
                }
                else
                {
                    city = null;
                    ModelState.AddModelError(string.Empty, "Terjadi kesalahan. Mohon hubungi admin.");
                }
            }
            return(Json(city));
        }
コード例 #4
0
 private void SetListItems(CityInputModel city)
 {
     city.CountriesSelectListItems = this.countriesService
                                     .GetMany <CountryViewModel>(orderBySelector: x => x.Name)
                                     .Select(x => new SelectListItem(x.Name, x.Id))
                                     .ToList();
 }
コード例 #5
0
        public IActionResult Create()
        {
            var city = new CityInputModel();

            this.SetListItems(city);

            return(this.View(city));
        }
コード例 #6
0
 public static City ToDataModel(CityInputModel model)
 {
     return(new City
     {
         Id = model.Id,
         Name = model.Name
     });
 }
コード例 #7
0
        public async Task <IActionResult> CreateCity(CityInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            await this.addressService.AddNewCityAsync <CityInputModel>(model);

            return(this.Redirect($"/Home/Index"));
        }
コード例 #8
0
        public async Task <IActionResult> AddCity(CityInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.citiesService.AddAsync(input.Name);

            return(this.RedirectToAction("Index"));
        }
コード例 #9
0
ファイル: DictionaryController.cs プロジェクト: evsig/DevEdu
        public async Task <ActionResult <int?> > UpdateCity([FromBody] CityInputModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            City dataModel = CityMapper.ToDataModel(model);
            int? id        = await dictionaryStorage.CityAddOrUpdate(dataModel);

            return(Ok(id));
        }
コード例 #10
0
        public async Task AddAsync(CityInputModel cityInput, ImageInputModel imageInput)
        {
            this.NullCheck(cityInput, nameof(cityInput));

            var city = this.mapper.Map <City>(cityInput);

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

            await this.citiesRepository.AddAsync(city);

            await this.citiesRepository.SaveChangesAsync();
        }
コード例 #11
0
        public async Task <IActionResult> Post(CityInputModel cityInput)
        {
            var response     = new Response();
            var existsByName = this.citiesService.Exists(x => x.Name == cityInput.Name);

            if (!this.ModelState.IsValid || existsByName)
            {
                response.Message = "City with that name already exists!";
                return(this.BadRequest(response));
            }

            await this.citiesService.AddAsync(cityInput);

            response.Message = "Successfully added city!";

            return(this.Ok(response));
        }
コード例 #12
0
        public async Task <IActionResult> AddCity(CityInputModel cityInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(cityInputModel));
            }

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

            return(this.RedirectToAction("Index"));
        }
コード例 #13
0
        public async Task EditAsync(string id, CityInputModel cityInput, ImageInputModel imageInput)
        {
            this.NullCheck(id, nameof(id));
            this.NullCheck(cityInput, nameof(cityInput));

            var city = this.citiesRepository
                       .All()
                       .FirstOrDefault(x => x.Id == id);

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

            city.Name        = cityInput.Name;
            city.CountryId   = cityInput.CountryId;
            city.Description = cityInput.Description;

            await this.citiesRepository.SaveChangesAsync();
        }
コード例 #14
0
        public async Task <IActionResult> Create(CityInputModel cityInput)
        {
            var existCountry = this.countriesService.Exists(x => x.Id == cityInput.CountryId);

            if (!this.ModelState.IsValid || !existCountry)
            {
                this.SetListItems(cityInput);
                return(this.View(cityInput));
            }

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

            await this.citiesService.AddAsync(cityInput, image);

            var cityNameEncoded = HttpUtility.HtmlEncode(cityInput.Name);

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

            return(this.RedirectToAction("Index", "Cities", new { area = "Places" }));
        }
コード例 #15
0
        public async Task <IActionResult> Edit(string id, CityInputModel cityInput)
        {
            var existsId = this.citiesService
                           .Exists(x => x.Id == id);

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

            var existsCity = this.citiesService
                             .Exists(x => x.Id != id && x.Name == cityInput.Name && x.CountryId == cityInput.CountryId);

            if (!this.ModelState.IsValid || existsCity)
            {
                var city = new CityEditViewModel()
                {
                    Id        = id,
                    CityInput = cityInput,
                };

                this.SetListItems(city.CityInput);

                return(this.View(city));
            }

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

            await this.citiesService.EditAsync(id, cityInput, image);

            var cityNameEncoded = HttpUtility.HtmlEncode(cityInput.Name);

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

            return(this.RedirectToAction("Details", "Cities", new { area = "Places", id }));
        }
コード例 #16
0
        public async Task EdidAsyncSetsTheNewPropertiesAndSavesTheResult(
            string name,
            string description,
            string countryId,
            string newName,
            string newDescription,
            string newCountryId,
            bool imageNull,
            string imageSource,
            string newImageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);
            var saved = true;

            var city = new City()
            {
                Name        = name,
                Description = description,
                CountryId   = countryId,
                Image       = new Image()
                {
                    Source = imageSource,
                },
            };

            var citiesList = new List <City>()
            {
                new City(),
                city,
                new City(),
                new City(),
                new City(),
            };

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

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

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

            var citiesService = new CitiesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            var cityEditModel = new CityInputModel()
            {
                Name        = newName,
                Description = newDescription,
                CountryId   = newCountryId,
            };

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

            if (imageNull)
            {
                imageEditModel = null;
            }

            // Act
            await citiesService.EditAsync(city.Id, cityEditModel, imageEditModel);

            // Assert
            Assert.True(saved);

            Assert.Equal(newName, city.Name);
            Assert.Equal(newDescription, city.Description);
            Assert.Equal(newCountryId, city.CountryId);

            var actualImage = city.Image;

            if (imageNull)
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
            else
            {
                Assert.Equal(newImageSource, actualImage.Source);
            }
        }
コード例 #17
0
        public async Task AddMapsTheInputModelAndImageAndAddsThem(
            string name,
            string description,
            string countryId,
            bool nullImage,
            string imageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved      = true;
            var citiesList = new List <City>();

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

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <City>()))
            .Callback((City city) =>
            {
                citiesList.Add(city);
            });

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

            var cityInputModel = new CityInputModel()
            {
                Name        = name,
                Description = description,
                CountryId   = countryId,
            };

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

            if (nullImage)
            {
                imageInputModel = null;
            }

            var citiesService = new CitiesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

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

            // Assert
            var actualCity = citiesList[0];

            Assert.True(saved);
            Assert.Equal(name, actualCity.Name);
            Assert.Equal(description, actualCity.Description);
            Assert.Equal(countryId, actualCity.CountryId);

            var actualImage = actualCity.Image;

            if (nullImage)
            {
                Assert.Null(actualImage);
            }
            else
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
        }