public async Task <ActionResult <TalksDTO> > CreateTalk(string moniker, TalksDTO model) { try { var camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest($"Camp for the specified moniker {moniker} doesn't exists")); } var talk = _mapper.Map <Talk>(model); talk.Camp = camp; // speaker 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 = _generator.GetPathByAction(HttpContext, "Get", values: new { moniker, id = model.TalkId }); return(Created(url, _mapper.Map <TalksDTO>(talk))); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "DB error")); } return(BadRequest("Failed to create the talk")); }
public async Task <ActionResult <TalksDTO> > UpdateTalkByID(string moniker, int id, TalksDTO model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { BadRequest("Talk not found"); } _mapper.Map(model, talk); if (model.Speaker != null) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } if (await _repository.SaveChangesAsync()) { return(_mapper.Map <TalksDTO>(talk)); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "DB error")); } return(BadRequest("Failed to Update the talk")); }