コード例 #1
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Name and Description should not be same");
            }

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

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(_city => _city.Id == cityId);

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

            var MaxPoiId = CitiesDataStore.Current.Cities.SelectMany(_city => _city.PointsOfInterests).Max(poi => poi.PId);
            PointsOfInterestsDto newPoi = new PointsOfInterestsDto()
            {
                PId         = ++MaxPoiId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterests.Add(newPoi);

            return(CreatedAtRoute("GetPointsOfInterests", new { cityID = cityId, pid = newPoi.PId }, newPoi));
        }
        public IActionResult GetPointOfInterest(int cityId, int id)
        {
            if (!_cityInfoRepository.CityExist(cityId))
            {
                return(NotFound());
            }

            var pointofInterest = _cityInfoRepository.GetPointOfInterestForCity(cityId, id);

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

            var pointofInterestResult = new PointsOfInterestsDto()
            {
                Id          = pointofInterest.Id,
                Name        = pointofInterest.Name,
                Description = pointofInterest.Description
            };

            return(Ok(pointofInterestResult));

            //var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if (city == null)
            //{
            //    return NotFound();
            //}

            //var interest = city.PointsOfInterests.FirstOrDefault(i => i.Id == id);
            //if (interest == null)
            //{
            //    return NotFound();
            //}
            //return Ok(interest);
        }