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 <ContestLearnerAdded> ExecuteAsync( ContestLearnerAdd contestLearnerAdd) { var contest = await _contestRepository.FetchByIdAsync( contestLearnerAdd.ContestId); if (contest == null) { throw new NullReferenceException($"Contest {contestLearnerAdd.ContestId} not found"); } contestLearnerAdd.UserName = contestLearnerAdd.UserName.ToLower(); var contestLearner = await _contestLearnerRepository.FetchByUserNameAsync( contestLearnerAdd.ContestId, contestLearnerAdd.UserName); if (contestLearner != null) { throw new ArgumentOutOfRangeException( $"The contest learner {contestLearnerAdd.UserName} already exists."); } contestLearner = _mapper.Map(contestLearnerAdd, new ContestLearner()); var microsoftProfile = await _microsoftProfileRepository.FetchProfileAsync( contestLearnerAdd.UserName); contestLearner = _mapper.Map(microsoftProfile, contestLearner); contestLearner = _contestLearnerStartValueUpdater.Update( contest, contestLearner, microsoftProfile); contestLearner = _contestLearnerTargetValueUpdater.Update( contest, contestLearner); if (contest.IsStatus(ContestStatus.InProgress)) { contestLearner = _contestLearnerCurrentValueUpdater.Update( contest, contestLearner, microsoftProfile); } contestLearner = await _contestLearnerRepository.AddAsync( contestLearner.ContestId, contestLearner); var contestLearnerAdded = _mapper.Map(contestLearner, new ContestLearnerAdded()); await _eventPublisher.PublishAsync("contestlearner.added", contestLearnerAdded); return(contestLearnerAdded); }