public IActionResult PutCity([FromBody] CityCreateModel cityCreateModel, int id) { var city = _citiesDataStore.Cities.Where(c => c.Id.Equals(id)).First(); if (city.Equals(null)) { NotFound(); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } city = new CityGetModel { Id = id, Name = cityCreateModel.Name, Description = cityCreateModel.Description, NumberOfPointsOfInterest = cityCreateModel.NumberOfPointsOfInterest }; _citiesDataStore.Cities[city.Id - 1] = city; return(CreatedAtRoute("GetCity", new { id = city.Id }, city)); }
public IActionResult AddCity([FromBody] CityGetModel city) { if (city == null) { return(BadRequest()); } var citiesDataStore = CitiesDataStore.GetCityDataStoreInstance(); int newCityId = citiesDataStore.Cities .Max(x => x.Id) + 1; var newCity = new CityGetModel { Id = newCityId, Name = city.Name, Description = city.Description, NumberOfPointsInterest = city.NumberOfPointsInterest }; citiesDataStore.Cities.Add(newCity); return(CreatedAtRoute( "GetCity", new { id = newCityId } , newCity)); }
public IActionResult CreateCity([FromBody] CityCreateModel city) { if (city == null) { BadRequest(); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int newCityId = _citiesDataStore.Cities.Max(c => c.Id) + 1; var newCity = new CityGetModel { Id = newCityId, Name = city.Name, Description = city.Description, NumberOfPointsOfInterest = city.NumberOfPointsOfInterest }; _citiesDataStore.Cities.Add(newCity); return(CreatedAtRoute("GetCity", new { id = newCityId }, newCity)); }
public CityGetModel GetCityById(int idn) { CityGetModel city = new CityGetModel(); //создаем подключение к БД, данные берем из конфига //string connectionString = ConfigurationManager.ConnectionStrings["Cities"].ConnectionString; string sqlExpression = "select * from Cities where id =" + idn; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если есть данные { while (reader.Read()) // построчно считываем данные { city.Id = (int)reader.GetValue(0); city.Name = (string)reader.GetValue(1); city.description = (string)reader.GetValue(2); city.NumberOfPintsOfInterest = (int)reader.GetValue(3); } } reader.Close(); } return(city); }
public Dictionary <int, CityGetModel> GetAllCities() { Dictionary <int, CityGetModel> AllCities = new Dictionary <int, CityGetModel>(); //создаем подключение к БД, данные берем из конфига //string connectionString = ConfigurationManager.ConnectionStrings["Cities"].ConnectionString; string sqlExpression = "SELECT * FROM Cities"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если есть данные { while (reader.Read()) // построчно считываем данные { CityGetModel city = new CityGetModel(); city.Id = (int)reader.GetValue(0); city.Name = (string)reader.GetValue(1); city.description = (string)reader.GetValue(2); city.NumberOfPintsOfInterest = (int)reader.GetValue(3); AllCities.Add(city.Id, city); } } reader.Close(); } return(AllCities); }
public IActionResult UpdateCity(int id, [FromBody] CityGetModel city) { if (storageOperations.isPrezent(id)) { return(Ok(storageOperations.UpdateCity(id, city))); } else { return(NotFound()); } }
public CityGetModel UpdateCity(int id, CityGetModel City) { var cityDatastore = CityDatastore.GetInstance(); CityGetModel updatedCity = cityDatastore.Cities[id]; updatedCity.Name = City.Name; updatedCity.description = City.description; updatedCity.NumberOfPintsOfInterest = City.NumberOfPintsOfInterest; return(updatedCity); }
public CityGetModel AddCity(CityCreateModel newCity) { var cityDatastore = CityDatastore.GetInstance(); CityGetModel cityGetModel = new CityGetModel(); int freeId = GetFreeId(); cityGetModel.Id = freeId; cityGetModel.Name = newCity.Name; cityGetModel.description = newCity.description; cityGetModel.NumberOfPintsOfInterest = newCity.NumberOfPintsOfInterest; cityDatastore.Cities.Add(freeId, cityGetModel); return(cityGetModel); }
private CityDatastore() { //пока заполним хранилище некоторыми значениями здесь CityGetModel city1 = new CityGetModel { Id = 1, Name = "Piter", description = "Здесь живет моя сестра", NumberOfPintsOfInterest = 100 }; CityGetModel city2 = new CityGetModel { Id = 2, Name = "Rostov", description = "Здесь живет Вася", NumberOfPintsOfInterest = 99 }; CityGetModel city3 = new CityGetModel { Id = 3, Name = "Taganrog", description = "Здесь учился я", NumberOfPintsOfInterest = 150 }; Cities.Add(city1.Id, city1); Cities.Add(city2.Id, city2); Cities.Add(city3.Id, city3); }
public CityGetModel UpdateCity(int id, CityGetModel City) { string sqlExpression = $"update Cities " + $"set Name ='{City.Name}'," + $"Description ='{City.description}'," + $"NumberOfPointsOfInterest = {City.NumberOfPintsOfInterest}" + $"where Id = {id}"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); int number = command.ExecuteNonQuery(); return(GetCityById(City.Id)); } }
public IActionResult PatchCity(int id, [FromBody] CityGetModel cityModel) { if (cityModel == null) { BadRequest(); } var citiesDataStore = CitiesDataStore.GetInstance(); var city = citiesDataStore.Cities .Where(x => x.Id == id) .FirstOrDefault(); if (city == null) { NotFound(); } var index = citiesDataStore.Cities .IndexOf(city); var patch = new JsonPatchDocument(); if (!string.IsNullOrEmpty(cityModel.Name)) { patch.Replace(nameof(cityModel.Name), cityModel.Name); } if (!string.IsNullOrEmpty(cityModel.Description)) { patch.Replace(nameof(cityModel.Description), cityModel.Description); } if (cityModel.NumberOfPointsOfInterest != citiesDataStore.Cities[index].NumberOfPointsOfInterest) { patch.Replace(nameof(cityModel.NumberOfPointsOfInterest), cityModel.NumberOfPointsOfInterest); } patch.ApplyTo(citiesDataStore.Cities[index]); return(Ok(citiesDataStore.Cities[index])); }
public IActionResult GetCity(int id) { var city = _citiesDataStore.Cities .Where(x => x.Id == id) .FirstOrDefault(); if (city == null) { return(NotFound()); } var outputCity = new CityGetModel() { Id = city.Id, Name = city.Name, Description = city.Description, NumberOfPointsOfInterest = city.NumberOfPointsOfInterest }; return(Ok(outputCity)); }
public bool isPrezent(int idn) { CityGetModel city = new CityGetModel(); bool Result = false; //создаем подключение к БД, данные берем из конфига //string connectionString = ConfigurationManager.ConnectionStrings["Cities"].ConnectionString; string sqlExpression = "select 1 from Cities where id =" + idn; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(sqlExpression, connection); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если есть данные { Result = true; } reader.Close(); } return(Result); }
public IActionResult CreateCity([FromBody] CityCreateModel cityCreateModel) { if (cityCreateModel == null) { return(BadRequest()); } if (_store.Cities.FirstOrDefault(c => c.Name == cityCreateModel.Name) != null) { return(Conflict()); } int newCityId = _store.Cities.Max(x => x.Id) + 1; CityDto cityDto = cityCreateModel.ToDto(newCityId); _store.Cities.Add(cityDto); var cityGetModel = new CityGetModel(cityDto); return(CreatedAtRoute( "GetCityById", new { id = cityDto.Id }, cityGetModel)); }