Beispiel #1
0
        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 <ContestLearnerFetched> ExecuteAsync(
            ContestLearnerFetch contestLearnerFetch)
        {
            var contestLearner =
                await _contestLearnerRepository.FetchByIdAsync(
                    contestLearnerFetch.ContestId,
                    contestLearnerFetch.Id);

            if (contestLearner == null)
            {
                throw new NullReferenceException($"Contest {contestLearnerFetch.ContestId} not found.");
            }

            var contestLearnerFetched =
                _mapper.Map(contestLearner, new ContestLearnerFetched());

            return(contestLearnerFetched);
        }
Beispiel #3
0
        public async Task <ContestLearnerDeleted> ExecuteAsync(
            ContestLearnerDelete contestLearnerDelete)
        {
            var contestLearner =
                await _contestLearnerRepository.FetchByIdAsync(
                    contestLearnerDelete.ContestId,
                    contestLearnerDelete.Id);

            if (contestLearner == null)
            {
                throw new ArgumentOutOfRangeException($"Contest learner {contestLearnerDelete.Id} not found.");
            }

            await _contestLearnerRepository.DeleteByIdAsync(
                contestLearnerDelete.ContestId,
                contestLearnerDelete.Id);

            var contestLearnerDeleted =
                _mapper.Map(contestLearner, new ContestLearnerDeleted());

            await _eventPublisher.PublishAsync("contestlearner.deleted", contestLearnerDeleted);

            return(contestLearnerDeleted);
        }
Beispiel #4
0
        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);
            }
        }