public async Task <ContestStarted> ExecuteAsync( ContestStart contestStart) { var contest = await _contestRepository.FetchByIdAsync( contestStart.Id); contest.Status = ContestStatus.InProgress; if (!contest.StartDate.HasValue) { contest.StartDate = DateTime.UtcNow; } contest = await _contestRepository.UpdateByIdAsync( contest.Id, contest); var contestStarted = _mapper.Map(contest, new ContestStarted()); await _eventPublisher.PublishAsync("contest.started", contestStarted); return(contestStarted); }
public async Task <ContestFetched> ExecuteAsync( ContestFetch contestFetch) { var contest = await _contestRepository.FetchByIdAsync( contestFetch.Id); if (contest == null) { throw new NullReferenceException($"Contest {contestFetch.Id} not found"); } var contestFetched = _mapper.Map(contest, new ContestFetched()); var contestLearners = await _contestLearnerRepository.FetchListAsync( contestFetch.Id); contestLearners = contestLearners.Where( x => !string.IsNullOrWhiteSpace(x.DisplayName)); _mapper.Map(contestLearners, contestFetched.Learners); return(contestFetched); }
public async Task <ContestNumberOfLearnersUpdated> ExecuteAsync( ContestNumberOfLearnersUpdate contestNumberOfLearnersUpdate) { var contest = await _contestRepository.FetchByIdAsync( contestNumberOfLearnersUpdate.ContestId); var contestLearners = await _contestLearnerRepository.FetchListAsync( contestNumberOfLearnersUpdate.ContestId); contest.NumberOfLearners = contestLearners.Count(); await _contestRepository.UpdateByIdAsync( contestNumberOfLearnersUpdate.ContestId, contest); var contestNumberOfLearnersUpdated = new ContestNumberOfLearnersUpdated { NumberOfLearners = contestLearners.Count() }; return(contestNumberOfLearnersUpdated); }
public async Task <ContestLearnerUpdated> ExecuteAsync( ContestLearnerUpdate contestLearnerUpdate) { var contest = await _contestRepository.FetchByIdAsync( contestLearnerUpdate.ContestId); if (contest == null) { throw new NullReferenceException($"Contest {contestLearnerUpdate.ContestId} not found"); } var contestLearner = await _contestLearnerRepository.FetchByIdAsync( contestLearnerUpdate.ContestId, contestLearnerUpdate.Id); if (contestLearner == null) { throw new ArgumentOutOfRangeException( $"Contest learner {contestLearnerUpdate.UserName} not found"); } contestLearner.UserName = contestLearnerUpdate.UserName.ToLower(); contestLearner.StartValue = contestLearnerUpdate.StartValue; var microsoftProfile = await _microsoftProfileRepository.FetchProfileAsync( contestLearner.UserName); contestLearner = _contestLearnerTargetValueUpdater.Update( contest, contestLearner); if (contest.IsStatus(ContestStatus.InProgress)) { contestLearner = _contestLearnerCurrentValueUpdater.Update( contest, contestLearner, microsoftProfile); } contestLearner = await _contestLearnerRepository.UpdateByIdAsync( contestLearnerUpdate.ContestId, contestLearnerUpdate.Id, contestLearner); var contestLearnerUpdated = _mapper.Map(contestLearner, new ContestLearnerUpdated()); await _eventPublisher.PublishAsync("contestlearner.updated", contestLearnerUpdated); return(contestLearnerUpdated); }
public async Task <ContestUpdated> ExecuteAsync( ContestUpdate contestUpdate) { var contest = await _contestRepository.FetchByIdAsync( contestUpdate.Id); _mapper.Map(contestUpdate, contest); if (!string.IsNullOrWhiteSpace(contestUpdate.Prizes)) { contest.HasPrizes = true; } if (contest.Type == ContestType.Leaderboard) { contest.TargetValue = null; } if (contest.Status == ContestStatus.InProgress && contest.StartDate == null) { contest.StartDate = DateTime.UtcNow; } if (contest.Status == ContestStatus.Ended && contest.EndDate == null) { contest.EndDate = DateTime.UtcNow; } contest = await _contestRepository.UpdateByIdAsync( contestUpdate.Id, contest); var contestUpdated = _mapper.Map(contest, new ContestUpdated()); await _eventPublisher.PublishAsync("contest.updated", contestUpdated); return(contestUpdated); }
public async Task <ContestCopied> ExecuteAsync( ContestCopy contestCopy) { var contest = await _contestRepository.FetchByIdAsync( contestCopy.Id); contest.Id = Guid.NewGuid(); contest.Name = contestCopy.Name; contest.Status = ContestStatus.InProgress; contest.StartDate = null; contest.EndDate = null; contest.CreatedOn = DateTime.UtcNow; contest = await _contestRepository.AddAsync( contest); var contestLearners = await _contestLearnerRepository.FetchListAsync( contestCopy.Id); foreach (var contestLearner in contestLearners) { contestLearner.Id = Guid.NewGuid(); contestLearner.ContestId = contest.Id; await _contestLearnerRepository.AddAsync( contest.Id, contestLearner); } var contestCopied = _mapper.Map(contest, new ContestCopied()); return(contestCopied); }
public async Task <ContestProgressUpdated> ExecuteAsync( ContestProgressUpdate contestUpdate) { var contest = await _contestRepository.FetchByIdAsync( contestUpdate.Id); if (contest.IsStatus(ContestStatus.InProgress)) { var contestLearners = await _contestLearnerRepository.FetchListAsync( contest.Id); foreach (var contestLearner in contestLearners) { var contestLearnerProgressUpdate = _mapper.Map(contestLearner, new ContestLearnerProgressUpdate()); await _eventPublisher.PublishAsync("contestlearnerprogress.update", contestLearnerProgressUpdate); } contest.LastProgressUpdateOn = DateTime.UtcNow; contest.NextProgressUpdateOn = contest.LastProgressUpdateOn.Value.AddMinutes(contest.ProgressUpdateInterval); contest = await _contestRepository.UpdateByIdAsync( contestUpdate.Id, contest); } var contestProgressUpdated = _mapper.Map(contest, new ContestProgressUpdated()); return(contestProgressUpdated); }
public async Task <ContestEnded> ExecuteAsync( ContestEnd contestEnd) { var contest = await _contestRepository.FetchByIdAsync( contestEnd.Id); contest.Status = ContestStatus.Ended; if (!contest.EndDate.HasValue) { contest.EndDate = DateTime.UtcNow; } contest = await _contestRepository.UpdateByIdAsync( contest.Id, contest); var contestEnded = _mapper.Map(contest, new ContestEnded()); return(contestEnded); }
public async Task <ContestLearnerStarted> ExecuteAsync( ContestLearnerStart contestLearnerStart) { var contestLearner = await _contestLearnerRepository.FetchByIdAsync( contestLearnerStart.ContestId, contestLearnerStart.Id); if (contestLearner == null) { throw new ArgumentOutOfRangeException($"Contest learner {contestLearnerStart.Id} not found."); } var contest = await _contestRepository.FetchByIdAsync( contestLearner.ContestId); var contestLearnerStarted = new ContestLearnerStarted(); try { var microsoftProfile = await _microsoftProfileRepository.FetchProfileAsync( contestLearner.UserName); contestLearner.Level = microsoftProfile.GameStatus.Level.LevelNumber; contestLearner.Points = _experiencePointsCalculator.Calculate( microsoftProfile.GameStatus.Level.LevelNumber, microsoftProfile.GameStatus.CurrentLevelPointsEarned); contestLearner = _contestLearnerStartValueUpdater.Update( contest, contestLearner, microsoftProfile, true); contestLearner = _contestLearnerCurrentValueUpdater.Update( contest, contestLearner, microsoftProfile); contestLearner.LastProgressUpdateOn = DateTime.UtcNow; contestLearner = await _contestLearnerRepository.UpdateByIdAsync( contestLearnerStart.ContestId, contestLearnerStart.Id, contestLearner); contestLearnerStarted = _mapper.Map(contestLearner, contestLearnerStarted); return(contestLearnerStarted); } catch (Exception ex) { throw new MicrosoftProfileException(contestLearner, ex); } }
public async Task <ContestLearnerProgressUpdated> ExecuteAsync( ContestLearnerProgressUpdate contestLearnerProgressUpdate) { var contest = await _contestRepository.FetchByIdAsync( contestLearnerProgressUpdate.ContestId); if (contest == null) { throw new NullReferenceException($"Contest {contestLearnerProgressUpdate.ContestId} not found"); } var contestLearner = await _contestLearnerRepository.FetchByIdAsync( contestLearnerProgressUpdate.ContestId, contestLearnerProgressUpdate.Id); var contestLearnerProgressUpdated = new ContestLearnerProgressUpdated(); if (contest.Status != ContestStatus.InProgress || string.IsNullOrWhiteSpace(contestLearner.DisplayName)) { contestLearnerProgressUpdated = _mapper.Map(contestLearner, contestLearnerProgressUpdated); return(contestLearnerProgressUpdated); } try { var microsoftProfile = await _microsoftProfileRepository.FetchProfileAsync( contestLearner.UserName); contestLearner.Level = microsoftProfile.GameStatus.Level.LevelNumber; contestLearner.Points = _experiencePointsCalculator.Calculate( microsoftProfile.GameStatus.Level.LevelNumber, microsoftProfile.GameStatus.CurrentLevelPointsEarned); contestLearner = _contestLearnerTargetValueUpdater.Update( contest, contestLearner); contestLearner = _contestLearnerCurrentValueUpdater.Update( contest, contestLearner, microsoftProfile); contestLearner.LastProgressUpdateOn = DateTime.UtcNow; contestLearner = await _contestLearnerRepository.UpdateByIdAsync( contestLearnerProgressUpdate.ContestId, contestLearnerProgressUpdate.Id, contestLearner); contestLearnerProgressUpdated = _mapper.Map(contestLearner, contestLearnerProgressUpdated); return(contestLearnerProgressUpdated); } catch (Exception ex) { throw new MicrosoftProfileException(contestLearner, ex); } }