public CityMasters Update(CityMasters cityMasters) { var city = _context.CityMasters.Find(cityMasters.Id); if (city == null) { throw new AppException("city not found"); } // update City name if it has changed if (!string.IsNullOrWhiteSpace(cityMasters.Name) && cityMasters.Name != city.Name) { // throw error if the new city name is already taken if (_context.CityMasters.Any(x => x.Name == cityMasters.Name)) { throw new AppException("City Name " + cityMasters.Name + " is already Registered"); } city.Name = cityMasters.Name; } _context.CityMasters.Update(city); _context.SaveChanges(); return(city); }
public CityMasters Create(CityMasters cityMasters) { // validation if (_context.CityMasters.Any(x => x.Name == cityMasters.Name)) { throw new AppException("City Name \"" + cityMasters.Name + "\" is already taken"); } _context.CityMasters.Add(cityMasters); _context.SaveChanges(); return(cityMasters); }