Example #1
0
        public IActionResult PatchCity(
            int id,
            [FromBody] JsonPatchDocument <CityModifyModel> patchDoc)
        {
            if (patchDoc == null)
            {
                BadRequest();
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            var city = citiesDataStore.Cities
                       .Where(x => x.Id == id)
                       .FirstOrDefault();

            if (city == null)
            {
                NotFound();
            }

            var cityModelToPatch = new CityModifyModel(city);

            patchDoc.ApplyTo(cityModelToPatch);

            city.Name        = cityModelToPatch.Name;
            city.Description = cityModelToPatch.Description;
            city.NumberOfPointsOfInterest = cityModelToPatch.NumberOfPointsOfInterest;

            return(NoContent());
        }
Example #2
0
        public IActionResult AddCity([FromBody] CityCreateModel cityCreateModel)
        {
            if (cityCreateModel == null)
            {
                return(BadRequest());
            }

            //собственная проверка
            //if (cityCreateModel.Name == cityCreateModel.Description)
            //	ModelState.AddModelError("Custom", "Description wrong");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            int  id   = citiesDataStore.Cities.Keys.Max() + 1;
            City city = cityCreateModel.ConvertToCity(id);

            citiesDataStore.Cities.Add(city.Id, city);

            return(Created($"/api/cities/{id}", new CityGetModel(city)));
        }
Example #3
0
        public IActionResult GetCities()
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var cities          = citiesDataStore.Cities;

            return(Ok(cities));
        }
        public IActionResult PutCity(int id, [FromBody] CityCreateModel 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);

            citiesDataStore.Cities[index].Name        = cityModel.Name;
            citiesDataStore.Cities[index].Description = cityModel.Description;
            citiesDataStore.Cities[index].NumberOfPointsOfInterest = cityModel.NumberOfPointsOfInterest;

            return(Ok(cityModel));
        }
Example #5
0
        public void DeleteCity([FromBody] CityDeleteModel city)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var cityId          = city.Id;

            citiesDataStore.Citites.RemoveAt(Convert.ToInt32(cityId));
        }
Example #6
0
        public IActionResult AddCity([FromBody] City city)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            citiesDataStore.Cities.Add(city);
            return(Created("/api/cities/" + city.Id, city));
        }
        public JsonResult GetCities()
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var cities          = citiesDataStore.Cities;

            return(new JsonResult(cities));

            /*return new JsonResult(
             *      new List<City>
             *      {
             *              new City
             *              {
             *                      Id=1,
             *                      Name = "Moscow",
             *                      Description = "The capital of Russia"
             *              },
             *
             *              new City
             *              {
             *                      Id=2,
             *                      Name = "New York",
             *                      Description = "The one of the biggest cities in the world"
             *              },
             *      });
             */
        }
Example #8
0
        public IActionResult GetCities()
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            return(Ok(
                       citiesDataStore.Cities
                       .Select(c => new CityGetModel(c))));
        }
Example #9
0
        //экшн кот вызывается внешним запросом, в коде это метод
        public IActionResult GetCities()
        {
            CitiesDataStore citiesDataStore = CitiesDataStore.GetInstance();
            List <City>     cities          = citiesDataStore.Cities;

            //var result = new JsonResult(cities);
            //result.StatusCode = 200;
            //return result;
            return(Ok(cities));
        }
        public IActionResult GetCities()
        {
            var _store = CitiesDataStore.GetInstance();
            var cities = _store.Cities;
            var models = cities.Select(c => new CityGetModel(c.Id, c.Name, c.Description, c.NumberOfPoi)).ToList();

            var result = Ok(models);

            return(result);
        }
        public IActionResult AddCity([FromBody] CityCreateOrUpdateModel cityCreateModel)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            var  id   = citiesDataStore.Cities.Keys.Max() + 1;
            City city = cityCreateModel.ConverToCity(id);

            citiesDataStore.Cities.Add(city.Id, city);

            return(Created($"/api/cities/{id}" + city.Id, new CityGetModel(city)));
        }
        public IActionResult GetCities()
        {
            var citiesDataStore   = CitiesDataStore.GetInstance();
            var citiyGetModelList =
                citiesDataStore.Cities
                .Values
                .Select(x => new CityGetModel(x))
                .ToList();

            return(Ok(citiyGetModelList));
        }
        public IActionResult GetCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            if (citiesDataStore.Cities.ContainsKey(id))
            {
                City city = citiesDataStore.Cities[id];
                return(Ok(new CityGetModel(city)));
            }
            return(NotFound());
        }
        public IActionResult DeleteCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            if (citiesDataStore.Cities.ContainsKey(id))
            {
                citiesDataStore.Cities.Remove(id);
                return(NoContent());
            }

            return(NotFound());
        }
        public IActionResult GetCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var city            = citiesDataStore.Cities
                                  .Where(x => x.Id == id)
                                  .FirstOrDefault();

            if (city == null)
            {
                return(NotFound());
            }

            return(Ok(city));
        }
Example #16
0
        public IActionResult GetCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            foreach (var city in citiesDataStore.Cities)
            {
                if (city.Id == id)
                {
                    return(Ok(city));
                }
            }

            return(NotFound());
        }
Example #17
0
        public IActionResult DeleteCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            foreach (var city in citiesDataStore.Cities)
            {
                if (city.Id == id)
                {
                    citiesDataStore.Cities.Remove(city);
                    return(NotFound());
                }
            }
            return(Ok(citiesDataStore.Cities));
        }
        public IActionResult ReplaceCity(int id, [FromBody] CityCreateOrUpdateModel cityCreateOrUpdateModel)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            if (citiesDataStore.Cities.ContainsKey(id))
            {
                citiesDataStore.Cities[id].Name        = cityCreateOrUpdateModel.Name;
                citiesDataStore.Cities[id].Description = cityCreateOrUpdateModel.Description;
                citiesDataStore.Cities[id].NumberOfPointsOfInterest = cityCreateOrUpdateModel.NumberOfPointsOfInterest;
                return(Ok(new CityGetModel(citiesDataStore.Cities[id])));
            }

            return(NotFound());
        }
Example #19
0
        public IActionResult DeleteCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var city            = citiesDataStore.Cities
                                  .Where(x => x.Id == id)
                                  .FirstOrDefault();

            if (city == null)
            {
                NotFound();
            }

            citiesDataStore.Cities.Remove(city);

            return(NoContent());
        }
        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 _store = CitiesDataStore.GetInstance();
            var city   = _store.Cities
                         .Where(c => c.Id == id)
                         .FirstOrDefault();

            //var result= new JsonResult(city);
            //if (city == null)
            //	result.StatusCode = 404;
            //else result.StatusCode = 200;

            //return result;

            if (city == null)
            {
                return(NotFound());
            }
            return(Ok(new CityGetModel(city.Id, city.Name, city.Description, city.NumberOfPoi)));
        }
        public IActionResult CreateCity([FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            var citiesDataStore = CitiesDataStore.GetInstance();
            int newCityId       = citiesDataStore.Cities.Max(x => x.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));
        }
Example #23
0
        public IActionResult GetCity(int id)
        {
            CitiesDataStore citiesDataStore = CitiesDataStore.GetInstance();
            //foreach(City city in citiesDataStore.Cities)
            //{
            //	if(city.Id == id)
            //	{
            //		return new JsonResult(city);
            //	}
            //}
            //return new JsonResult(null);
            //или так
            //return new JsonResult(citiesDataStore.Cities.FirstOrDefault(с => с.Id == id));

            var city = citiesDataStore.Cities.FirstOrDefault(c => c.Id == id);

            if (city == null)
            {
                return(NotFound());
            }
            return(Ok(city));
        }
Example #24
0
        public IActionResult ReplaceCity(int id, [FromBody] CityReplaceModel cityModel)
        {
            if (cityModel == null)
            {
                BadRequest();
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            var city = citiesDataStore.Cities
                       .Where(x => x.Id == id)
                       .FirstOrDefault();

            if (city == null)
            {
                NotFound();
            }

            city.Name        = cityModel.Name;
            city.Description = cityModel.Description;
            city.NumberOfPointsOfInterest = cityModel.NumberOfPointsOfInterest;

            return(NoContent());
        }
        public IActionResult UpdateCity(int id, [FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                BadRequest();
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            var updatedCity = citiesDataStore.Cities
                              .Where(x => x.Id == id)
                              .FirstOrDefault();

            if (updatedCity == null)
            {
                NotFound();
            }

            updatedCity.Name        = updatedCity.Name;
            updatedCity.Description = updatedCity.Description;
            updatedCity.NumberOfPointsOfInterest = updatedCity.NumberOfPointsOfInterest;

            return(Ok(city));
        }
Example #26
0
 public CitiesController()
 {
     _store = CitiesDataStore.GetInstance();
 }