public IActionResult CreatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestCreationDto obj)
        {
            if (obj.Description == obj.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            var pointOfInterestFromStore = city.PointsOfInterest.FirstOrDefault(c => c.Id == id);

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

            pointOfInterestFromStore.Name        = obj.Name;
            pointOfInterestFromStore.Description = obj.Description;

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDto obj)
        {
            /**
             * customized model validation
             */
            if (obj.Description == obj.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }
            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 finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,
                Name        = obj.Name,
                Description = obj.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
        public IActionResult poster(int cityId, [FromBody] PointOfInterestCreationDto newPointOfInterest)
        {
            var city = CitiesStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

            if (newPointOfInterest.Comment == newPointOfInterest.Name)
            {
                ModelState.AddModelError("comment", "comment should not be same as name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }                                                          //for data anotation.
            //can be automatically handle by apiConroller, but its required if custom Modelerror are added.

            var maxId = CitiesStore.Current.Cities.SelectMany(c => c.PointOfInterest).Max(p => p.Id);

            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id      = ++maxId,
                Name    = newPointOfInterest.Name,
                Comment = newPointOfInterest.Comment
            };

            city.PointOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, id = finalPointOfInterest.Id },
                                  finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            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 finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
Esempio n. 5
0
        public IActionResult CreatPointOfInterest(int cityId,
                                                  [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name");
            }

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

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

            var finalPointOfInterest = _mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            _cityInfoRepository.Save();

            var createdPointOfInterestReturn = _mapper
                                               .Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId, id = createdPointOfInterestReturn.Id },
                                  createdPointOfInterestReturn));
        }
        public IActionResult PointsOfInterest(int cityId, int id, [FromBody] PointOfInterestCreationDto PointOfInterestCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            var poitntOfInterest = city.PointsOfInterest.FirstOrDefault(c => c.Id == id);

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

            poitntOfInterest.Name        = PointOfInterestCreationDto.Name;
            poitntOfInterest.Description = PointOfInterestCreationDto.Description;
            return(NoContent());
        }
        public IActionResult PointsOfInterest(int cityId, [FromBody] PointOfInterestCreationDto PointOfInterestCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            var currentPointOfInterest = city.NumberOfPointsOfInterest;

            var pointOfInterest = new PointOfInterestDto()
            {
                Id          = ++currentPointOfInterest,
                Name        = PointOfInterestCreationDto.Name,
                Description = PointOfInterestCreationDto.Description
            };

            city.PointsOfInterest.Add(pointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = city.Id, id = currentPointOfInterest }, pointOfInterest));
        }
        public IActionResult PartialPointsOfInterest(int cityId, int id, [FromBody] JsonPatchDocument <PointOfInterestCreationDto> PointOfInterestCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            var poitntOfInterest = city.PointsOfInterest.FirstOrDefault(c => c.Id == id);

            if (poitntOfInterest == null)
            {
                return(NotFound());
            }
            var poitntOfInterestToUpdate = new PointOfInterestCreationDto()
            {
                Name        = poitntOfInterest.Name,
                Description = poitntOfInterest.Description
            };

            PointOfInterestCreationDto.ApplyTo(poitntOfInterestToUpdate, ModelState);
            poitntOfInterest.Name        = poitntOfInterestToUpdate.Name;
            poitntOfInterest.Description = poitntOfInterestToUpdate.Description;

            return(NoContent());
        }
        public IActionResult CreatePostPointOfInterest(int cityid, [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            // We can add custom modelstate rules, but we need using manual model valid
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(string.Empty, "Name have to be different from the description");
            }

            //If controller got attribute[ApiController], Asp Core validate model automatic.
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            if (!_cityInfoRepository.CityExists(cityid))
            {
                return(NotFound());
            }


            // Manual mapping, future we can use AutoMapper
            //var finalPointOfInterest = new PointOfInterestDto
            //{
            //    Id = ++maxPointOfInterest,
            //    Name = pointOfInterest.Name,
            //    Description = pointOfInterest.Description
            //};

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

            _cityInfoRepository.AddPointOfInterestForCity(cityid, finalPointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request"));
            }

            var createdPointOfInterestToReturn = Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            // Return a response with location header
            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityid = cityid, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
Esempio n. 10
0
        public IActionResult UpdatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestCreationDto poiDto)
        {
            #region do validation checks
            if (poiDto == null)
            {
                return(BadRequest());
            }

            if (poiDto.Description == poiDto.Name)
            {
                ModelState.AddModelError("Description", "can't be the same");
            }

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

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

            var poi = _cityInfoRepository.GetPOIForCity(cityId, id);

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

            #endregion

            // poi.Name = poiDto.Name;
            // poi.Description = poiDto.Description;

            //Use AutoMapper to update properties , from source ...to destination
            AutoMapper.Mapper.Map(poiDto, poi);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling Updating"));
            }
            return(NoContent()); // 204
        }
        public ActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The description should be different from the Name");
                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(i => i.Id);

            var finalPointOfInterest = new PointOfInterestDto
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Fields 'name' and 'description' can't have equal values");
            }

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

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

            _cityInfoRepository.AddPointsOfInterestForCity(cityId, newPointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem occurred while handling your request."));
            }

            var createdPointOfInterestToReturn =
                AutoMapper.Mapper.Map <Models.PointOfInterestDto>(newPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
Esempio n. 13
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDto poiDto)
        {
            //check if dto is empty
            if (poiDto == null)
            {
                return(BadRequest());
            }

            // custom validation
            if (poiDto.Description == poiDto.Name)
            {
                ModelState.AddModelError("Description", "Description and name can't be the same");
            }

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

            // validation: check against dto annotations
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var poi = AutoMapper.Mapper.Map <POI> (poiDto);

            _cityInfoRepository.AddPoiForCity(cityId, poi);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handleing your request"));
            }

            //map back to return
            var createdPoi = AutoMapper.Mapper.Map <PointOfInterestDto> (poi);

            return(CreatedAtRoute("GetPoi", new { cityId = cityId, id = createdPoi.Id }, createdPoi));
        }
Esempio n. 14
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestCreationDto pointOfInterestCreation)
        {
            if (pointOfInterestCreation == null)
            {
                return(BadRequest());
            }

            if (pointOfInterestCreation.Name == pointOfInterestCreation.Description)
            {
                ModelState.AddModelError("Description", "Name of city and city description cannot be the same.");
            }

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

            var city = _cityInfoRepository.CheckIfCityExists(cityId);

            if (!city)
            {
                return(NotFound());
            }

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterestCreation);

            _cityInfoRepository.AddPointOfInterestForCity(finalPointOfInterest, cityId);

            if (!_cityInfoRepository.SavePointOfInterest())
            {
                return(StatusCode(500, "A problem happened while handling your request"));
            }

            var createdPointOfInterestToReturn = Mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId = cityId, id = finalPointOfInterest.Id }, createdPointOfInterestToReturn));
        }
Esempio n. 15
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            // Custom Error Validation to ModelState
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "Description should be deffrent from the Name!");
            }

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

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

            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem occured while handing your request"));
            }

            var createPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestCreationDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, createPointOfInterestToReturn));
        }