public IActionResult CreatePointOfIntest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfIntest)
        {
            if (pointOfIntest.Description == pointOfIntest.Name)
            {
                ModelState.AddModelError("Description", "description should be  different from name");
            }

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

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

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

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_cityInfoRepository.save())
            {
                return(StatusCode(500, "Error while handling your request .Please try again"));
            }

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

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
示例#2
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name.");
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //demo purposes - to be improved
            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, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
示例#3
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Name and Description can't be same.");
            }

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

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

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

            var maxPointOfINterest = city.PointOfInterest.Max(p => p.Id);

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

            city.PointOfInterest.Add(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
示例#4
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointofinterest)
        {
            if (pointofinterest == null)
            {
                return(BadRequest());
            }
            if (pointofinterest.Name == pointofinterest.Description)
            {
                ModelState.AddModelError("Description", "The name & description must not be same");
            }
            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 happened while handling your request"));
            }
            var createdPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
            {
                cityId = cityId,
                id = finalPointOfInterest.Id
            }, createdPointOfInterestToReturn));
        }
示例#5
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)            //Input Validation
            {
                return(BadRequest(ModelState)); //Return Bad request with the corresponding messages
            }

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

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

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

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

            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId = cityId, pointId = createdPointOfInterest.Id }, createdPointOfInterest));
        }
示例#6
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "Please provide different description than name.");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(x => x.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            var lastPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id);
            var pointOfInterestToAdd  = new PointsOfInterestDto
            {
                Id          = ++lastPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(pointOfInterestToAdd);

            return(CreatedAtRoute("GetPointOfInterestOfACity", new { cityId = cityId, id = pointOfInterestToAdd.Id }, pointOfInterestToAdd));
        }
示例#7
0
        public IActionResult CreatePointOfInterest(int cityId, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.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(p => p.Id == id);

            if (pointOfInterestFromStore == null)
            {
                return(NotFound());
            }
            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;
            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityid, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

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

            _cityRepo.AddPointOfInterestForCity(cityid, finalPoi);
            _cityRepo.Save();

            var createdPoi = _mapper.Map <Models.PointOfInterestDto>(finalPoi);

            // validating the input that way
            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityid = cityid, id = createdPoi.Id },
                       createdPoi));
        }
        [HttpPut("{id}")]    // id of the poi that we will update
        public IActionResult UpdatePointOfInterest(int cityid, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

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

            var poiEntity = _cityRepo.GetPointOfInterestForCity(cityid, id);

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

            _mapper.Map(pointOfInterest, poiEntity);

            _cityRepo.UpdatePointOfInterestForCity(cityid, poiEntity);
            _cityRepo.Save();

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointsOfInterest)
        {
            if (pointsOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (pointsOfInterest.Name == pointsOfInterest.Description)
            {
                //   ModelState.AddModelError("Description", "The provided description should be different from name");
                return(BadRequest("The provided description should be different from name"));
            }

            var finalPointOfInterest = Mapper.Map <Entity.PointsOfInterest>(pointsOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            //city.PointOfInterests.Add(finalPointOfInterest);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

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


            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId = cityId, id = finalPointOfInterest }, createdPointOfInterestToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The description and name should be different");
            }

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

            if (!VerifyCityExists(cityId))
            {
                return(NotFound());
            }

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            _cityInfoRepository.Save();

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

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = newPointOfInterest.Id }, newPointOfInterest));
        }
示例#12
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_cityInfoRepository.DoesCityExist(cityId))
            {
                _logger.LogWarning($"The city with id={cityId} was not found.");
                return(NotFound());
            }
            var newPointOfInterst = Mapper.Map <PointsOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, newPointOfInterst);
            if (!_cityInfoRepository.Save())
            {
                return(StatusCode(500, "Error while trying to save new point of interest"));
            }
            return(CreatedAtRoute("GetPointsOfInterest", new { cityId = newPointOfInterst.Id, id = newPointOfInterst.Id },
                                  Mapper.Map <PointOfInterestDto>(newPointOfInterst)));
        }
示例#13
0
        public IActionResult createPointOfInteres(int cityId,
                                                  [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (city == null)
            {
                return(NotFound());
            }
            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
                c => c.PointOfInterest).Max(p => p.Id);
            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointOfInterest.Add(finalPointOfInterest);
            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The Provide Description Must be different than the Name Lolz"
                    );
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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


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


            cityInfo.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            cityInfo.save();
            var createdPointOfInterestToReturn = mapper.Map <PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute(
                       "GetPointOfInterest", new { cityId, id = createdPointOfInterestToReturn.ID },
                       createdPointOfInterestToReturn
                       ));
        }
        public IActionResult CreatePointOfInterest(Guid cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                _logger.LogInformation($"Point of interest is null");
                return(BadRequest());
            }

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The Provided description should be different from the name");
            }

            if (!_cityInfoRepository.CityExist(cityId))
            {
                _logger.LogInformation($"City with id {cityId} was not found when creating points of interest");
                return(NotFound());
            }

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, FinalPointOfInterest);

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

            var CreatedPointOfInterest = Mapper.Map <PointOfInterestDto>(FinalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest",
                                  new { cityId, id = CreatedPointOfInterest.Id },
                                  CreatedPointOfInterest));
        }
        public IActionResult updatePointOfInterest(int cityId, int id,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));//We return the error message if field is empty
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

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

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

            pointOfInterestFromStore.Name        = pointOfInterest.Name;
            pointOfInterestFromStore.Description = pointOfInterest.Description;

            return(NoContent());
        }
        public IActionResult Add(int cityId, PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }


            var city = _cityDataStore.Cities.Where(a => a.Id == cityId).FirstOrDefault();

            if (city == null)
            {
                return(BadRequest());
            }
            var maxPointOfInterestId = city.PointsOfInterest.Max(a => a.Id);
            var newPointOfInterest   = new PointOfInterestDto()
            {
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description,
                Id          = ++maxPointOfInterestId
            };

            city.PointsOfInterest.Add(newPointOfInterest);
            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = newPointOfInterest.Id }, newPointOfInterest));
        }
        public IActionResult UpdatePointOfInterestFull(int cityId, int id, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The description and name should be different");
            }

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

            if (!VerifyCityExists(cityId))
            {
                return(NotFound(CityNotFound));
            }

            var pointOfInterestToUpdate = _cityInfoRepository.GetPointOfInterest(cityId, id);

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

            _mapper.Map(pointOfInterest, pointOfInterestToUpdate);

            _cityInfoRepository.Save();

            return(NoContent());
        }
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto 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 <PointOfInterest>(pointOfInterest);

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            _cityInfoRepository.Save();

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

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId, id = createdPointOfInterestToReturn.Id },
                       createdPointOfInterestToReturn));
        }
示例#20
0
        public IActionResult createPointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointsOfInterest)
        {
            if (pointsOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointsOfInterest.Name == pointsOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The provided description should be diferent from the name");
            }

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

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

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

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

            //AUTO MAPPING
            //Map a entity from a dto(json object income)
            var finalPointOfInterest = Mapper.Map <Entities.PointOfInterest>(pointsOfInterest);

            //demo, we dont need it because id is auto generated
            // var maxPointOfIterestId = CitiesDataStore.Current.Cities.SelectMany(
            //     c => c.PointsOfInterest).Max(p => p.Id);

            //MANUAL MAP
            // var finalPointOfInterest = new PointsOfInterestDto()
            // {
            //     Id = ++maxPointOfIterestId,
            //     Name =  pointsOfInterest.Name,
            //     Description = pointsOfInterest.Description
            // };

            //city.PointsOfInterest.Add(finalPointOfInterest);
            _cityInfoRepository.AddPointOfIterestForCity(cityId, finalPointOfInterest);

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

            var createdPointOfInterest = Mapper.Map <models.PointsOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, Id = finalPointOfInterest.Id }, createdPointOfInterest));
        }
示例#21
0
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            if (pointOfInterest.Name == pointOfInterest.Description)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name");
            }

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

            // Use map handmade

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

            //// demo purposes - to be improved
            //var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(c => c.PointOfInterest).Max(p => p.Id);

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

            //city.PointOfInterest.Add(finalPointOfInterest);
            //return CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest);

            // =================================================== //

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

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

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

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

            return(CreatedAtRoute("GetPointOfInterest", new { city = cityId, Id = createPointOfInterestToReturn.Id }, createPointOfInterestToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            // [FromBody]  - allows us to get the content of the request. The request body will contain the information needed
            // to create a new point of interest. We want to serialize this point of interest dto.


            // because we are expecting a well formed PointOfInterestForCreationDto json object to be passed in we need to do some validation first
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }

            //validate the incoming PointOfInterestForCreationDto has valid properties


            // we can of course create our own validation on the object like so:

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different than the name.");
            }

            if (!ModelState.IsValid)
            {
                //return BadRequest(); // if we just want to return a bad request then we can just use this

                return(BadRequest(ModelState)); // if we want to include the property error messages (in the response body) that are defined on the
                                                // PointOfInterestForCreationDto model then we need to include the model state.
            }


            // make sure the city exists
            CityDto city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

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

            // get the last POI ID so we can create the new one
            int lastPointOfInterestID = CitiesDataStore.Current.Cities.SelectMany(c => city.PointsOfInterest).Max(p => p.Id);

            // map the POI for creation object to a DTO that we will use
            PointOfInterestDto newPointOfInterestDto = new PointOfInterestDto()
            {
                Id          = ++lastPointOfInterestID,
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            city.PointsOfInterest.Add(newPointOfInterestDto);

            // for posts, its recommended to return a 201 Created response, we can return this using a the built in helper methods.
            // This helper response method will allow us to add a location header to the response, this will contain the new location URI where
            // the newly created information can be found.
            return(CreatedAtRoute("GetPointOfInterestReferenceName", new { cityId = cityId, id = newPointOfInterestDto.Id }, newPointOfInterestDto));
        }
示例#23
0
        public IActionResult CreaePointOfInterest(int cityId,
                                                  [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            var cityfound = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (cityfound == null)
            {
                return(NotFound());
            }
        }
 public PointOfInterestDto AddPointOfInterestForCity(int cityId, PointOfInterestForCreationDto pointOfInterestDto)
 {
     try
     {
         return(_pointsOfInterestOperation.AddPointOfInterestForCity(cityId, pointOfInterestDto));
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "AddPointOfInterestForCity error!!!");
         throw;
     }
 }
示例#25
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be differnt from name");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            /*  if (city == null)
             * {
             *    return NotFound();
             * }*/
            if (!_cityInforepository.CityExists(cityId))
            {
                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
             * };*/

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

            _cityInforepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            if (!_cityInforepository.Save())
            {
                return(StatusCode(500, "Problem tokom rukovamnja zahtevom"));
            }

            // city.PointsOfInterest.Add(finalPointOfInterest);
            var createPointOfInterestToReturn = Mapper.Map <Models.PointOfInterestDto>(finalPointOfInterest);

            return(CreatedAtRoute("GetPointOfInterest", new
                                  { cityId = cityId, id = createPointOfInterestToReturn.Id }, createPointOfInterestToReturn));
        }
        public IActionResult CreatePointOfInterest(int cityId, [FromBody] PointOfInterestForCreationDto 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());
            }

            #region in memory data (commented out)
            //var city = CitiesMockData.Current.Cities.FirstOrDefault(c => c.Id == cityId);
            //if (city == null)
            //{
            //    return NotFound();
            //}

            //var maxPointOfInterestId = CitiesMockData.Current.Cities.SelectMany(c => c.PointsOfInterest).Max(p => p.Id);
            #endregion

            #region manually mapping

            //var finalPointOfInterest = new PointOfInterestDto()
            //{
            //    Id = ++maxPointOfInterestId,
            //    Name = pointOfInterest.Name,
            //    Description = pointOfInterest.Description
            //city.PointsOfInterest.Add(finalPointOfInterest);
            //return CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest);
            //};

            #endregion

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);

            // if something goes wrong here it will produces a server 500 error
            _cityInfoRepository.Save();

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

            // name of the IActionResult   // we pass into an anonymous object the parameters   // finalPointOfInterest is the newly created object passed into the body
            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn));
        }
示例#27
0
        public IActionResult CreatPointOfInterest(int cityId,
                                                  [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            // Error: Bad request
            if (pointOfInterest == null)
            {
                return(BadRequest());
            }
            // if description and name are the same, return an error
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name.");
            }
            // make sure the request is valid according to our 'PointOfInterestForCreationDto' [required] blocks
            // it will also check to see if ModelState is set to invalid inside of this function, e.g., ModelState.AddModelError
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState)); // ModelState tells you the specifically what is wrong, in the response for the request. For customized error handling + default
            }
            // ---
            // Error: Trying to add a point of interest to a city that does not exist
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }
            // ---
            // Bad Practice, will be improved: Calculate the number of cities we have by looping through all of them and finding the one with the highest Id
            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(
                c => c.PointsOfInterest).Max(p => p.Id);

            // Create the new point of interest object
            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId, // add one more to the current value, which we will use for the new point of interest
                Name        = pointOfInterest.Name,
                Description = pointOfInterest.Description
            };

            // add the new point of interest
            city.PointsOfInterest.Add(finalPointOfInterest);

            // return our status code CreatedAtRoute which requires route(city) value, and the created object value (finalPointOfInterest)
            // note: getpoint ofinterest refers to the id that was sent in the GET route
            return(CreatedAtRoute("GetPointOfInterest", new { cityId = cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));
        }
示例#28
0
        public IActionResult CreatePointOfInterest(int cityId,
                                                   [FromBody] PointOfInterestForCreationDto pointOfInterest) // FromBody is optional, the ApiController takes care of that
        {
            //if (pointOfInterest == null)
            //{
            //    return BadRequest();
            //} // this is taken care of by ApiController Attribute

            //if (pointOfInterest.Name == null)
            //{
            //    return BadRequest();
            //} // better way: model attributes

            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",  // key, could be property name, not must
                    "The provided description should be different from the name");
            } // add our custom validation in ModelState

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState)); // ModelState will be deseriallized in the response body
            }
            // the ApiController will do that for us in case of automatic validation by Attribute
            // in case of custom added validation, we do need to check the ModelState
            // because it is already too late for the ApiController to handle this
            // model binding has already occurred

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

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            _cityInfoRepository.Save();

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

            return(CreatedAtRoute(
                       "GetPointOfInterest",
                       new { cityId, id = createdPointOfInterestToReturn.Id },
                       createdPointOfInterestToReturn));
        }
示例#29
0
        public IActionResult CreatePointOfInterest(int cityId, PointOfInterestForCreationDto pointOfInterest)
        {
            //---------------------------------------------------->
            // This section of code is custom validation to check if the name matches the description
            if (pointOfInterest.Description == pointOfInterest.Name)
            {
                ModelState.AddModelError(
                    "Description",
                    "The provided description should be different from the name"
                    );
            }

            // ModelState must be manually thrown if you add a Model Error
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //---------------------------------------------------->

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

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

            // Amp through all POI and get the highest value
            var maxPointOfInterestId = CitiesDataStore.Current.Cities.SelectMany(item => item.PointOfInterest).Max(poi => poi.Id);

            var finalPointOfInterest = new PointOfInterestDto()
            {
                Id          = ++maxPointOfInterestId,     // increment the existing id by +1
                Name        = pointOfInterest.Name,       // pointOfInterest.Name = user input
                Description = pointOfInterest.Description // pointOfInterest.Description = user input
            };

            city.PointOfInterest.Add(finalPointOfInterest); // Add the result into List<PointOfInterest> defined in CitiesDto

            // CreatedAtRoute(routeName, routeValues, objectValues)
            return(CreatedAtRoute("GetPointOfInterest", new { cityId, id = finalPointOfInterest.Id }, finalPointOfInterest));

            /* To execute an POST request, it requires 201 response code.
             *
             * CreatedAtRoute() returns a location header
             */
        }
示例#30
0
        public IActionResult CreatePointOfInterest(int cityId,
            [FromBody] PointOfInterestForCreationDto pointOfInterest)
        {
            if (!_cityInfoRepository.CityExists(cityId))
            {
                return NotFound();
            }

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

            _cityInfoRepository.AddPointOfInterestForCity(cityId, finalPointOfInterest);
            _cityInfoRepository.Save();

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

            return CreatedAtRoute(nameof(GetPointOfInterest), new { cityId = cityId, id = result.Id }, result);
        }