コード例 #1
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointsOfInterestForCreationDTO pointOfInterest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.SingleOrDefault(x => x.Id == cityId);

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

            var pointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
                x => x.PointsOfInterests).Max(x => x.Id);

            var finalPointOfInterest = new PointsOfInterestDTO()
            {
                Id          = ++pointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterests.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetSinglePointOfInterest", new
            {
                cityId = cityId,
                pointOfInterestId = finalPointOfInterest.Id
            }, finalPointOfInterest));
        }
コード例 #2
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointsOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

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

            var maxPointOfInterestID = CitiesDataStore.Current.Cities
                                       .SelectMany(c => c.PointsOfInterest).Max(p => p.Id);

            var newPointOfInterest = new PointsOfInterestDTO()
            {
                Id          = ++maxPointOfInterestID,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterestById", new { cityId, newPointOfInterest.Id }, newPointOfInterest));
        }
コード例 #3
0
        public IActionResult GetPointOfInterest(int cityID, int id)
        {
            CityDTO city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.ID == cityID);

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

            PointsOfInterestDTO pointOfInterest = city.PointsOfInterest.FirstOrDefault(poi => poi.ID == id);

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

            return(Ok(pointOfInterest));
        }
コード例 #4
0
        public IActionResult CreatePointOfInterest(int cityId, PointsOfInterestForCreationDTO pointOfInterest)
        {
            //For any complex validations like checking if name==description or if same POI is added earlier..we can check Fluent Validation here..
            //https://fluentvalidation.net/
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            int nextId = city.PointsOfInterest.Max(x => x.Id) + 1;
            PointsOfInterestDTO finalpoi = new PointsOfInterestDTO
            {
                Id          = nextId,
                Description = pointOfInterest.Description,
                Name        = pointOfInterest.Name
            };

            city.PointsOfInterest.Add(finalpoi);

            return(CreatedAtRoute("RouteParam",
                                  new { cityId, finalpoi.Id },
                                  finalpoi));
        }