예제 #1
0
        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);
        }
예제 #2
0
        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);
        }