private async Task <IActionResult> AddSpecificTour <T>(T tour) where T : class { if (tour == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var tourEntity = Mapper.Map <Entities.Tour>(tour); if (tourEntity.ManagerId == Guid.Empty) { if (!Guid.TryParse(_userInfoService.UserId, out Guid userIdAsGuid)) { return(Forbid()); } tourEntity.ManagerId = userIdAsGuid; } await _tourManagementRepository.AddTour(tourEntity); if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Adding a tour failed on save."); } var tourToReturn = Mapper.Map <Tour>(tourEntity); return(CreatedAtRoute("GetTour", new { tourId = tourToReturn.TourId }, tourToReturn)); }
public async Task <IActionResult> PartiallyUpdateTour(Guid tourId, [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument) { if (jsonPatchDocument == null) { return(BadRequest()); } var tourFromRepo = await _tourManagementRepository.GetTour(tourId); if (tourFromRepo == null) { return(BadRequest()); } var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo); jsonPatchDocument.ApplyTo(tourToPatch); Mapper.Map(tourToPatch, tourFromRepo); await _tourManagementRepository.UpdateTour(tourFromRepo); if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Updating a tour failed on save."); } return(NoContent()); }
public async Task <IActionResult> AddSpecificTour <T>(T tour) where T : class { if (tour == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); // we make sure invalid objects are not passed through } var tourEntity = Mapper.Map <Entities.Tour>(tour); // map parameter to persistance model if (tourEntity.ManagerId == Guid.Empty) // if no managerid, hard code one { tourEntity.ManagerId = new Guid("g07ba678-b6e0-4307-afd9-e804c23b3cd3"); } await _tourManagementRepository.AddTour(tourEntity); // add to repo if (!await _tourManagementRepository.SaveAsync()) // error message if fails on save { throw new Exception("Failed on save!"); } var tourToReturn = Mapper.Map <Tour>(tourEntity); // need to remap to return to the client // 201 status plus creating the access route for the new tour return(CreatedAtRoute("GetTour", new { tourId = tourToReturn.TourId }, tourToReturn)); }
private async Task <IActionResult> AddSpecificTour <T>(T tour) where T : class { if (tour == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var tourEntity = Mapper.Map <Entities.Tour>(tour); if (tourEntity.ManagerId == Guid.Empty) { tourEntity.ManagerId = new Guid("fec0a4d6-5830-4eb8-8024-272bd5d6d2bb"); } await this._tourManagementRepository.AddTour(tourEntity); if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Adding a tour failed on save."); } var tourToReturn = Mapper.Map <Tour>(tourEntity); return(CreatedAtRoute("GetTour", new { tourId = tourToReturn.TourId }, tourToReturn)); }
public async Task <IActionResult> CreateShowCollection( Guid tourId, [FromBody] IEnumerable <ShowForCreation> showCollection) { if (showCollection == null) { return(BadRequest()); } if (!await _tourManagementRepository.TourExists(tourId)) { return(NotFound()); } var showEntities = Mapper.Map <IEnumerable <Entities.Show> >(showCollection); foreach (var show in showEntities) { await _tourManagementRepository.AddShow(tourId, show); } if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Adding a collection of shows failed on save."); } var showCollectionToReturn = Mapper.Map <IEnumerable <Show> >(showEntities); var showIdsAsString = string.Join(",", showCollectionToReturn.Select(a => a.ShowId)); return(CreatedAtRoute("GetShowCollection", new { tourId, showIds = showIdsAsString }, showCollectionToReturn)); }
private async Task <IActionResult> AddSpecificTour <T>(T tour) { if (tour == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(UnprocessableEntity(new CustomizedValidationResult(ModelState))); } Entities.Tour tourEntity = null; try { tourEntity = Mapper.Map <Entities.Tour>(tour); } catch (Exception ex) { var e = ex.ToString(); } if (tourEntity.ManagerId == null || tourEntity.ManagerId == Guid.Empty) { if (!Guid.TryParse(_userInfoService.UserId, out Guid userId)) { return(BadRequest()); } tourEntity.ManagerId = userId; } await _tourManagementRepository.AddTour(tourEntity); if (await _tourManagementRepository.SaveAsync()) { return(CreatedAtRoute("GetTour", new { tourId = tourEntity.TourId }, Mapper.Map <Dtos.Tour>(tourEntity))); } else { throw new Exception("The creation of the tour failed"); } }
public async Task <IActionResult> PartiallyUpdateTour(int tourId, [FromBody] JsonPatchDocument <TourForUpdate> jsonPatchDocument) { if (jsonPatchDocument == null) { return(BadRequest()); } var tourFromRepo = await _tourManagementRepository.GetTour(tourId); if (tourFromRepo == null) { return(BadRequest()); } var tourToPatch = Mapper.Map <TourForUpdate>(tourFromRepo); jsonPatchDocument.ApplyTo(tourToPatch, ModelState); if (!ModelState.IsValid) { return(UnprocessableEntity()); } if (!TryValidateModel(tourToPatch)) { return(UnprocessableEntity()); } Mapper.Map(tourToPatch, tourFromRepo); await _tourManagementRepository.UpdateTour(tourFromRepo); if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Updating a tour failed on save."); } return(NoContent()); }
public async Task <IActionResult> AddSpecificTour <T>(T tour) where T : class { var tourEntity = Mapper.Map <Entities.Tour>(tour); if (tourEntity.ManagerId == Guid.Empty) { tourEntity.ManagerId = new Guid("fec0a4d6-5830-4eb8-8024-272bd5d6d2bb"); } await _tourManagementRepository.AddTour(tourEntity); if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Adding a tour failed on save."); } var tourToReturn = Mapper.Map <Tour>(tourEntity); return(CreatedAtRoute("GetTour", new { tourId = tourToReturn.TourId }, tourToReturn)); }
public async Task <IActionResult> CreateShowCollection(Guid tourId, [FromBody] IEnumerable <ShowForCreation> showCollection) { if (showCollection == null) { return(BadRequest()); } if (!await _tourManagementRepository.TourExists(tourId)) { return(NotFound()); } var showEntities = Mapper.Map <IEnumerable <Entities.Show> >(showCollection); foreach (var show in showEntities) { await _tourManagementRepository.AddShow(tourId, show); } if (!await _tourManagementRepository.SaveAsync()) { throw new Exception("Error"); } return(Ok()); }