public IActionResult Get(string moniker, bool includeSpeakers = false) { //query string of ?includeSpeakers=true. Additional params are query string try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} was not found")); } return(Ok(_mapper.Map <CampModel>(camp))); } catch (Exception ex) { return(BadRequest(ex)); } }
public ActionResult <CampModel> Get(string moniker, bool includeSpeakers = false) { try { Camp result; if (includeSpeakers) { result = _repository.GetCampByMonikerWithSpeakers(moniker); } else { result = _repository.GetCampByMoniker(moniker); } if (result == null) { return(NotFound()); } return(_mapper.Map <CampModel>(result)); } catch (Exception) { } return(BadRequest()); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp with moniker = '{moniker}' was not found.")); } return(Ok(_mapper.Map <CampModel>(camp))); } catch (Exception ex) { _logger.LogError($"Ex in Get by moniker: {moniker}: {ex}"); } return(BadRequest()); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} was not found")); } return(Ok(_mapper.Map <CampModel>(camp, opt => opt.Items["UrlHelper"] = this.Url))); } catch { } return(BadRequest()); }
public IActionResult GetMoniker(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} was not found")); } return(Ok(_mapper.Map <CampModel>(camp))); //LD STEP3 } catch { } return(BadRequest()); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = campRepository.GetCampByMonikerWithSpeakers(moniker); } else { camp = campRepository.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} was not found.")); } return(Ok(mapper.Map <CampModel>(camp))); } catch (Exception ex) { logger.LogError($"Exception was thrown while saving Camp: {ex}"); return(BadRequest()); } }
// MVC assumes any pass in parameter listed in the url are query string parameters public IActionResult Get(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} not found.")); } // opt.Items passes a collection into the resolver. // we are passing down UrlHelper to the resolver return(Ok(_mapper.Map <CampModel>(camp))); } catch (Exception ex) { _logger.LogError($"get camp exception: {ex}"); } return(BadRequest()); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { var camp = campRepository.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest($"Could not find camp {moniker}")); } var speaker = mapper.Map <Speaker>(model); speaker.Camp = camp; campRepository.Add(speaker); if (await campRepository.SaveAllAsync()) { var url = Url.Link("GetSpeaker", new { moniker = camp.Moniker, id = speaker.Id }); return(Created(url, mapper.Map <SpeakerModel>(speaker))); } else { logger.LogWarning("Could not save speaker to the database."); } } catch (Exception ex) { logger.LogError($"Exception was thrown while adding a speaker for camp {moniker}: {ex}"); } return(BadRequest("Could not add new speaker.")); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { var camp = _repository.GetCampByMoniker(moniker); // get the camp if (camp == null) { return(BadRequest("Could not find camp")); } var speaker = _mapper.Map <Speaker>(model); // get the speaker. convert the passed in model to our Speaker Entities using automapper speaker.Camp = camp; // assign camp to the speaker - relationship // Authentication var campUser = await _userMgr.FindByNameAsync(this.User.Identity.Name); // find the camp user and return it. Ensured that the user logged in is the same user we have if (campUser != null) { speaker.User = campUser; _repository.Add(speaker); // add speaker to repository if (await _repository.SaveAllAsync()) // save to database { var url = Url.Link("SpeakGet", new { moniker = camp.Moniker, id = speaker.Id }); // generate a url for the new object return(Created(url, _mapper.Map <SpeakerModel>(speaker))); // new speaker is created } } } catch (Exception ex) { _logger.LogError($"++++++++++++++++++++ Exception thrown while adding speaker +++++++++++++++++++++++++ {ex}"); } return(BadRequest("Could not add new speaker")); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"Camp {moniker} was not found.")); } // We're mapping our camp but also map an URL helper that we'll use in our // AutoMapper profile resolver. return(Ok(_mapper.Map <CampModel>(camp))); } catch { } return(BadRequest()); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { var camp = Repository.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest("Could not find camp")); } var speaker = Mapper.Map <Speaker>(model); speaker.Camp = camp; var campUser = await UserManager.FindByNameAsync(User.Identity.Name); if (campUser != null) { speaker.User = campUser; Repository.Add(speaker); if (await Repository.SaveAllAsync()) { var url = Url.Link("SpeakerGet", new { moniker = camp.Moniker, id = speaker.Id }); return(Created(url, Mapper.Map <SpeakerModel>(speaker))); } } } catch (Exception exception) { Logger.LogError("Threw exception while saving Speaker", exception); } return(BadRequest("Could not add new speaker")); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { var camp = _repository.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest("Could not find camp")); } var speaker = _mapper.Map <Speaker>(model); speaker.Camp = camp; var campUser = await _userMgr.FindByNameAsync(this.User.Identity.Name); if (campUser != null) { speaker.User = campUser; _repository.Add(speaker); if (await _repository.SaveAllAsync()) { var url = Url.Link("SpeakerGet", new { moniker = camp.Moniker, id = speaker.Id }); return(Created(url, _mapper.Map <SpeakerModel>(speaker))); } } } catch (Exception ex) { _logger.LogError($"Exception thrown while adding speaker: {ex}"); } return(BadRequest("Could not add new speaker")); }
public async Task<IActionResult> Post(string moniker,[FromBody]SpeakerModel model) { try { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} var camp = _repo.GetCampByMoniker(moniker); if(camp == null) { return BadRequest("could not find camp"); } var speaker = _mapper.Map<Speaker>(model); _repo.Add(speaker); if(await _repo.SaveAllAsync()) { var newUri = Url.Link("SpeakerGet",new { moniker = camp.Moniker, id= speaker.Id }); return Created(newUri,_mapper.Map<SpeakerModel>(speaker)); } } catch (Exception ex) { _logger.LogError($"Exception thrown while adding speaker : {ex}"); } return BadRequest("Could not add a new Speaker"); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { var camp = includeSpeakers ? _campRepository.GetCampByMonikerWithSpeakers(moniker) : _campRepository.GetCampByMoniker(moniker); if (camp == null) { return(NotFound($"Camp with moniker '{moniker}' was not found.")); } // return Ok(_mapper.Map<CampViewModel>(camp, opt => opt.Items["UrlHelper"] = Url)); /* => this method was cumbersome to use, now implemented by custom resolver */ return(Ok(_mapper.Map <CampViewModel>(camp))); } catch (Exception ex) { _logger.LogCritical($"Threw exception while getting camp with moniker='{moniker}' and includeSpeakers='{includeSpeakers}': {ex}"); } return(BadRequest("Could not get camp")); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { Camp camp = null; if (includeSpeakers) { camp = _repo.GetCampByMonikerWithSpeakers(moniker); } else { camp = _repo.GetCampByMoniker(moniker); } if (camp == null) { return(NotFound($"camp : {moniker} was not found")); } return(Ok(_mapper.Map <CampModel>(camp))); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { var camp = includeSpeakers ? repository.GetCampByMonikerWithSpeakers(moniker) : repository.GetCampByMoniker(moniker); if (camp == null) { return(NotFound($"Camp {moniker} was not found")); } return(Ok(mapper.Map <CampModel>(camp))); } catch (Exception exception) { logger.LogError("Threw exception while getting a Camp", exception); } return(BadRequest()); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerViewModel viewModel) { try { var camp = _campRepository.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest("Could not get camp")); } var speaker = _mapper.Map <Speaker>(viewModel); speaker.Camp = camp; _campRepository.Add(speaker); if (await _campRepository.SaveAllAsync()) { var url = Url.Link("GetSpeaker", new { moniker = camp.Moniker, id = speaker.Id }); return(Created(url, _mapper.Map <SpeakerViewModel>(speaker))); } } catch (Exception ex) { _logger.LogCritical($"Exception thrown while saving a speaker: {ex}."); } return(BadRequest("Could not add new speaker.")); }
public IActionResult Get(string moniker, bool includeSpeakers = false) { try { _logger.LogInformation("Getting a Code Camp"); var camp = includeSpeakers ? _campRepository.GetCampByMonikerWithSpeakers(moniker) : _campRepository.GetCampByMoniker(moniker); if (camp == null) { return(NotFound($"Camp {moniker} was not found")); } return(Ok(_mapper.Map <CampModel>(camp))); } catch (Exception exception) { _logger.LogError($"Threw exception while getting Camp: {exception}"); } return(BadRequest("Couldn't get Camp")); }
public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { var camp = _repo.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest("Could not find camp")); } var speaker = _mapper.Map <Speaker>(model); speaker.Camp = camp; var campUser = await _userManager.FindByNameAsync(this.User.Identity.Name); if (campUser != null) { speaker.User = campUser; _logger.LogInformation("Creating a new Code Camp"); _repo.Add(speaker); if (await _repo.SaveAllAsync()) { var newUri = Url.Link("SpeakerGet", new { moniker = speaker.Camp.Moniker, id = speaker.Id }); return(Created(newUri, _mapper.Map <SpeakerModel>(speaker))); } else { _logger.LogWarning("could not save speaker to tadabase"); } } } catch (Exception ex) { _logger.LogError($"Exeption throen while adding speaker: {ex}"); return(BadRequest()); } return(BadRequest("Unable to add new speaker!")); }
[Authorize] //LD STEP30 public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model) { try { //LD we get the specific camp record by searching by moniker var camp = _repository.GetCampByMoniker(moniker); if (camp == null) { return(BadRequest("Could not find camp")); } //LD after we map the "SpeakerModel" to "Speaker" entity var speaker = _mapper.Map <Speaker>(model); //LD then we assign the "Camp" entity to the "Speaker" entity speaker.Camp = camp; //LD STEP31 var campUser = await _userMgr.FindByNameAsync(this.User.Identity.Name); if (campUser != null) { speaker.User = campUser; _repository.Add(speaker); if (await _repository.SaveAllAsync()) { var url = Url.Link("SpeakerGet", new { moniker = camp.Moniker, id = speaker.Id }); return(Created(url, _mapper.Map <SpeakerModel>(speaker))); } } } catch (Exception ex) { _logger.LogError($"Exception thrown while adding speaker: {ex}"); } return(BadRequest("Could not add new speaker")); }