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> 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 _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> 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) { 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> AddSpecificTour <T>(T tour) where T : class { var tourEntity = Mapper.Map <Entities.Tour>(tour); if (tourEntity.ManagerId == Guid.Empty)//a8dd37c5-1c37-45d7-830b-59a1be2a3c52 { 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)); }