public Guid SaveTalk(ITalk talk) { using (var db = new RtDataContext(_connectionString)) { var dto = db.Talks.FirstOrDefault(t => t.TalkId == talk.TalkId); if (dto == null || talk.TalkId == Guid.Empty) { dto = new TalkDto() { TalkId = Guid.NewGuid() }; db.Talks.InsertOnSubmit(dto); } dto.LocationId = talk.LocationId; dto.PresenterId = talk.PresenterId; dto.Topic = talk.Topic; db.SubmitChanges(); return(dto.TalkId); } }
public async Task <ActionResult <TalkDto> > Put(string moniker, int id, TalkDto talkDto) { try { var talk = await repository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { return(BadRequest("Cannot find talk.")); } if (talkDto.Speaker != null) { var speaker = await repository.GetSpeakerAsync(talkDto.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } if (await repository.SaveChangesAsync()) { return(mapper.Map <TalkDto>(talk)); } else { return(BadRequest("Failed to update database.")); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
// GET: TalksController/Edit/5 public ActionResult Edit(int id) { var talk = context.Talks.Find(id); TalkDto model = new TalkDto(); model = mapper.Map <TalkDto>(talk); return(View(model)); }
public ActionResult <SpeakerDto> Create(TalkDto talkDto) { var model = mapper.Map <Talk>(talkDto); service.Add(model); service.Save(); var talkRead = mapper.Map <TalkDto>(model); return(CreatedAtRoute(nameof(GetById), new { Id = talkDto.ID }, talkRead)); }
public IActionResult Create(TalkDto model) { if (ModelState.IsValid) { Talk newTalk = new Talk(); newTalk = mapper.Map <Talk>(model); service.Add(newTalk); service.Save(); return(RedirectToAction("List", "Talk")); } return(View(model)); }
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 ActionResult Update(int id, TalkDto talkUpdateDto) { var model = service.getTalkById(id); if (model == null) { return(NotFound()); } mapper.Map(talkUpdateDto, model); service.Update(model); service.Save(); return(NoContent()); }
public IActionResult Edit(int?id) { TalkDto model = new TalkDto(); if (id.HasValue) { var existingTalk = service.GetTalkById(id.Value); if (existingTalk != null) { model = mapper.Map <TalkDto>(existingTalk); } } return(View(model)); }
public async Task <ActionResult <TalkDto> > Post(string moniker, TalkDto talkDto) { try { var camp = await repository.GetCampAsync(moniker); if (camp == null) { return(BadRequest("Camp does not exist")); } var talk = mapper.Map <Talk>(talkDto); talk.Camp = camp; if (talkDto.Speaker == null) { return(BadRequest("Speaker ID is required")); } var speaker = await repository.GetSpeakerAsync(talkDto.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 <TalkDto>(talk))); } else { return(BadRequest("Failed to save new talk")); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public IActionResult Edit(TalkDto incomingModel) { if (incomingModel.ID > 0) { if (ModelState.IsValid) { var talkInDb = new Talk(); talkInDb = mapper.Map <Talk>(incomingModel); service.Update(talkInDb); service.Save(); return(RedirectToAction("List", "Talks")); } } return(View(incomingModel)); }
public async Task <IHttpActionResult> Put(string moniker, int talkId, TalkDto talkDto) { try { if (talkId != talkDto.TalkId) { ModelState.AddModelError("TalkId", "talkId != TalkId ==> Confusion of da highest order!!! :)"); } if (ModelState.IsValid) { Talk talk = await _repository.GetTalkByMonikerAsync(moniker, talkDto.TalkId, true); if (talk == null) { return(NotFound()); } // We don't update the camp (foreign key) here because it's not a part of the TalkDto. See our Mapping Profile too... // If necessary, change the speaker of the talk... if (talkDto.Speaker != null && talkDto.Speaker.SpeakerId != talk.Speaker.SpeakerId) { Speaker speaker = await _repository.GetSpeakerAsync(talkDto.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("No speaker found with this Id..!")); } talk.Speaker = speaker; } _mapper.Map(talkDto, talk); if (await _repository.SaveChangesAsync()) { return(Ok(_mapper.Map <TalkDto>(talk))); //return StatusCode(HttpStatusCode.NoContent); } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); // TODO: on the 2nd call without changing the body --> "exceptionMessage": "The model state is valid.\r\nParameter name: modelState". Why??? }
public ActionResult Create(TalkDto model) { if (ModelState.IsValid) { //create a new Speaker Talk newTalk = new Talk(); //newTalk = mapper.Map<Talk>(model); //service.Add(newTalk); ////save speaker //service.Save(); //redirect to List return(RedirectToAction("List", "Speakers")); } return(View(model)); }
public async Task <QueryResults <TalkDto> > CreateTalk(Guid me, TalkDto talkDto) { var userMe = await db.Users.GetByIdAsync(me) ?? throw new NotFoundApiException(); var receiver = await db.Users.GetByIdAsync(talkDto.Receiver) ?? throw new NotFoundApiException(); var talk = talkDto.ConvertToModel(userMe, receiver); db.Talks.Add(talk); await db.SaveChangesAsync(); return(new QueryResults <TalkDto> { Data = talk.ConvertToDto() }); }
public IActionResult Edit(TalkDto incomingModel) { if (incomingModel.Id > 0) { if (ModelState.IsValid) { Talk model = new Talk(); model = mapper.Map <Talk>(incomingModel); service.Update(model); service.Save(); return(RedirectToAction("List", "Talk")); } } return(View(incomingModel)); }
public async Task <IActionResult> CreateTalk([FromBody] TalkDto talkDto) { try { var myId = ExtractIdFromToken(Request.Headers[HttpRequestHeader.Authorization.ToString()]); var talk = await service.CreateTalk(myId, talkDto); return(Created($"{Request.Path.Value}/{talk.Data.Id}", talk)); } catch (HttpResponseException) { throw; } catch (Exception) { throw new BadRequestApiException(); } }
public async Task <ActionResult <TalkDto> > Post(string moniker, TalkDto model) { try { var camp = await _campRepository.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 rquired")); } var speaker = await _campRepository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker == null) { return(BadRequest("Speaker could not be found")); } talk.Speaker = speaker; _campRepository.Add(talk); _logger.LogInformation("Talk added to collection"); if (await _campRepository.SaveChangesAsync()) { var url = _linkGenerator.GetPathByAction(HttpContext, "GetTalk", values: new { moniker, id = talk.TalkId }); return(Created(url, _mapper.Map <TalkDto>(talk))); } else { return(BadRequest("Failed to save talk")); } } catch (Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Failure retrieving camps\n" + ex.Message + "\n" + ex.InnerException ?? "")); } }
public async Task <IHttpActionResult> GetTalk(string moniker, int id, bool includeSpeakers = false) { try { Talk talk = await _repository.GetTalkByMonikerAsync(moniker, id, includeSpeakers); if (talk == null) { return(NotFound()); } //TalkDto mappedTalk = _mapper.Map<Talk, TalkDto>(talk); TalkDto mappedTalk = _mapper.Map <TalkDto>(talk); return(Ok(mappedTalk)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <ActionResult <TalkDto> > Put(string moniker, int id, TalkDto model) { try { var talk = await _campRepository.GetTalkByMonikerAsync(moniker, id, true); if (talk == null) { return(BadRequest("Talk does not exist!")); } if (model.Speaker != null) { var speaker = await _campRepository.GetSpeakerAsync(model.Speaker.SpeakerId); if (speaker != null) { talk.Speaker = speaker; } } _mapper.Map(model, talk); if (await _campRepository.SaveChangesAsync()) { return(_mapper.Map <TalkDto>(talk)); } else { return(BadRequest("Failed to update")); } } catch (Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Failure updating talk" + ex.Message + "\n" + ex.InnerException ?? "")); } }
public IActionResult Create() { var model = new TalkDto(); return(View(model)); }
public IActionResult FillData() { var model = new TalkDto(); return(new JsonResult(model)); }