예제 #1
0
        public IActionResult UpdateTrip(int id, [FromBody] TripUpdaterDto trip)
        {
            try
            {
                if (trip == null)
                {
                    return(BadRequest());
                }

                if (trip.Name != null && trip.Description != null)
                {
                    if (trip.Name.Trim() == trip.Description.Trim())
                    {
                        ModelState.AddModelError("Description", "Please enter a description that is different from the name");
                    }
                }

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

                if (!_tripInfoRepository.TripExists(id))
                {
                    return(NotFound());
                }

                var tripEntity = _tripInfoRepository.GetTrip(id, false);
                if (tripEntity == null)
                {
                    return(NotFound());
                }

                Mapper.Map(trip, tripEntity);

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

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while getting trip with Id {id}.", ex);
                return(StatusCode(500, "A problem occured while handling your request."));
            }
        }
예제 #2
0
        public IActionResult GetPointsOfInterest(int tripId)
        {
            try
            {
                if (!_tripInfoRepository.TripExists(tripId))
                {
                    _logger.LogInformation($"Trip with id {tripId} wasn't found when trying to access Points of Interest.");
                    return(NotFound());
                }
                var pointsOfInterestForTrip = _tripInfoRepository.GetPointsOfInterestForTrip(tripId);

                var pointsOfInterestForTripResults =
                    Mapper.Map <IEnumerable <PointsOfInterestDto> >(pointsOfInterestForTrip);

                return(Ok(pointsOfInterestForTripResults));

                //WITHOUT AutoMapper
                //var pointsOfInterestForTripResults = new List<PointsOfInterestDto>();
                //foreach (var poi in pointsOfInterestForTrip)
                //{
                //    pointsOfInterestForTripResults.Add(new PointsOfInterestDto()
                //    {
                //        Id = poi.Id,
                //        Name = poi.Name,
                //        Description = poi.Description
                //    });
                //}
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while getting points of interest for trip with Id {tripId}.", ex);
                return(StatusCode(500, "A problem occured while handling your request."));
            }
        }