// GET: Predictions/Predict?parentId=5 public ActionResult Predict(int parentId) { PredictionsDetailsViewModel model = new PredictionsDetailsViewModel(); model.MatchId = parentId; ViewBag.Title = "Predict"; var match = webClient.ExecuteGet <MatchDto>(new Models.ApiRequest() { EndPoint = $"matches/{parentId}" }); if (match == null) { return(View()); } var currentUser = AuthorizeExtensions.GetCurrentUser(); if (currentUser == null) { throw new UnauthorizedAccessException(); } model.UserId = currentUser.Id; var prediction = webClient.ExecuteGet <IEnumerable <PredictionDto> >(new Models.ApiRequest() { EndPoint = "predictions" }) ?.Select(q => new PredictionsListItem() { Id = q.Id, UserId = q.UserId, MatchId = q.MatchId, PredictedBoxerId = q.PredictedBoxerId })?. FirstOrDefault(p => p.MatchId == match.Id && p.UserId == currentUser.Id); if (prediction != null) { model.Id = prediction.Id; model.PredictedBoxerId = prediction.PredictedBoxerId; } var boxers = webClient.ExecuteGet <IEnumerable <BoxerDto> >(new Models.ApiRequest() { EndPoint = "boxers" }) ?.Select(q => new BoxersListItem() { Id = q.Id, Name = q.Name })?.Where(b => b.Id == match.Boxer1Id || b.Id == match.Boxer2Id)?.ToList(); if (boxers != null && boxers.Count == 2) { if (model.PredictedBoxerId != 0) { ViewData["PredictedBoxer"] = boxers.FirstOrDefault(b => b.Id == model.PredictedBoxerId); var boxersList = new List <BoxersListItem>(); if (model.PredictedBoxerId == match.Boxer1Id) { boxersList.Add(null); } boxersList.Add(boxers.FirstOrDefault(b => b.Id != model.PredictedBoxerId)); if (model.PredictedBoxerId == match.Boxer2Id) { boxersList.Add(null); } ViewData["Boxers"] = boxersList; } else { ViewData["Boxers"] = boxers; } } return(View(model)); }
// GET: Matches public ActionResult Index([FromUri] int skip = 0, [FromUri] int take = 10, [FromUri] string search = null, [FromUri] bool?pastUnfinished = null) { if (pastUnfinished.HasValue && pastUnfinished.Value && !AuthorizeExtensions.CurrentUserIsAdmin()) { throw new UnauthorizedAccessException(); } MatchesListViewModel model = new MatchesListViewModel(); var searchQueryParam = string.Empty; if (!string.IsNullOrEmpty(search)) { searchQueryParam = $"&search={search}"; ViewData["SearchString"] = search; } var pastUnfinishedQueryParam = string.Empty; if (pastUnfinished.HasValue) { pastUnfinishedQueryParam = $"&pastUnfinished={pastUnfinished.Value}"; ViewData["PastUnfinished"] = pastUnfinished.Value; } model.Items = webClient.ExecuteGet <IEnumerable <MatchDto> >(new Models.ApiRequest() { EndPoint = $"matches?skip={skip}&take={take}{searchQueryParam}{pastUnfinishedQueryParam}" }) ?.Select(q => new MatchesListItem() { Id = q.Id, Boxer1Id = q.Boxer1Id, Boxer2Id = q.Boxer2Id, Boxer1 = new BoxersListItem(q.Boxer1.Name), Boxer2 = new BoxersListItem(q.Boxer2.Name), Address = q.Address, Time = q.Time, Description = q.Description, WinnerId = q.WinnerId })?.ToList(); var currentUser = AuthorizeExtensions.GetCurrentUser(); var currentUserId = currentUser != null ? currentUser.Id : 0; var predictions = webClient.ExecuteGet <IEnumerable <PredictionDto> >(new Models.ApiRequest() { EndPoint = "predictions" }) ?.Select(q => new PredictionsListItem() { Id = q.Id, PredictedBoxerId = q.PredictedBoxerId, MatchId = q.MatchId, UserId = q.UserId })?.Where(p => p.UserId == currentUserId)?.ToList(); ViewData["Predictions"] = predictions; ViewData["Page"] = (skip / take) + 1; ViewData["PageSize"] = take; return(View(model)); }
public ActionResult Predict(PredictionsDetailsViewModel model) { try { var currentUser = AuthorizeExtensions.GetCurrentUser(); var match = webClient.ExecuteGet <MatchDto>(new Models.ApiRequest() { EndPoint = $"matches/{model.MatchId}" }); if (currentUser == null || match == null) { return(View()); } if (Request.Form[match.Boxer1Id.ToString()] != null) { model.PredictedBoxerId = match.Boxer1Id; } else if (Request.Form[match.Boxer2Id.ToString()] != null) { model.PredictedBoxerId = match.Boxer2Id; } else { return(View()); } var prediction = webClient.ExecuteGet <IEnumerable <PredictionDto> >(new Models.ApiRequest() { EndPoint = "predictions" })?. FirstOrDefault(p => p.MatchId == model.MatchId && p.UserId == currentUser.Id); if (prediction != null) { webClient.ExecutePut <object>(new Models.ApiRequest() { EndPoint = string.Format("predictions/{0}", prediction.Id), Request = new PredictionDto() { UserId = model.UserId, MatchId = model.MatchId, PredictedBoxerId = model.PredictedBoxerId } }); } else { webClient.ExecutePost <object>(new Models.ApiRequest() { EndPoint = string.Format("predictions"), Request = new PredictionDto() { UserId = model.UserId, MatchId = model.MatchId, PredictedBoxerId = model.PredictedBoxerId } }); } return(RedirectToAction("Index", controllerName: "Matches")); } catch { return(View()); } }