public async Task <IActionResult> Put(string moniker, int speakerId, int id, [FromBody] TalkModel model) { try { var talk = _repo.GetTalk(id); if (talk == null) { return(NotFound()); } _mapper.Map(model, talk); if (await _repo.SaveAllAsync()) { return(Ok(_mapper.Map <TalkModel>(talk))); } } catch (Exception ex) { _logger.LogError($"Failed to update talk: {ex}"); } return(BadRequest("Failed to update talk")); }
public async Task <IHttpActionResult> Put(string moniker, int talkId, TalkModel model) { try { if (ModelState.IsValid) { var talk = await _repository.GetTalkByMonikerAsync(moniker, talkId, true); if (talk == null) { return(NotFound()); } //It's going to ignore the Speaker _mapper.Map(model, talk); //Change Speaker if needed if (talk.Speaker.SpeakerId != model.Speaker.SpeakerId) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } if (await _repository.SaveChangesAsync()) { return(Ok(_mapper.Map <TalkModel>(talk))); } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public async Task <IHttpActionResult> Put(string moniker, int talkId, TalkModel model) { try { var talk = _mapper.Map <Talk>(model); if (talk == null) { return(NotFound()); } _mapper.Map(model, talk); if (await _repository.SaveChangesAsync()) { return(Ok(_mapper.Map <TalkModel>(talk))); } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public static string GetDisplayTime(this TalkModel talkModel) { if (!talkModel.StartTime.HasValue || !talkModel.EndTime.HasValue || talkModel.StartTime.Value.IsTba()) { return("TBA"); } var start = talkModel.StartTime.Value.ToEventTimeZone(); var startString = start.ToString("t"); var end = talkModel.EndTime.Value.ToEventTimeZone(); var endString = end.ToString("t"); var location = string.Empty; if (FeatureFlags.ShowLocationInSessionCell) { if (talkModel.Room != null) { location = $", {talkModel.Room.Name}"; } } return($"{startString}–{endString}{location}"); }
public async Task <ActionResult <TalkModel> > Put(string moniker, int id, TalkModel model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { return(NotFound("Couldn't find the talk")); } _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 <TalkModel>(talk)); } else { return(BadRequest("failed to update database")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.ToString())); } }
public static AppLinkEntry GetAppLink(this TalkModel talkModel) { var url = $"http://{AboutThisApp.AppLinksBaseDomain}/{AboutThisApp.SessionsSiteSubdirectory.ToLowerInvariant()}/{talkModel.Id}"; var entry = new AppLinkEntry { Title = talkModel.Title ?? string.Empty, Description = talkModel.Abstract ?? string.Empty, AppLinkUri = new Uri(url, UriKind.RelativeOrAbsolute), IsLinkActive = true }; if (Device.RuntimePlatform == Device.iOS) { entry.Thumbnail = ImageSource.FromFile("Icon.png"); } entry.KeyValues.Add("contentType", "Session"); entry.KeyValues.Add("appName", AboutThisApp.AppName); entry.KeyValues.Add("companyName", AboutThisApp.CompanyName); return(entry); }
public async Task <ActionResult <TalkModel> > Put(string moniker, int id, TalkModel model) { try { var talk = await campRepository.GetTalkByMonikerAsync(moniker, id); if (talk is null) { return(NotFound("Couldn't find the talk")); } if (model.Speaker != null) { var speaker = await campRepository.GetSpeakerAsync(id); if (speaker != null) { talk.Speaker = speaker; } } Mapper.Map(model, talk); if (await campRepository.SaveChangesAsync()) { return(Mapper.Map <TalkModel>(talk)); } else { return(BadRequest(" Failed to update database. ")); } } catch (Exception) { return(StatusCode(500, "DataBase failed")); } }
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()); }
public async Task <ActionResult <TalkModel> > UpdateTalk(string moniker, int id, TalkModel talk) { try { var entTalk = await campRepository.GetTalkByMonikerAsync(moniker, id); if (entTalk == null) { return(NotFound()); } mapper.Map(talk, entTalk); if (talk.speaker != null) { var entSpeaker = await campRepository.GetSpeakerAsync(talk.speaker.SpeakerId); if (entSpeaker != null) { entTalk.Speaker = entSpeaker; } } if (await campRepository.SaveChangesAsync()) { return(mapper.Map <TalkModel>(entTalk)); } return(BadRequest("Failed to update talk in the db")); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, "AddTalk - " + e.ToString())); } }
//http://localhost:6600/api/camps/ATL2018/talks/3 //De esta forma se envian los datos //{ // "talkId" : 3, // "title": "CharlaPrueba100", // "abstract": "probando actualizr una nueva charla 100 con 40 caracteres como minimo", // "Level" : "200", // "speaker" : // { // "speakerId" : 2 // } // } public async Task <ActionResult <TalkModel> > Put(string moniker, int idTalk, TalkModel modeloTalk) { try { //Recuperamos la charla por medio del moniker y el id e indicamos que el modeloTalk traiga todo mediante el true //lo asignamos a la variable charla var charla = await campamentoRepositorio.GetTalkByMonikerAsync(moniker, idTalk, true); //Validamos si la charla existe if (charla == null) { return(NotFound("No se encontro la charla indicada")); } //Validamos si el modelo trae valores en la propiedad speaker, si trae recuperamos el speaker y lo asignamos a la variable //speaker, enseguida validamos si el speaker existe: la propiedad Charla.Speaker sera igual a la variable speaker if (modeloTalk.Speaker != null) { var speaker = await campamentoRepositorio.GetSpeakerAsync(modeloTalk.Speaker.SpeakerId); if (speaker != null) { charla.Speaker = speaker; } } //Mapeamos el modelo a la variable charla mapper.Map(modeloTalk, charla); //Validamos que los cambiamos hayan sido guardados con exito y hacemos solo el mapeo para la pura charla //Recordemos que la charla tiene 2 propiedades navegables Camp y Speaker //Tenemos que ir a la clase PerfilCampamento e indicar que el CreateMap ignore los valores de las 2 propiedades tanto de camp como de speaker if (await campamentoRepositorio.SaveChangesAsync()) { return(mapper.Map <TalkModel>(charla)); } else { return(BadRequest("Error al actualizar la BD")); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Fallo en la BD")); } }
public async Task <ActionResult <TalkModel> > UpdateTalk(string moniker, int talkId, TalkModel model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, talkId, true); if (talk == null) { return(NotFound("Could not find the talk")); } _mapper.Map(model, talk); if (model.Speaker != null) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); talk.Speaker = speaker; } if (await _repository.SaveChangesAsync()) { return(_mapper.Map <TalkModel>(talk)); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }
public async Task <ActionResult <TalkModel> > PutTalk(string moniker, int id, TalkModel model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { return(NotFound("Couldn't find the talk")); } if (talk.TalkId != model.TalkId) { return(BadRequest("Cannot update non-matching talk")); } _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 <TalkModel>(talk)); } } catch (Exception e) { Console.WriteLine(e); return(this.StatusCode(StatusCodes.Status500InternalServerError, "It's not you, it's us")); } return(BadRequest("Could not update talk")); }
public async Task <(int statusCode, string message, TalkModel talkModel)> UpdateTalk(string moniker, int id, TalkModel model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { return(404, "Couldn't find the talk", null); } _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(200, null, _mapper.Map <TalkModel>(talk)); } else { return(400, "Failed to update database.", null); } } catch (Exception) { return(500, "Failed to get Talks", null); } }
public async Task <(int statusCode, string message, TalkModel talkModel)> CreateTalk(string moniker, TalkModel model) { try { var camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(400, "Camp does not exist", null); } var talk = _mapper.Map <Talk>(model); talk.Camp = camp; if (model.Speaker == null) { return(400, "Speaker ID is required", null); } var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(400, "Speaker could not be found", null); } talk.Speaker = speaker; _repository.Add(talk); if (await _repository.SaveChangesAsync()) { return(200, null, _mapper.Map <TalkModel>(talk)); } else { return(400, "Failed to save new Talk", null); } } catch (Exception) { return(500, "Failed to get Talks", null); } }
public static string GetWebUrl(this TalkModel talkModel) { return ($"http://{AboutThisApp.AppLinksBaseDomain}/{AboutThisApp.SessionsSiteSubdirectory}/#{talkModel.Id}"); }
public async Task <ActionResult <TalkModel> > Put(string moniker, int talkId, TalkModel model) { try { var talk = await campRepository.GetTalkByMonikerAsync(moniker, talkId, true); if (talk is null) { return(NotFound("Could not find this talk.")); } if (model.Speaker != null) { var speaker = await campRepository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } else { return(BadRequest("Speaker not found.")); } } mapper.Map(model, talk); if (await campRepository.SaveChangesAsync()) { var location = linkGenerator.GetPathByAction( "Get", "Talks", new { moniker, talk.TalkId }); return(Created(location, mapper.Map <TalkModel>(talk))); } else { return(StatusCode(StatusCodes.Status500InternalServerError, "Error saving to database")); } } catch (Exception) { return(StatusCode( StatusCodes.Status500InternalServerError, "An error occurred.")); } }
public async Task <ActionResult <TalkModel> > UpdateTalk(string moniker, int id, TalkModel model) { try { var results = await _campRepository.GetTalkByMonikerAsync(moniker, id); if (results == null) { return(BadRequest("Talk not found")); } _mapper.Map(model, results); if (model.Speaker != null) { var speaker = await _campRepository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { results.Speaker = speaker; } } if (await _campRepository.SaveChangesAsync()) { return(_mapper.Map <TalkModel>(results)); } } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Error")); } return(BadRequest("Failed to update database")); }
public async Task <ActionResult <TalkModel> > Put(string moniker, int talkId, TalkModel model) { try { var talk = await _repo.GetTalkByMonikerAsync(moniker, talkId, true); if (talk == null) { return(NotFound($"Talk not found for moniker {moniker}")); } if (model.Speaker != null) { var speaker = await _repo.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } _mapper.Map(model, talk); if (await _repo.SaveChangesAsync()) { return(_mapper.Map <TalkModel>(talk)); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Failed to get talk for moniker {moniker}")); } return(BadRequest()); }
public async Task <ActionResult <TalkModel> > UpdateTalk(string moniker, int talkId, TalkModel model) { try { // added true option because without it, speaker changes aren't possible var talk = await _repository.GetTalkByMonikerAsync(moniker, talkId, true); if (talk == null) { return(BadRequest("Talk does not exist")); } _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 <TalkModel>(talk)); } return(BadRequest("Unabled to update talk")); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public async Task <ActionResult <TalkModel> > Update(string moniker, int talkId, [FromBody] TalkModel model) { try { var talk = await _repository.GetTalkByMonikerAsync(moniker, talkId, true); if (talk == null) { return(BadRequest(new { message = "Talk not exits." })); } if (talk.Speaker.SpeakerId != model.Speaker.SpeakerId) { var speaker = await _repository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Speaker not exist")); } talk.Speaker = speaker; } _mapper.Map(model, talk); if (await _repository.SaveChangesAsync()) { return(_mapper.Map <TalkModel>(talk)); } } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(StatusCode(StatusCodes.Status500InternalServerError, "Something goes wrong")); }