public IActionResult CreatePoint(int cityId, [FromBody] PointOfInterestCreationDTO pointOfInterestCreationDTO)
        {
            if (pointOfInterestCreationDTO == null)
            {
                return(BadRequest());
            }

            var city = cityInfoRepository.GetCity(cityId);

            if (city == null)
            {
                return(NotFound());
            }
            if (pointOfInterestCreationDTO.Name == pointOfInterestCreationDTO.Description)
            {
                ModelState.AddModelError("Description", "Description musr be differ");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var pointOfInterest = AutoMapper.Mapper.Map <PointOfInterest>(pointOfInterestCreationDTO);

            cityInfoRepository.AddPointOfInterest(cityId, pointOfInterest);

            return(CreatedAtRoute("Point", new { cityId = city.Id, id = pointOfInterest.Id }, pointOfInterest));
        }
Пример #2
0
        public IActionResult UpdatePOI(int cityId, int poiId, [FromBody] PointOfInterestCreationDTO poi)
        {
            if (poi == null)
            {
                return(BadRequest());
            }

            if (!_cityRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            if (poi.Description == poi.Name)
            {
                ModelState.AddModelError("Description", "Name and Description should be different");
            }

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

            var city        = CitiesDataStore.Current.cities.FirstOrDefault(x => x.Id == cityId);
            var poiToUpdate = city.POI.FirstOrDefault(x => x.Id == poiId);

            poiToUpdate.Name        = poi.Name;
            poiToUpdate.Description = poi.Description;

            return(NoContent());
        }
Пример #3
0
        public IActionResult CreatePOI(int cityId, [FromBody] PointOfInterestCreationDTO poi)
        {
            if (poi == null)
            {
                return(BadRequest());
            }

            if (!_cityRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            if (poi.Description == poi.Name)
            {
                ModelState.AddModelError("Description", "Name and Description should be different");
            }

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

            var city = CitiesDataStore.Current.cities.FirstOrDefault(x => x.Id == cityId);

            PointOfInterestDTO poiObj = new PointOfInterestDTO()
            {
                Id          = city.NumberOfPointOfInterst + 1, //temporary
                Name        = poi.Name,
                Description = poi.Description
            };

            city.POI.Add(poiObj);

            return(CreatedAtRoute("GetPoiRoute", new { cityId = cityId, pId = poiObj.Id }, poiObj));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDTO pointofInterest)
        {
            if (pointofInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }
            var city = CitiesDataStore.current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            var MaxPoindOfInterestId = CitiesDataStore.current.Cities.SelectMany(c => c.pointofInterest).Max(p => p.Id);

            var finalPointOfDTO = new PointOfInterestDTO()
            {
                Id          = ++MaxPoindOfInterestId,
                Name        = pointofInterest.Name,
                Description = pointofInterest.Description
            };

            city.pointofInterest.Add(finalPointOfDTO);
            return(CreatedAtRoute("getPoindOfInterest", new { cityId = cityId, id = finalPointOfDTO.Id }));
            //return Ok();
        }
        public IActionResult CreatePointOfInterest(int cityID,
                                                   [FromBody] PointOfInterestCreationDTO pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description cannot be the same as the name");
            }
            //TODO: Check FluentValidation

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

            var city = CitiesDataStore.Current.Cities.Where(c => c.ID == cityID).FirstOrDefault();

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

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

            var finalPointOfInterest = new PointOfInterestDTO()
            {
                ID          = maxPointOfInterestID + 1,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
            {
                cityID,
                poiID = finalPointOfInterest.ID
            }, finalPointOfInterest));
        }
Пример #6
0
        public IActionResult PartialUpdatePOI(int cityId, int poiId, [FromBody] JsonPatchDocument <PointOfInterestCreationDTO> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            if (!_cityRepository.CityExists(cityId))
            {
                return(NotFound());
            }

            var city        = CitiesDataStore.Current.cities.FirstOrDefault(x => x.Id == cityId);
            var poiToUpdate = city.POI.FirstOrDefault(x => x.Id == poiId);

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

            var poiToPatch = new PointOfInterestCreationDTO()
            {
                Name        = poiToUpdate.Name,
                Description = poiToUpdate.Description
            };

            patchDoc.ApplyTo(poiToPatch, ModelState);

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

            //map the patched obj with the actual object and store
            poiToUpdate.Name        = poiToPatch.Name;
            poiToUpdate.Description = poiToPatch.Description;

            return(NoContent());
        }