/// <summary> Return the city point of interest by id </summary>
        private PointOfInterestDto GetPointOfInterest(CityDto city, int id)
        {
            var pointOfInterest = city.PointsOfInterest.FirstOrDefault(p => p.Id == id);

            return(pointOfInterest);
        }
Beispiel #2
0
        public void Post(CityDto dto)
        {
            // this.context.Cities.Add(new City { Name = dto.Name, Description = dto.Description });

            // this.context.SaveChanges();
        }
Beispiel #3
0
        public IActionResult CreateCity([FromBody] CityForCreationDto city)
        {
            //the new city is in the POST body with no id (which is generated by SQL)

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

            //add custom validation before checking data annotation rules
            if (city.Name == city.Description)
            {
                ModelState.AddModelError("Description", "The provided description must be different than the name.");
            }

            //validate data annotations on the model, returning error messages in the response
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //begin hardcoded data
            ////create a new city and add it to the collection
            //var maxCityId = CitiesDataStore.Current.Cities.Max(c => c.Id);

            //var cityDto = new CityDto
            //{
            //    Id = ++maxCityId, //make a fake new id
            //    Name = city.Name,
            //    Description = city.Description
            //};

            //CitiesDataStore.Current.Cities.Add(cityDto);
            //end hardcoded data

            //create a new entity
            var cityEntity = new City
            {
                Name        = city.Name,
                Description = city.Description
            };

            //add entity and save to db
            _cityInfoRepository.AddCity(cityEntity);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            //map entity to dto (method must return a dto, not an entity)
            var cityDto = new CityDto
            {
                Id          = cityEntity.Id, //save populated Id identity property
                Name        = cityEntity.Name,
                Description = cityEntity.Description
            };

            //return a 201
            //named route and parameters to get new City are used to build a url in the Location response header
            //the City just created will be in response body
            return(CreatedAtRoute("GetCity", new { id = cityDto.Id }, cityDto));
        }
        private PointOfInterestDto FindPointOfInterest(CityDto city, int id)
        {
            var pointOfInterestFromStore = city.PointsOfInterests.FirstOrDefault(p => p.Id == id);

            return(pointOfInterestFromStore);
        }