public async Task <IActionResult> Create([Bind("Id,InnovationScore,UsefulnessScore,QualityScore,CompanyValueScore,PresentationScore,Team")] JudgeScoreViewModel teamScoreViewModel) { if (ModelState.IsValid) { var me = User.FindFirst(ClaimTypes.NameIdentifier).Value; //If user already inserted score for this team, don't reinsert inform them var record = await _context.JudgeScores.Where(x => x.Team.TeamCode == teamScoreViewModel.Team && x.KolpiUserId == me).ToListAsync(); var team = await _context.Teams.FirstAsync(x => x.TeamCode.Equals(teamScoreViewModel.Team)); if (record.Any()) { return(RedirectToAction(nameof(HomeController.Error), "Home", new { errorCode = "Duplicate Record", message = $"You aleady evaluated team: {team.TeamName}. Please update their score if you wish to." })); } var teamScore = new JudgeScore(teamScoreViewModel) { KolpiUserId = me, Team = team }; _context.Add(teamScore); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //Something went wrong, keep user on same page return(View(teamScoreViewModel)); }
public async Task <IActionResult> VoteTeams(ParticipantVoteViewModel voteViewModel) { var editSetting = await _context.Settings.FirstOrDefaultAsync(x => x.Name == Setting.Voting); var allowVoting = editSetting?.Value == "1"; if (!allowVoting) { ModelState.AddModelError("", $"Voting Disabled: Bear with us! Once judeges submit their scores, we will open this up to vote for teams of your choice."); voteViewModel.Teams = await GetTeamsSelectList(); return(View(voteViewModel)); } if (ModelState.IsValid) { // Validation - dropdown uniqueness var votes = new List <string> { voteViewModel.OrderOneTeam, voteViewModel.OrderTwoTeam, voteViewModel.OrderThreeTeam, voteViewModel.OrderFourTeam, voteViewModel.OrderFiveTeam }; if (votes.Distinct().Count() != votes.Count) { ModelState.AddModelError("", $"You can't make same team take multiple ranks, or do you? Your top five selections must be unique."); voteViewModel.Teams = await GetTeamsSelectList(); return(View(voteViewModel)); } var me = User.FindFirst(ClaimTypes.NameIdentifier).Value; //Validation - Has user already voted? var record = await _context.ParticipantVotes.Where(x => x.UserId.Equals(me) && x.VotedOn.Value.Year == DateTime.Now.Year).ToListAsync(); if (record.Any()) { return(RedirectToAction(nameof(HomeController.Error), "Home", new { errorCode = "Already Voted", message = $"Don't be so greedy :), You have aleady voted for people's choice award. Thanks!" })); } var participantVote = new ParticipantVote(voteViewModel) { UserId = me, VotedOn = DateTime.Now }; _context.Add(participantVote); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(PeoplesChoiceController.Thanks))); } //Something went wrong, keep user on same page return(View(voteViewModel)); }
public async Task <IActionResult> CreateSurveyThread(SurveyThreadViewModel surveyViewModel) { if (!ModelState.IsValid) { return(View(surveyViewModel)); } var buffer = new byte[5]; new Random().NextBytes(buffer); var hash = string.Join("", buffer.Select(b => b.ToString("X2"))); surveyViewModel.ThreadHash = hash; surveyViewModel.AbsoluteUrl = $"{Request.Scheme}://{Request.Host.Value}/feedback/{hash}"; surveyViewModel.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; var survey = new SurveyThread(surveyViewModel); _context.Add(survey); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(AdminIndex))); }
public async Task <IActionResult> Create([Bind("Name,Value")] Settings settings) { if (ModelState.IsValid) { settings.DateCreated = DateTime.Now; settings.CreatedBy = User.FindFirst(ClaimTypes.NameIdentifier).Value; _context.Add(settings); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(settings)); }
public async Task <IActionResult> Create([Bind("TeamName,Avatar,Theme,ProblemStatement,ITRequirements,Location,OtherRequirements,Participants")] TeamViewModel teamViewModel) { //if (teamViewModel.Location.Equals("Kathmandu", StringComparison.InvariantCultureIgnoreCase)) // return RedirectToAction("Error", "Home", new ErrorViewModel { ErrorCode = "Registration", Message = "Team registration expired for Nepal center." }); if (ModelState.IsValid) { if (!ParticipantsEnteredCorrectly(teamViewModel.Participants)) { ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); } //Raw participants in text are good to deserialize var participantsDtos = TeamViewModel.DeserializeParticipants(teamViewModel.Participants); List <Participant> partcipantsOnDb = _context.Participants .Where(x => x.Team.CreatedOn.Year == DateTime.Now.Year).ToList(); //partcipantsOnDb = _context.Teams // .Include(x => x.Participants) // .Where(x => IsCurrentYear(x.CreatedOn)) // .SelectMany(x => x.Participants).ToList(); var alreadyTakenParticipants = partcipantsOnDb.Intersect(participantsDtos, ComplexTypeComparer <Participant> .Create(x => x.Inumber)); if (alreadyTakenParticipants.Any()) { ModelState.AddModelError("Participants", $"Participants submitted are already involved with other teams: {string.Join(", ", alreadyTakenParticipants.Select(x => x.Name))}. Note: individual participants are distinguished by their INumber."); ViewData["locations"] = FetchEventLocationSelectItemList(); return(View(teamViewModel)); } //Autogenerate properties at creation time teamViewModel.CreatedBy = User.FindFirst(ClaimTypes.Name)?.Value ?? ""; teamViewModel.CreatedOn = DateTime.Now; teamViewModel.TeamCode = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", ""); _context.Add(new Team(teamViewModel, participantsDtos)); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(teamViewModel)); }