public async Task <IHttpActionResult> Post(string moniker, TalkReqDto talkReqDto) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var camp = await _campRepository.GetCampAsync(moniker); if (camp == null) { return(NotFound()); } var talk = _mapper.Map <Talk>(talkReqDto); talk.Camp = camp; _campRepository.AddTalk(talk); if (!await _campRepository.SaveChangesAsync()) { return(InternalServerError()); } return(CreatedAtRoute( "GetTalk", new { moniker, id = talk.TalkId }, _mapper.Map <TalkReqDto>(talk))); } catch (Exception e) { return(InternalServerError(e)); } }
public async Task <IHttpActionResult> Post(string moniker, TalkModel talkModel) { try { if (ModelState.IsValid) { var camp = await campRepository.GetCampAsync(moniker); if (camp != null) { var talk = mapper.Map <Talk>(talkModel); talk.Camp = camp; campRepository.AddTalk(talk); // Add speaker if provided if (talkModel.Speaker != null) { var speaker = await campRepository.GetSpeakerAsync(talkModel.Speaker.SpeakerId); talk.Speaker = speaker; } if (await campRepository.SaveChangesAsync()) { return(CreatedAtRoute("GetTalk", new { moniker = moniker, id = talk.TalkId }, mapper.Map <TalkModel>(talk))); } } } } catch (Exception ex) { InternalServerError(ex); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Post(string monikar, TalkModel talkModel) { if (ModelState.IsValid) { try { var camp = await _repositary.GetCampAsync(monikar); if (camp != null) { var talk = _mapper.Map <Talk>(talkModel); talk.Camp = camp; _repositary.AddTalk(talk); if (await _repositary.SaveChangesAsync()) { return(CreatedAtRoute("GetTalk", new { monikar = monikar, id = talk.TalkId }, _mapper.Map <TalkModel>(talk))); } } } catch (Exception ex) { return(InternalServerError(ex)); } } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Post(string moniker, TalkModel model) { try { //if (ModelState.IsValid) { var camp = await cr.GetCampAsync(moniker); if (null != camp) { var talk = mp.Map <Talk>(model); talk.Camp = camp; if (null != model.Speaker) { var speaker = await cr.GetSpeakerAsync(model.Speaker.SpeakerId); if (null != speaker) { talk.Speaker = speaker; } } cr.AddTalk(talk); await cr.SaveChangesAsync(); var talkModel = mp.Map <TalkModel>(talk); return(CreatedAtRoute("GetTalk", new { moniker = moniker, id = talk.TalkId }, talkModel)); } } } catch (Exception e) { return(InternalServerError()); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Post(string moniker, TalkModel talkModel) { try { var camp = await _repository.GetCampAsync(moniker); var talk = _mapper.Map <Talk>(talkModel); talk.Camp = camp; _repository.AddTalk(talk); bool successfulChanges = await _repository.SaveChangesAsync(); if (successfulChanges) { return(CreatedAtRoute("GetTalk", new { moniker = moniker, talkId = talk.TalkId }, _mapper.Map <TalkModel>(talk))); } else { return(InternalServerError()); } } catch { return(InternalServerError()); } }
public async Task <IHttpActionResult> Post(string moniker, TalkDto talkDto) { try { if (await _repository.GetTalkByMonikerAsync(moniker, talkDto.TalkId) != null) { ModelState.AddModelError("TalkId", "TalkId is already in use inside this camp..!"); // optional because EF generates a new id every time. The id inside the body won't be used... } if (ModelState.IsValid) { Talk talk = _mapper.Map <Talk>(talkDto); // Map the camp to the talk (camp is here a foreign key for the Talk class/model and it's not a part of the TalkDto)... Camp camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("No camp found for this moniker..!")); } talk.Camp = camp; // If a SpeakerId is given, map the speaker to the talk... if (talkDto.Speaker != null) { Speaker speaker = await _repository.GetSpeakerAsync(talkDto.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("No speaker found with this Id..!")); } talk.Speaker = speaker; // append the speaker data for the given id to the talk to be created... } _repository.AddTalk(talk); if (await _repository.SaveChangesAsync()) { TalkDto newTalk = _mapper.Map <TalkDto>(talk); return(Created(new Uri(Request.RequestUri + "/" + newTalk.TalkId), newTalk)); //return CreatedAtRoute("GetTalk", new { moniker = moniker, talkId = talk.TalkId }, newTalk); // TODO: Exception "UrlHelper.Link must not return null". But why??? } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Post(string moniker, TalkModel model) { try { if (ModelState.IsValid) { var camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest()); } var talk = _mapper.Map <Talk>(model); talk.Camp = camp; if (model.Speaker != null) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } _repository.AddTalk(talk); if (await _repository.SaveChangesAsync()) { return(CreatedAtRoute("GetTalk", new { moniker = moniker, id = talk.TalkId }, _mapper.Map <TalkModel>(talk))); } } else { return(BadRequest()); } } catch (Exception e) { return(InternalServerError()); } return(BadRequest()); }
public async Task <IHttpActionResult> Post(string moniker, TalkModel talkModel) { try { if (!ModelState.IsValid) { return(BadRequest()); } var camp = await repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest()); // or not found? } var talk = mapper.Map <Talk>(talkModel); talk.Camp = camp; // In model Talk has 1 speaker but speaker gives many talks. // Hence an existing speaker(id) can be attached to model if (talkModel.Speaker != null) { var speaker = await repository.GetSpeakerAsync(talkModel.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } repository.AddTalk(talk); if (!await repository.SaveChangesAsync()) { return(InternalServerError()); } var newTalkModel = mapper.Map <TalkModel>(talk); return(CreatedAtRoute("GetTalk", new { moniker = moniker, id = newTalkModel.TalkId }, newTalkModel)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <IHttpActionResult> Post(string moniker, TalkModel model) { try { //validation if (ModelState.IsValid) { var camp = await _repository.GetCampAsync(moniker).ConfigureAwait(false); if (camp != null) { var talk = _mapper.Map <Talk>(model); talk.Camp = camp; //mapping the speaker if we need that if (model.Speaker != null) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } _repository.AddTalk(talk); if (await _repository.SaveChangesAsync().ConfigureAwait(false)) { var newModel = _mapper.Map <TalkModel>(talk); return(CreatedAtRoute("GetTalk", new { moniker = moniker, talk = talk.TalkId }, newModel)); } } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Create(String moniker, TalkModel model) { try { var camp = await _repository.GetCampAsync(moniker); if (camp != null) { var talk = _mapper.Map <Talk>(model); talk.Camp = camp; //map speaker if (model.Speaker != null) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } _repository.AddTalk(talk); if (await _repository.SaveChangesAsync()) { return(CreatedAtRoute("GetByIdTalk", new { moniker = moniker, idTalk = talk.TalkId }, _mapper.Map <TalkModel>(talk))); } } } catch (Exception ex) { return(InternalServerError(ex)); } //manda los mensajes de error de validacion return(BadRequest(ModelState)); }