public async Task <Guid> Handle(CreateQuestionCommand request, CancellationToken cancellationToken)
        {
            var question = _mapper.Map <Question>(request);

            question.EmployerId = _currentUserService.GetCurrentUserId();

            await _store.AddEntityAsync(question, saveChanges : true);

            var options = _mapper.Map <List <Option> >(request.Options);

            foreach (var option in options)
            {
                option.QuestionId = question.Id;
                await _store.AddEntityAsync(option);
            }

            await _store.SaveChangesAsync();

            return(question.Id);
        }
        public async Task <Guid> Handle(CreateVacancyCommand request, CancellationToken cancellationToken)
        {
            var vacancy    = _mapper.Map <Vacancy>(request);
            var employerId = _currentUserService.GetCurrentUserId();

            vacancy.DateCreated = DateTime.Now;

            vacancy.EmployerId = employerId;

            await _store.AddEntityAsync(vacancy, saveChanges : true);

            return(vacancy.Id);
        }
        public async Task <Guid> Handle(CreateCampaignCommand request, CancellationToken cancellationToken)
        {
            var campaign = new Campaign()
            {
                Name      = request.Name,
                StartDate = request.StartDate,
                EndDate   = request.EndDate,
                AdminId   = _currentUserService.GetCurrentUserId()
            };

            var addedEntity = await _store.AddEntityAsync(campaign, saveChanges : true);

            return(addedEntity.Id);
        }
        public async Task <short> Handle(AddTestResultCommand request, CancellationToken cancellationToken)
        {
            var  selectedVacancy = _store.Vacancies.FirstOrDefault(v => v.Id == request.VacancyId);
            Guid studentId       = _currentUserService.GetCurrentUserId();

            if (selectedVacancy == null)
            {
                throw new Exception("Vacancy doe not exist.");
            }

            var questionIds = request.Answers.Select(ans => ans.Id).ToList();

            var questionOptionDict = await _store.Questions
                                     .Include(q => q.Options)
                                     .Where(q => questionIds.Contains(q.Id))
                                     .Select(q => new KeyValuePair <Guid, List <Guid> >
                                             (
                                                 q.Id,
                                                 q.Options.Where(op => op.IsCorrect).Select(op => op.Id).ToList()
                                             ))
                                     .ToDictionaryAsync(x => x.Key, x => x.Value);

            int correctOptionCount      = questionOptionDict.Sum(q => q.Value.Count());
            int correctUserAnswersCount = request
                                          .Answers.Sum(ans => ans.ChoosedOptions.Intersect(questionOptionDict[ans.Id]).Count());

            short score = (short)Math.Round(((double)correctUserAnswersCount / correctOptionCount) * 100.0, 0);


            short priority = (short)(_store.StudentVacancies
                                     .Where(x => x.StudentId == studentId && x.Vacancy.CampaignId == selectedVacancy.CampaignId)
                                     .Count() + 1);

            var newStudentVacancy = new StudentVacancy
            {
                StudentId = _currentUserService.GetCurrentUserId(),
                VacancyId = request.VacancyId,
                Score     = score,
                Priority  = priority
            };

            await _store.AddEntityAsync(newStudentVacancy);

            await _store.SaveChangesAsync();

            return(score);
        }