public async Task <IActionResult> PostAsync([FromBody] Camp model) { try { _logger.LogInformation("Creating a new Code Camp"); //LD STEP101 _repo.Add(model); if (await _repo.SaveAllAsync()) { var newUri = Url.Link("CampGetSpecific", new { id = model.Id }); return(Created(newUri, model)); } else { _logger.LogWarning("Could not save Camp to the database"); } } catch (Exception ex) { _logger.LogError($"Threw exception while saving Camp: {ex}"); } return(BadRequest()); }
public async Task <IHttpActionResult> Post(CampModel campModel) { if (await _iCampRepository.GetCampAsync(campModel.Moniker) != null) { ModelState.AddModelError("Moniker", "Moniker in use"); } if (ModelState.IsValid) { try { var camp = Mapper.Map <Camp>(campModel); _iCampRepository.Add(camp); if (await _iCampRepository.SaveChangesAsync()) { var campM = Mapper.Map <CampModel>(camp); var location = Url.Link("GetCamp", new { moniker = campM.Moniker }); return(Created(location, campM)); } } catch (Exception ex) { return(InternalServerError(ex)); } } return(BadRequest(ModelState)); }
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")); }
// use [FromBody] if incoming data is json // make request async using async Task<> ... await public async Task <IActionResult> Post([FromBody] CampModel model) { try { _logger.LogInformation("Creating new camp."); // we are getting the model from request.body. need to convert model into entity. var camp = _mapper.Map <Camp>(model); // save camp entity to server; // the returned entity will have server generated data such as id _repo.Add(camp); if (await _repo.SaveAllAsync()) { // pass in id to Url.Link via anonymous object var newUri = Url.Link("CampGet", new { moniker = camp.Moniker }); // use Created status for Post. // Created needs new uri and record created. // use mapper to turn entity into a model that has server generated data return(Created(newUri, _mapper.Map <CampModel>(camp))); } else { _logger.LogWarning("Could not save camp."); } } catch (Exception ex) { _logger.LogError($"Save camp exception: {ex}"); } return(BadRequest()); }
public async Task <IActionResult> Post([FromBody] CampModel model) { try { _logger.LogInformation("Creating a new Code Camp"); var newCamp = _mapper.Map <Camp>(model); _repo.Add(newCamp); if (await _repo.SaveAllAsync()) { var newUri = Url.Link("CampGet", new { moniker = newCamp.Moniker }); return(Created(newUri, _mapper.Map <CampModel>(newCamp))); } else { _logger.LogWarning("Could not save Camp to the database"); } } catch (Exception ex) { _logger.LogError($"Threw exception while saving Camp: {ex}"); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var existing = await _campRepository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker is Use")); } var location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(model); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(Created(location, _mapper.Map <CampModel>(camp))); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Something went wrong on the server")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > CreateCamp(CampModel model) { try { //if(ModelState.IsValid){} OR [ApiController] at Controller Definition var campExist = await _campRepository.GetCampAsync(model.Moniker); if (campExist != null) { return(BadRequest($"{model.Moniker} is already exists")); } var camp = _mapper.Map <Camp>(model); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(CreatedAtRoute(nameof(GetCampsByName), new { campName = model.Moniker }, _mapper.Map <CampModel>(camp))); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Server Error!")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post([FromBody] CampModel campModel) { try { var esistingCamp = await _repository.GetCampAsync(campModel.Moniker); if (esistingCamp != null) { return(BadRequest("Moniker in use")); } var campLocation = _linkGenerator.GetPathByAction(HttpContext, nameof(Get), //"Camps", //nameof(CampsController).Replace("Controller", ""), values: new { moniker = campModel.Moniker }); if (string.IsNullOrWhiteSpace(campLocation)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(campModel); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(Created(campLocation, campModel)); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } return(BadRequest()); }
public async Task <IActionResult> Post([FromBody] CampModel model) { try { _logger.LogInformation($"Creating na new Code Camp '{model.Name}'"); Camp camp = _mapper.Map <Camp>(model); _repo.Add(camp); if (await _repo.SaveAllAsync()) { string newUri = Url.Link("CampGet", new { moniker = camp.Moniker }); return(Created(newUri, _mapper.Map <CampModel>(camp))); } else { _logger.LogWarning("Could not save Camp to db"); } } catch (Exception ex) { _logger.LogError($"Exception while saving Camp: {ex}"); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post([FromBody] CampModel campmodel) { try { if (await campRepository.GetCampAsync(campmodel.Moniker) != null) { return(BadRequest("moniker is already exist")); } var Location = linkGenerator.GetPathByAction ("GetCamp", "Camps", new { moniker = campmodel.Moniker }); if (String.IsNullOrEmpty(Location)) { return(BadRequest("couldn't use current moniker")); } var camp = mapper.Map <Camp>(campmodel); campRepository.Add(camp); if (await campRepository.SaveChangesAsync()) { return(Created(Location, mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "database failure")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel campModel) { try { var oldCamp = await _campRepository.GetCampAsync(campModel.Moniker); if (oldCamp != null) { return(BadRequest("Already Exists")); } var location = _linkGenerator.GetPathByAction("Get", "Camps", new { campModel.Moniker }); if (string.IsNullOrEmpty(location)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(campModel); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(Created($"/api/camps/{campModel.Moniker}", _mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Cannot Create")); } return(StatusCode(StatusCodes.Status400BadRequest)); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var camp = await repository.GetCampAsync(model.Moniker); if (camp != null) { return(BadRequest("Moniken in Use")); } var location = linkGenerator.GetPathByAction(action: "GetCamp", controller: "Camps", values: new { moniker = model.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } camp = mapper.Map <Camp>(model); repository.Add(camp); if (await repository.SaveChangesAsync()) { return(Created(location, mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database error")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var link = linkGenerator.GetPathByAction(nameof(Get), "Camps", new { moniker = model.Moniker }); if (string.IsNullOrEmpty(link)) { return(BadRequest()); } var existing = await campRepository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker already exists")); } var camp = mapper.Map <Camp>(model); campRepository.Add(camp); if (await campRepository.SaveChangesAsync()) { return(Created(link, mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } return(BadRequest()); }
public async Task <IActionResult> Post([FromBody] CampModel model) { try { logger.LogInformation("Creating a new Code Camp"); var camp = mapper.Map <Camp>(model); repository.Add(camp); if (await repository.SaveAllAsync()) { var newUri = Url.Link("CampGet", new { moniker = camp.Moniker }); return(Created(newUri, mapper.Map <CampModel>(camp))); } logger.LogWarning("Could not save Camp to the database"); } catch (Exception exception) { logger.LogError("Threw exception while saving Camp", exception); } return(BadRequest()); }
public async Task <ActionResult <CampDto> > Post(CampDto model) { try { var existing = await _campRepository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker already in use")); } var location = _linkGenerator.GetPathByAction("GetCamp", "Camps", new { moniker = model.Moniker }); _logger.LogInformation($"{location}"); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } _logger.LogInformation($"model.Moniker:{model.Moniker}, model.Name:{model.Name}"); var camp = _mapper.Map <Camp>(model); _logger.LogInformation("Mapping done"); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(Created(location, _mapper.Map <CampDto>(camp))); } return(BadRequest()); } catch (System.Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Failed to create Camp" + ex.StackTrace)); } }
public async Task <IActionResult> CreateCamp(CampModel newCamp) { try { var found = await _repository.GetCampAsync(newCamp.Moniker); if (found != null) { return(BadRequest("Moniker exists")); } var link = _link.GetPathByAction("GetCamp", "Camps", new { moniker = newCamp.Moniker }); if (string.IsNullOrEmpty(link)) { return(BadRequest("Couldn't create a link using the given moniker.")); } var camp = _mapper.Map <Camp>(newCamp); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(Created(link, _mapper.Map <CampModel>(camp))); } return(BadRequest("Something went wrong.")); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to create new camp.")); } }
public async Task <ActionResult <CampModel[]> > Post(CampModel model) { try { var checkMoniker = await _repository.GetCampAsync(model.Moniker); if (checkMoniker != null) { return(BadRequest("Moniker in use, please use another moniker and retry.")); } var location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker }); if (!string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(model); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(Created("", _mapper.Map <CampModel>(camp))); } // TODO: implement this POST functionality. return(Ok()); } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Data was not stored: something went wrong.")); } }
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 async Task <ActionResult <CampModel> > PostCamp(CampModel campModel) { try { var isDuplicatedCamp = (await campRepository.GetCampAsync(campModel.Moniker) != null); if (isDuplicatedCamp) { return(BadRequest($"Moniker {campModel.Moniker} in use")); } var camp = mapper.Map <CampModel, Camp>(campModel); var uri = linkGenerator.GetPathByAction("GetCampByMonikerAsync", "Camps", new { moniker = campModel.Moniker }); if (string.IsNullOrWhiteSpace(uri)) { return(BadRequest("Could not create a location using the provided moniker.")); } campRepository.Add(camp); if (await campRepository.SaveChangesAsync()) { return(Created(uri, mapper.Map <Camp, CampModel>(camp))); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "PostCamp - " + e.ToString())); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > PostCamp(CampModel model) { try { var exists = await _repository.GetCampAsync(model.Moniker); if (exists != null) { return(BadRequest("Moniker in use")); } var camp = _mapper.Map <Camp>(model); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(CreatedAtAction("GetCamp", new { moniker = camp.Moniker }, _mapper.Map <CampModel>(camp))); } } catch (Exception e) { Console.WriteLine(e); return(this.StatusCode(StatusCodes.Status500InternalServerError, "It's not you, it's us")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var campExists = await _repository.GetCampAsync(model.Moniker); if (campExists != null) { return(BadRequest("Moniker already exists")); } var location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(model); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(Created(location, _mapper.Map <CampModel>(camp))); } } catch (Exception) { return(this.StatusCode(500, "Database failure")); } return(BadRequest()); }
public async Task <ActionResult <SpeakerModel> > Post(string moniker, [FromBody] SpeakerModel model) { try { var user = await _userManager.FindByNameAsync(this.User.Identity.Name); if (user != null) { var camp = _repository.GetCampByMonikerWithSpeakers(moniker); if (camp != null) { var speaker = _mapper.Map <Speaker>(model); speaker.User = user; camp.Speakers.Add(speaker); _repository.Add(speaker); if (await _repository.SaveAllAsync()) { var url = Url.RouteUrl("GetSpeaker", new { Moniker = moniker, id = speaker.Id }); return(Created(url, _mapper.Map <SpeakerModel>(speaker))); } } } } catch (Exception ex) { Console.WriteLine(ex); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var existing = await repository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker in use")); } var location = linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } var camp = mapper.Map <Camp>(model); repository.Add(camp); if (await repository.SaveChangesAsync()) { return(Created($"/api/camps/{camp.Moniker}", mapper.Map <CampModel>(camp))); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }
public async Task <ActionResult <CampModel> > Post(CampModel model) { try { var existing = await _repository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest($"moniker {model.Moniker} already exists")); } var location = _linkGenerator.GetPathByAction("Get", "Camps", new { moniker = model.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("could not use current moniker")); } var camp = _mapper.Map <Camp>(model); _repository.Add(camp); await _repository.SaveChangesAsync(); return(Created(location, _mapper.Map <CampModel>(camp))); } catch (Exception e) { return(BadRequest(e)); } }
public async Task <IActionResult> Post([FromBody] CampModel campModel) { try { logger.LogInformation("Creating a new code camp."); var camp = mapper.Map <Camp>(campModel); campRepository.Add(camp); if (await campRepository.SaveAllAsync()) { var newUri = Url.Link("GetCamp", new { moniker = camp.Moniker }); return(Created(newUri, mapper.Map <CampModel>(camp))); } else { logger.LogWarning("Could not save code camp to the database."); } } catch (Exception ex) { logger.LogError($"Exception was thrown while saving Camp: {ex}"); } return(BadRequest()); }
public async Task <IActionResult> Post(string moniker, int speakerId, [FromBody] TalkModel model) { try { var speaker = _repo.GetSpeaker(speakerId); if (speaker != null) { var talk = _mapper.Map <Talk>(model); talk.Speaker = speaker; _repo.Add(talk); if (await _repo.SaveAllAsync()) { AddETag(talk); return(Created(Url.Link("GetTalk", new { moniker = moniker, speakerId = speakerId, id = talk.Id }), _mapper.Map <TalkModel>(talk))); } } } catch (Exception ex) { _logger.LogError($"Failed to save new talk: {ex}"); } return(BadRequest("Failed to save new talk")); }
public async Task <ActionResult <CampModel> > Post(CampModel campModel) { try { var existingCamp = await _campRepository.GetCampAsync(campModel.Moniker); if (existingCamp != null) { return(BadRequest("Moniker " + campModel.Moniker + " already exists")); } var location = _linkGenerator.GetPathByAction("GetCamp", "Camps", new { moniker = campModel.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } var camp = _mapper.Map <Camp>(campModel); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(Created(location, _mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } return(BadRequest()); }
public async Task <IActionResult> Post(string moniker, int speakerId, [FromBody] TalkModel model) { try { var speaker = _repo.GetSpeaker(speakerId); if (speaker == null) { return(BadRequest("Could not find speaker")); } var talk = _mapper.Map <Talk>(model); talk.Speaker = speaker; _repo.Add(talk); if (await _repo.SaveAllAsync()) { var url = Url.Link("TalkGet", new { moniker = moniker, speakerId = speakerId, id = talk.Id }); return(Created(url, _mapper.Map <TalkModel>(model))); } } catch (Exception ex) { _logger.LogError($"Exception thrown while adding talk: {ex}"); } return(BadRequest("Could not add new talk")); }
public async Task <ActionResult <CampModel> > CreateCamp(CampModel model) { try { var existing = await _repository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker in use")); } var camp = _mapper.Map <Camp>(model); _repository.Add(camp); if (await _repository.SaveChangesAsync()) { return(CreatedAtAction(nameof(GetCampByMoniker), new { moniker = model.Moniker }, _mapper.Map <CampModel>(camp))); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } 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")); }