示例#1
0
        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 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"
             *              },
             *      });
             */
        }
示例#3
0
        public IActionResult GetCities()
        {
            var citiesDataStore = CitiesDataStore.GetCityDataStoreInstance();
            var cities          = citiesDataStore.Cities;

            return(Ok(cities));
        }
示例#4
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)));
        }
示例#5
0
        public IActionResult UpdateCity([FromBody] CityUpdateModel cityForUpdate, int id)
        {
            var citiesDataStore = CitiesDataStore.GetCityDataStoreInstance();

            foreach (var city in citiesDataStore.Cities)
            {
                if (id == city.Id)
                {
                    if (city.Name == null)
                    {
                        return(BadRequest($"Property {nameof(city.Name) } can not have a null value"));
                    }

                    else
                    {
                        if (city.Name != cityForUpdate.Name)
                        {
                            city.Name = cityForUpdate.Name;
                        }
                        if (city.Description != cityForUpdate.Description)
                        {
                            city.Description = cityForUpdate.Description;
                        }
                        if (city.NumberOfPointsInterest != cityForUpdate.NumberOfPointsInterest)
                        {
                            city.NumberOfPointsInterest = cityForUpdate.NumberOfPointsInterest;
                        }
                    }
                }
            }

            return(Ok(cityForUpdate));
        }
        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));
        }
示例#7
0
        public IActionResult AddCity([FromBody] City city)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            citiesDataStore.Cities.Add(city);
            return(Created("/api/cities/" + city.Id, city));
        }
示例#8
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());
        }
示例#9
0
        public void DeleteCity([FromBody] CityDeleteModel city)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();
            var cityId          = city.Id;

            citiesDataStore.Citites.RemoveAt(Convert.ToInt32(cityId));
        }
示例#10
0
        public IActionResult GetCities()
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            return(Ok(
                       citiesDataStore.Cities
                       .Select(c => new CityGetModel(c))));
        }
        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);
        }
示例#12
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 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 GetPointsOfInterest(int cityID)
        {
            var temp = new CitiesDataStore().Cities.Find(c => c.id == cityID);

            if (temp == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(temp.pointOfInterests));
            }
        }
示例#18
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());
        }
示例#19
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));
        }
示例#20
0
        public IActionResult GetCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetCityDataStoreInstance();
            var city            = citiesDataStore.Cities
                                  .Where(x => x.Id == id)
                                  .FirstOrDefault();

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

            return(Ok(city));
        }
示例#21
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Cities API", Version = "V1"
                });
            });

            ICitiesDataStore dataStoreService = new CitiesDataStore();

            services.AddSingleton(dataStoreService);
        }
示例#22
0
        public IActionResult DeleteCity(int id)
        {
            var citiesDataStore = CitiesDataStore.GetCityDataStoreInstance();

            if (citiesDataStore.Cities.Exists(x => x.Id == id))
            {
                citiesDataStore.Cities.Remove(citiesDataStore.Cities.Find(x => x.Id == id));
            }
            else
            {
                return(NotFound($"Object city with id = {id} not found"));
            }
            return(Ok());
        }
        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());
        }
示例#24
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());
        }
示例#25
0
        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)));
        }
示例#27
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));
        }
示例#28
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());
        }
示例#29
0
        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));
        }
示例#30
0
 public CityController(CitiesDataStore dataStore)
 {
     _dataStore = dataStore;
 }