/// <summary> /// Exists the specified city. /// </summary> /// <param name="city">The city.</param> /// <returns>Task<System.Boolean>.</returns> public async Task<bool> Exist(City city) { using (ChePingContext db = new ChePingContext()) { return await db.Cities.AnyAsync(c => c.ProvinceName == city.ProvinceName && c.CityName == city.CityName); } }
public async Task<IHttpActionResult> Exist(CityDto dto) { City city = new City { CityName = dto.CityName, ProvinceName = dto.ProvinceName }; return this.Ok(new BoolResponse { Result = await this.cityService.Exist(city) }); }
/// <summary> /// Creates the specified city. /// </summary> /// <param name="city">The city.</param> /// <returns>Task<City>.</returns> /// <exception cref="ApplicationException">城市信息已经存在</exception> public async Task<City> Create(City city) { if (await this.Exist(city)) { throw new ApplicationException("城市信息已经存在"); } using (ChePingContext db = new ChePingContext()) { await db.SaveAsync(city); } return city; }
public async Task<IHttpActionResult> Create(CityDto dto) { City city = new City { CityName = dto.CityName, ProvinceName = dto.ProvinceName }; if (await this.cityService.Exist(city)) { return this.BadRequest("城市信息已经存在"); } return this.Ok((await this.cityService.Create(city)).ToDto()); }