public IActionResult CreateDestinationCollection( [FromBody] IEnumerable <DestinationForCreationDto> destinationCollection) { if (destinationCollection == null) { return(BadRequest()); } var destinationEntities = Mapper.Map <IEnumerable <Destination> >(destinationCollection); foreach (var destination in destinationEntities) { _tripInfoRepository.AddDestination(destination); } if (!_tripInfoRepository.Save()) { throw new Exception("Creating an destination collection failed on save."); } var destinationCollectionToReturn = Mapper.Map <IEnumerable <DestinationDto> >(destinationEntities); var idsAsString = string.Join(",", destinationCollectionToReturn.Select(a => a.Id)); return(CreatedAtRoute("GetDestinationCollection", new { ids = idsAsString }, destinationCollectionToReturn)); //return Ok(); }
public IActionResult CreateTrip([FromBody] TripWithPointsOfInterestCreatorDto trip, bool includePointsOfInterest = false) { try { if (trip == null) { return(BadRequest()); } if (trip.Name.Trim() == trip.Description.Trim()) { ModelState.AddModelError("Description", "Please enter a description that is different from the name"); } if (includePointsOfInterest) { if (trip.PointsOfInterest == null) { ModelState.AddModelError("Description", "Please enter a Point Of Interest or change the includePointsOfInterest parameter to false"); } } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var finaltrip = Mapper.Map <Entities.Trip>(trip); _tripInfoRepository.AddTrip(finaltrip); if (!_tripInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdTripToReturn = Mapper.Map <Models.TripDto>(finaltrip); return(CreatedAtRoute("GetTrip", new { id = createdTripToReturn.Id }, createdTripToReturn)); } catch (Exception ex) { _logger.LogCritical($"Exception while creating trip.", ex); return(StatusCode(500, "A problem occured while handling your request.")); } }
public IActionResult CreatePointOfInterest(int tripId, [FromBody] PointsOfInterestCreatorDto pointOfInterest) { try { if (pointOfInterest == null) { return(BadRequest()); } if (pointOfInterest.Name.Trim() == pointOfInterest.Description.Trim()) { ModelState.AddModelError("Description", "Please enter a description that is different from the name"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_tripInfoRepository.TripExists(tripId)) { return(NotFound()); } var finalPointOfInterest = Mapper.Map <Entities.PointsOfInterest>(pointOfInterest); _tripInfoRepository.AddPointOfInterestForTrip(tripId, finalPointOfInterest); if (!_tripInfoRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdPointOfInterestToReturn = Mapper.Map <Models.PointsOfInterestDto>(finalPointOfInterest); return(CreatedAtRoute("GetPointOfInterest", new { tripId = tripId, id = createdPointOfInterestToReturn.Id }, createdPointOfInterestToReturn)); } 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.")); } }
public IActionResult CreateDestination([FromBody] DestinationForCreationDto destination) { if (destination == null) { return(BadRequest()); } var destinationEntity = Mapper.Map <Destination>(destination); _tripInfoRepository.AddDestination(destinationEntity); if (!_tripInfoRepository.Save()) { throw new Exception("Creating an destination failed on save."); // return StatusCode(500, "A problem happened with handling your request."); } var destinationToReturn = Mapper.Map <DestinationDto>(destinationEntity); return(CreatedAtRoute("GetDestination", new { id = destinationToReturn.Id }, destinationToReturn)); }
public IActionResult CreateAttractionForDestination(Guid destinationId, [FromBody] AttractionForCreationDto attraction) { if (attraction == null) { return(BadRequest()); } if (attraction.Description == attraction.Name) { ModelState.AddModelError(nameof(AttractionForCreationDto), "The provided description should be different from the name."); } if (!ModelState.IsValid) { // return 422 return(new UnprocessableEntityObjectResult(ModelState)); } if (!_tripInfoRepository.DestinationExists(destinationId)) { return(NotFound()); } var attractionEntity = Mapper.Map <Attraction>(attraction); _tripInfoRepository.AddAttractionForDestination(destinationId, attractionEntity); if (!_tripInfoRepository.Save()) { throw new Exception($"Creating a attraction for destination {destinationId} failed on save."); } var attractionToReturn = Mapper.Map <AttractionDto>(attractionEntity); return(CreatedAtRoute("GetAttractionForDestination", new { destinationId = destinationId, id = attractionToReturn.Id }, attractionToReturn)); }