public async Task <ActionResult <TalkModel> > Post(string moniker, TalkModel model) { try { var camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("Camp does not exist")); } var talk = _mapper.Map <Talk>(model); talk.Camp = camp; if (model.Speaker == null) { return(BadRequest("Speaker ID is required")); } var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Speaker could not be found")); } talk.Speaker = speaker; _repository.Add(talk); if (await _repository.SaveChangesAsync()) { var url = _linkGenerator.GetPathByAction(HttpContext, "Get", values: new { moniker, id = talk.TalkId }); return(Created(url, _mapper.Map <TalkModel>(talk))); } else { return(BadRequest("Failed to save new Talk")); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to get Talks")); } }
public async Task <IHttpActionResult> Post(string moniker, TalkModel model) { try { if (ModelState.IsValid) { var camp = await _repository.GetCampAsync(moniker); if (camp != null) { var talk = _mapper.Map <Talk>(model); talk.Camp = camp; // Map the Speaker if necessary 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))); } } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public async Task <ActionResult <TalkModel> > Post(string moniker, TalkModel model) { try { var camp = await repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("moniker does not exist")); } var talk = mapper.Map <Talk>(model); talk.Camp = camp; if (model.Speaker == null) { return(BadRequest("speaker.speakerId not specified")); } var speaker = await repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("speaker.speakerID not found")); } talk.Speaker = speaker; repository.Add(talk); if (await repository.SaveChangesAsync()) { var url = linkGenerator.GetPathByAction(HttpContext, "Get", values: new { moniker, id = talk.TalkId }); return(Created(url, mapper.Map <TalkModel>(talk))); } else { return(BadRequest("Error creating talk")); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to create Talk")); } }
public async Task <ActionResult <TalkModel> > AddTalk(string moniker, TalkModel talkModel) { try { var entMoniker = await campRepository.GetCampAsync(moniker); if (entMoniker == null) { return(BadRequest("The moniker provided does not exist")); } var entSpeaker = await campRepository.GetSpeakerAsync(talkModel.speaker.SpeakerId); if (entSpeaker == null) { return(BadRequest("The speaker provided does not exist")); } var talk = mapper.Map <Talk>(talkModel); talk.Camp = entMoniker; talk.Speaker = entSpeaker; campRepository.Add(talk); if (await campRepository.SaveChangesAsync()) { var Url = linkGenerator.GetPathByAction(HttpContext, "GetTalkByID", values: new { moniker, id = talk.TalkId }); return(Created(Url, mapper.Map <TalkModel>(talk))); } return(BadRequest()); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "AddTalk - " + e.ToString())); } }
public async Task <IActionResult> Post(string moniker, TalkModel newTalk) { try { var talk = _mapper.Map <Talk>(newTalk); var camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("Couldn't find the camp.")); } talk.Camp = camp; var speaker = await _repository.GetSpeakerAsync(newTalk.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Couldn't find the speaker.")); } talk.Speaker = speaker; _repository.Add(talk); if (!await _repository.SaveChangesAsync()) { return(BadRequest("Couldn't save the new talk.")); } var url = _link.GetPathByAction(HttpContext, "GetTalks", values: new { moniker, id = talk.TalkId }); if (string.IsNullOrEmpty(url)) { return(BadRequest()); } return(Created(url, _mapper.Map <TalkModel>(talk))); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public async Task <ActionResult <TalkModel> > Post(string moniker, TalkModel model) { try { var camp = await _campRepository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("Camp doesn't exists")); } var talk = _mapper.Map <Talk>(model); if (talk.Speaker == null) { return(BadRequest("Speaker not defined.")); } var speaker = await _campRepository.GetSpeakerAsync(talk.Speaker.SpeakerId); if (speaker == null) { BadRequest("Speaker not found"); } talk.Camp = camp; talk.Speaker = speaker; _campRepository.Add(talk); if (await _campRepository.SaveChangesAsync()) { var url = _linkGenerator.GetPathByAction(HttpContext, "get", "talks", new { moniker = moniker, id = talk.TalkId }); return(Created(url, _mapper.Map <TalkModel>(talk))); } else { return(BadRequest("Speaker not defined.")); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } }
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)); }
public async Task <ActionResult <TalkModel> > Post(string moniker, [FromBody] TalkModel model) //[FromBody] es para indicar que viene data y tiene que bindearla a esa entrada { try { var existin_campg = await _repository.GetCampAsync(moniker); // quiero que el Moniker sea unico if (existin_campg == null) { return(BadRequest("Camp does not exist")); } var talk = _mapper.Map <Talk>(model); // Hago al reves, creo un campo desde un modelo EXPLOTA EL MAPEO talk.Camp = existin_campg; if (model.Speaker == null) { return(BadRequest("Speaker ID is rquired")); } var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Speaker could not be found")); } talk.Speaker = speaker; _repository.Add(talk); // Y lo meto al repositorio if (await _repository.SaveChangesAsync()) { return(Created($"/api/camps/{talk.Camp.Moniker}/{talk.TalkId}", _mapper.Map <TalkModel>(talk))); // Created es un status code 20X, pero no OK, es el que se manda cuando se agrega algo a la API // Deberia usar la clase LinkGenerator en vez de hardcodear la direccion, pero no la podia importar } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }
public async Task <IHttpActionResult> Add(string moniker, TalkModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var camp = await _db.GetCampAsync(moniker); if (camp == null) { return(NotFound()); } var talk = _mapper.Map <Talk>(model); camp.Talks.Add(talk); await _db.SaveChangesAsync(); return(CreatedAtRoute("GetTalkById", new { moniker, Id = talk.TalkId }, _mapper.Map(talk, model))); }
public async Task <IActionResult> Post(string moniker, TalkModel newTalk) { try { var camp = await repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest()); } var talk = mapper.Map <Talk>(newTalk); talk.Camp = camp; if (newTalk.Speaker == null) { return(BadRequest("Speaker ID is required")); } var speaker = await repository.GetSpeakerAsync(newTalk.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Speaker could not be found")); } talk.Speaker = speaker; repository.Add(talk); if (await repository.SaveChangesAsync()) { return(StatusCode(StatusCodes.Status201Created, $"New talk of moniker {moniker} is created")); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } return(BadRequest()); }
public async Task <IHttpActionResult> GetCamp(string moniker, bool includeTalks = false) { Camp result; try { result = await _repository.GetCampAsync(moniker, includeTalks); if (result == null) { return(NotFound()); } } catch (Exception ex) { return(InternalServerError(ex)); } // Mapping (Model/Entity --> Dto) CampDto mappedResult = _mapper.Map <CampDto>(result); return(Ok(new { success = true, camp = mappedResult })); }
public async Task <IHttpActionResult> Post(string moniker, TalkModel talkModel) { try { var camp = await _repository.GetCampAsync(moniker); if (camp != null) { var talk = _mapper.Map <Talk>(talkModel); talk.Camp = camp; _repository.AddTalk(talk); if (await _repository.SaveChangesAsync()) { return(CreatedAtRoute("GetTalk", new { moniker = moniker, id = talk.TalkId }, _mapper.Map <TalkModel>(talk))); } } } catch (Exception e) { return(InternalServerError(e)); } return(BadRequest()); }