public ActionResult <AnswerGetResponse> PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists = _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }

            var savedAnswer = _dataRepository.PostAnswer(new AnswerPostFullRequest
            {
                QuestionId = answerPostRequest.QuestionId.Value,
                Content    = answerPostRequest.Content,
                UserId     = "1",
                UserName   = "******",
                Created    = DateTime.UtcNow
            }
                                                         );

            _questionsHub.Clients.Group(
                $"Question-{answerPostRequest.QuestionId.Value}")
            .SendAsync(
                "ReceiveQuestion",
                _dataRepository.GetQuestion(
                    answerPostRequest.QuestionId.Value));

            return(savedAnswer);
        }
Esempio n. 2
0
        public async Task <ActionResult <AnswerGetResponse> > PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists = await _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }

            var savedAnswer = await _dataRepository.PostAnswer(new AnswerPostFullRequest
            {
                QuestionId = answerPostRequest.QuestionId.Value,
                Content    = answerPostRequest.Content,
                UserId     = User.FindFirst(ClaimTypes.NameIdentifier).Value,
                UserName   = await GetUserName(),
                Created    = DateTime.UtcNow
            });

            _cache.Remove(answerPostRequest.QuestionId.Value);

            await _questionHubContext.Clients.Group($"Question-{answerPostRequest.QuestionId.Value}")
            .SendAsync("ReceiveQuestion", _dataRepository.GetQuestion(answerPostRequest.QuestionId.Value));

            return(savedAnswer);
        }
Esempio n. 3
0
        public async Task <ActionResult <AnswerGetResponse> > PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists =
                await _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }

            var userId   = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var userName = await GetUserName();

            var converted = _mapper.Map <AnswerPostFullRequest>(answerPostRequest, opt =>
            {
                opt.Items["UserId"]   = userId;
                opt.Items["UserName"] = userName;
            });

            var savedAnswer = await _dataRepository.PostAnswer(converted);

            _cache.Remove(answerPostRequest.QuestionId.Value);

            await _questionHubContext.Clients.Group(
                $"Question-{answerPostRequest.QuestionId.Value}"
                ).SendAsync(
                "ReceiveQuestion",
                _dataRepository.GetQuestion(answerPostRequest.QuestionId.Value)
                );

            return(savedAnswer);
        }
        public ActionResult <AnswerGetResponse> PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists = _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }
            var savedAnswer = _dataRepository.PostAnswer(new AnswerPostFullRequest
            {
                QuestionId = answerPostRequest.QuestionId.Value,
                Content    = answerPostRequest.Content,
                UserId     = "1",
                UserName   = "******",
                Created    = DateTime.UtcNow
            });

            ////this pushes questions with the new answer to clients that have subcribed to the question.
            //_questionHubContext.Clients.Group(
            //    $"Question-{answerPostRequest.QuestionId.Value}"
            //    ).SendAsync("RecieveQuestion", _dataRepository.GetQuestion(answerPostRequest.QuestionId.Value));
            ////------------------------------------------------------------------------------------------------

            return(savedAnswer);
        }
Esempio n. 5
0
        public AnswerGetResponse PostAnswer(AnswerPostRequest answer)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();
                return(connection.QueryFirst <AnswerGetResponse>(
                           @"EXEC dbo.Answer_Post @QuestionId = @QuestionId, @Content = @Content, 
@UserId = @UserId, @UserName = @UserName, @Created = @Created", answer
                           ));
            }
        }
        public ActionResult <AnswerGetResponse> PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists = _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }
            var savedAnswer = _dataRepository.PostAnswer(new AnswerPostFullRequest
            {
                QuestionId = answerPostRequest.QuestionId.Value,
                Content    = answerPostRequest.Content,
                UserId     = "1",
                UserName   = "******",
                Created    = DateTime.UtcNow
            });

            return(savedAnswer);
        }
Esempio n. 7
0
        public async Task <ActionResult <AnswerGetResponse> > PostAnswer(AnswerPostRequest answerPostRequest)
        {
            var questionExists = await _dataRepository.QuestionExists(answerPostRequest.QuestionId.Value);

            if (!questionExists)
            {
                return(NotFound());
            }

            // mapping data from request model in the data respitory.
            // For big projects can also using AutoMapper
            var savedAnswer = await _dataRepository.PostAnswer(new AnswerPostFullRequest
            {
                QuestionId = answerPostRequest.QuestionId.Value,
                Content    = answerPostRequest.Content,
                UserId     = User.FindFirst(ClaimTypes.NameIdentifier).Value,
                UserName   = "******",
                Created    = DateTime.UtcNow
            });

            _cache.Remove(answerPostRequest.QuestionId.Value);

            /**
             * We get access to the SignalR group through the Group method in the Clients property in the hub context
             * by passing in the group name. Remember that the group name is the word "Question",
             * followed by a hyphen and then the question ID. Then, we use the SendAsync method to push
             * the question with the new answer to all the clients in the group. A handler called ReceiveQuestion
             * will be invoked in the client, with the question being passed in as the parameter after
             * we have got it from the data repository.
             */

            await _questionHubContext.Clients.Group(
                $"Question-{answerPostRequest.QuestionId.Value}").
            SendAsync("ReceiveQuestion",
                      _dataRepository.GetQuestion(answerPostRequest.QuestionId.Value));


            return(savedAnswer);
        }