Exemple #1
0
        public async Task <TopicDto> GetByIdAsync(string userId, int topicId)
        {
            using (IDbConnection connection = _connectionProvider.GetConnection())
            {
                TopicDto topic = await connection.QueryFirstOrDefaultAsync <TopicDto>(GET_TOPICS, new { topicId });

                NotFoundException.ThrowIfNull(topic, nameof(topic));

                IEnumerable <EventDto> events = await connection.QueryAsync <EventDto>(GET_EVENTS_FOR_TOPIC, new { topicId });

                IEnumerable <QuestionDto> questions = await connection.QueryAsync <QuestionDto>(GET_QUESTIONS_FOR_TOPIC, new { topicId });

                IEnumerable <AnswerDto> answers = await connection.QueryAsync <AnswerDto>(GET_ANSWERS_FOR_TOPIC, new { topicId });

                IEnumerable <AttemptDto> attempts = await connection.QueryAsync <AttemptDto>(GET_ATTEMPTS_FOR_USER, new { userId, topicId });

                ILookup <int, AnswerDto> answerLokup = answers.ToLookup(x => x.QuestionId);

                foreach (QuestionDto question in questions)
                {
                    question.Answers = answerLokup[question.Id];
                }

                topic.Events    = events;
                topic.Questions = questions;
                topic.Attempts  = attempts;

                return(topic);
            }
        }
Exemple #2
0
        public async Task <AttemptDto> Handle(CreateAttemptCommand request, CancellationToken cancellationToken)
        {
            Topic topic = await _topicRepository.GetAsync(request.TopicId.Value);

            NotFoundException.ThrowIfNull(topic, nameof(topic));

            Attempt attempt = topic.AddAttempt(request.UserId, request.Correct.Value, request.Incorrect.Value);

            await _topicRepository.UnitOfWork.SaveEntitiesAsync();

            return(_mapper.Map <AttemptDto>(attempt));
        }
        public async Task <int> Handle(CreateQuestionCommand request, CancellationToken cancellationToken)
        {
            Topic topic = await _topicRepository.GetAsync(request.TopicId.Value);

            NotFoundException.ThrowIfNull(topic, nameof(topic));

            Question question = topic.AddQuestion(
                request.Answers.Select(x => new Answer(x.Text, x.IsCorrect.Value)).ToList(),
                request.EventId,
                request.Text);

            await _topicRepository.UnitOfWork.SaveEntitiesAsync();

            return(question.Id);
        }
Exemple #4
0
        public async Task <int> Handle(CreateEventCommand request, CancellationToken cancellationToken)
        {
            Topic topic = await _topicRepository.GetAsync(request.TopicId.Value);

            NotFoundException.ThrowIfNull(topic, nameof(topic));

            Event @event = topic.AddEvent(
                request.Title,
                request.Content,
                request.StartDate.Value,
                request.EndDate,
                request.X.Value,
                request.Y.Value,
                request.EventTypeId.Value);

            await _topicRepository.UnitOfWork.SaveEntitiesAsync();

            return(@event.Id);
        }