public ActionResult <Continent> Post(int continentId, int countryId, [FromBody] City city, int capital) { log("Post"); try { cityRepos.AddCity(continentId, countryId, city, capital); return(CreatedAtAction(nameof(GetCity), new { continentId = continentId, countryId = countryId, cityId = city.CityId }, city)); } catch (GeoException ex) { return(NotFound(ex.Message)); } }
public async Task <IActionResult> CreateCityForCountry(int countryId, [FromBody] CityAddVM city) { if (city == null) { return(BadRequest()); } // 验证 if (!ModelState.IsValid) { // 验证不通过时返回 422 状态,表示请求的格式没有问题,但是语义有错误,比如实体验证错误。 return(new UnprocessableEntityObjectResult(ModelState)); } if (!await _countryRepository.CountryExistsAsync(countryId)) { return(NotFound()); } var cityModel = _mapper.Map <City>(city); _cityRepository.AddCity(countryId, cityModel); if (!await _unitOfWork.SaveAsync()) { return(StatusCode(500, "添加数据失败")); } var cityVM = _mapper.Map <CityVM>(cityModel); return(CreatedAtAction(nameof(GetCity), new { countryId, cityId = cityVM.Id }, cityVM)); }
public async Task <ActionResult <CityDto> > AddCity([FromBody] CityForCreationDto cityForCreation) { var validationResults = new CityForCreationDtoValidator().Validate(cityForCreation); validationResults.AddToModelState(ModelState, null); if (!ModelState.IsValid) { return(BadRequest(new ValidationProblemDetails(ModelState))); //return ValidationProblem(); } var city = _mapper.Map <City>(cityForCreation); await _cityRepository.AddCity(city); var saveSuccessful = await _cityRepository.SaveAsync(); if (saveSuccessful) { var cityFromRepo = await _cityRepository.GetCityAsync(city.CityId); var cityDto = _mapper.Map <CityDto>(cityFromRepo); var response = new Response <CityDto>(cityDto); return(CreatedAtRoute("GetCity", new { cityDto.CityId }, response)); } return(StatusCode(500)); }
public async Task <ActionResult <CityDto> > AddCity(CityForCreationDto cityForCreation) { var validationResults = new CityForCreationDtoValidator().Validate(cityForCreation); validationResults.AddToModelState(ModelState, null); if (!ModelState.IsValid) { return(BadRequest(new ValidationProblemDetails(ModelState))); //return ValidationProblem(); } var city = _mapper.Map <City>(cityForCreation); _cityRepository.AddCity(city); var saveSuccessful = await _cityRepository.SaveAsync(); if (saveSuccessful) { var cityDto = await _cityRepository.GetCityAsync(city.CityId); //get from repo for fk object, if needed return(CreatedAtRoute("GetCity", new { cityDto.CityId }, cityDto)); } return(StatusCode(500)); }
public async Task <ActionResult <City> > PostCity(City city) { repo.AddCity(city); await repo.SaveAsync(); return(Ok(city)); }
public async Task <IActionResult> AddCity(City city) { repo.AddCity(city); await repo.SaveAsync(); return(StatusCode(201)); }
public IActionResult AddCountry(CountryViewModel countryVM) { if (ModelState.IsValid) { City cityDB; Region regionDB; if (_cityRepository.CheckExistCity(countryVM.CapitalName) == "EXIST") { cityDB = _cityRepository.GetCityByName(countryVM.CapitalName); } else { _cityRepository.AddCity(new City { Name = countryVM.CapitalName }); cityDB = _cityRepository.GetCityByName(countryVM.CapitalName); } if (_regionRepository.CheckExistRegion(countryVM.RegionName) == "EXIST") { regionDB = _regionRepository.GetRegionByName(countryVM.RegionName); } else { _regionRepository.AddRegion(new Region { Name = countryVM.RegionName }); regionDB = _regionRepository.GetRegionByName(countryVM.RegionName); } if (_countryRepository.CheckExistCountry(countryVM.Country.CountryCode) == "NOT EXIST") { _countryRepository.AddCountry(countryVM.Country, cityDB.Name, regionDB.Name); TempData["successMessage"] = string.Format($"{countryVM.Country.Name}" + " has been successfully added to the database."); return(RedirectToAction("Index")); } else { _countryRepository.UpdateCountry(countryVM.Country, cityDB.Name, regionDB.Name); TempData["successMessage"] = string.Format($"{countryVM.Country.Name} has been successfully updated."); return(RedirectToAction("Index")); } } else { return(View(countryVM)); } }
public async Task <IActionResult> AddCity(CityResource cityResource) { var city = CityMapper.MapCityResourceToCity(cityResource); _cityRepository.AddCity(city); await _unitOfWork.UpdateDatabase(); return(Ok()); }
/// <summary> /// Adds new country. /// </summary> /// <param name="countryModel">Country view model.</param> /// <returns></returns> public async Task AddCountry(CountryModel countryModel) { // Check if city already exists. var city = await cityRepository.GetCityByName(countryModel.Capital); if (city == null) { // City not found, add it and get Id. await cityRepository.AddCity(new City { Name = countryModel.Capital }); city = await cityRepository.GetCityByName(countryModel.Capital); } // Check if region already exists. var region = await regionRepository.GetRegionByName(countryModel.Region); if (region == null) { // Region not found, add it and get Id. await regionRepository.AddRegion(new Region { Name = countryModel.Region }); region = await regionRepository.GetRegionByName(countryModel.Region); } // Check if country with the provided code already exists. var countryExtracted = await countryRepository.GetCountryByCode(countryModel.Code); if (countryExtracted == null) { // Country by code not found, add new country. await countryRepository.AddCountry(new Country { Name = countryModel.Name, Code = countryModel.Code, Area = countryModel.Area, Population = countryModel.Population, RegionId = region.Id, CapitalId = city.Id, }); } else { // Update existing country. countryExtracted.Name = countryModel.Name; countryExtracted.Code = countryModel.Code; countryExtracted.Area = countryModel.Area; countryExtracted.Population = countryModel.Population; countryExtracted.RegionId = region.Id; countryExtracted.CapitalId = city.Id; countryExtracted.Region = region; countryExtracted.Capital = city; await countryRepository.UpdateCountry(countryExtracted); } }
public async Task <ActionResult <City> > AddCity(City city) { if (await _cityRepository.CityExists(city.Name)) { return(BadRequest("City already added")); } _cityRepository.AddCity(city); await _cityRepository.SaveAllAsync(); return(new City { Name = city.Name, }); }
public void SaveCity(City city) { var validation = city.Validate(); Guard.AssertIsSuccess(validation); CityMustBeUniqueValidation.It(_repository).Validate(city); if (city.IsNew) { _repository.AddCity(city); } else { _repository.EditCity(city); } }
/// <summary> /// It add City to the state in db /// </summary> /// <param name="addCity">City Name and state Id</param> /// <returns>Add City Response Model</returns> public AddCityResponseModel AddCity(AddCityRequestModel addCity) { try { if (addCity == null) { return(null); } else { return(_cityRepository.AddCity(addCity)); } } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <IActionResult> CteateCityForCountry(int countryId, [FromBody] CityAddResource city) { if (city == null) { return(BadRequest()); } if (!await _countryRepository.CountryExistAsync(countryId)) { return(NotFound()); } var cityModel = _mapper.Map <City>(city); _cityRepository.AddCity(countryId, cityModel); if (!await _unitOfWork.SaveAsync()) { return(StatusCode(500, "保存错误")); } var cityResource = _mapper.Map <CityResource>(cityModel); return(CreatedAtRoute("GetCity", new { countryId, cityId = cityResource.Id }, cityResource)); }
public IActionResult CreateCountry([FromBody] City city) { if (city == null) { return(BadRequest()); } if (city.Name == string.Empty) { ModelState.AddModelError("Name", "The name of the City shouldn't be empty"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var createdCity = _cityRepository.AddCity(city); return(Created("city", city)); }
public void AddCity([FromBody] CityModel model) { if (ModelState.IsValid) { if (model.Id == 0) { City city = new City() { Id = model.Id, Name = model.Name, CountryId = model.CountryId, IsDeleted = false }; citiesRepository.AddCity(city); } if (model.Id != 0) { City city = citiesRepository.GetCityById(model.Id); city.Name = model.Name; city.CountryId = model.CountryId; citiesRepository.UpdateCity(city); } } }
//[ValidateAntiForgeryToken] public IActionResult AddPost(CityViewModel cityViewModel) { ViewBag.CountryId = new SelectList(_country.GetCountries(), "Id", "Name"); var cityList = _city.GetCities(); ViewBag.Cities = cityList; if (cityViewModel.CountryId == null) { ModelState.AddModelError("", "الرجاء ادخال البلد"); } if (cityViewModel.Id == 0) { ModelState.Remove("Id"); ModelState.Remove("CountryId"); if (ModelState.IsValid) { var city = _mapper.Map <City>(cityViewModel); _city.AddCity(city); _toastNotification.AddSuccessToastMessage("تم أضافةالمدينة بنجاح"); return(RedirectToAction(nameof(Index))); } return(View(nameof(Index), cityViewModel)); } else { ModelState.Remove("CountryId"); if (ModelState.IsValid) { var city = _mapper.Map <City>(cityViewModel); _city.UpdateCity(cityViewModel.Id, city); _toastNotification.AddSuccessToastMessage("تم تعديل المدينة بنجاح"); return(RedirectToAction(nameof(Index))); } return(View(nameof(Index), cityViewModel)); } }
public async Task <int> Post([FromBody] City city) { return(await _cityRepository.AddCity(city)); }
public async Task AddCity(CityModel city) { var cityDataModel = city.ToCoreDataModel(_mapper); await _demoRepository.AddCity(cityDataModel); }
public void AddCity(City city) { _cityRepository.AddCity(city); _cityRepository.Save(); }
public IActionResult Create(City newCity) { _repository.AddCity(newCity); return(RedirectToAction("Index", "Home")); }
// POST: api/DatabaseSample public void Post([FromBody] City city) { iCity.AddCity(city); }