Beispiel #1
0
        public async Task AnswerQuestion(Answer answer, CancellationToken cancellationToken)
        {
            var currentUserId = _requestUserProvider.GetUserId();

            if (string.IsNullOrEmpty(currentUserId))
            {
                throw new Exception("User not found");
            }

            var question = await _questionGateway.GetQuestion(answer.QuestionId);

            if (question == null)
            {
                throw new Exception("Question not found");
            }
            try
            {
                answer.AuthorId = currentUserId;
                answer.Date     = DateTime.Now;
                await _answersGateway.AddAnswer(answer, cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Beispiel #2
0
        public async Task AskQuestion(Question question, CancellationToken cancellationToken)
        {
            var currentUserId = _requestUserProvider.GetUserId();

            if (string.IsNullOrEmpty(currentUserId))
            {
                throw new Exception("User not found");
            }

            var questions = await _questionGateway.GetAll(cancellationToken);

            if (questions.Any(t => t.Title == question.Title))
            {
                throw new Exception("Title already exist!");
            }

            try
            {
                question.DateCreated  = DateTime.Now;
                question.LastModified = DateTime.Now;
                question.AuthorId     = currentUserId;
                await _questionGateway.Add(question, cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Create([Bind("Title, Abstract,Contents")] Article article)
        {
            if (ModelState.IsValid)
            {
                article.AuthorId    = _requestUserProvider.GetUserId();
                article.CreatedDate = DateTime.Now;
                _articlesRepository.Add(article);
                await _articlesRepository.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View(article));
        }
Beispiel #4
0
        public async Task <ActionResult> AddArticle([FromBody] Article article)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            article.AuthorId    = _requestUserProvider.GetUserId();
            article.CreatedDate = DateTime.Now;
            _articlesRepository.Add(article);
            await _articlesRepository.SaveChanges();

            return(Ok(article));
        }
Beispiel #5
0
        public IActionResult Get()
        {
            try
            {
                var userId = _requestUserProvider.GetUserId();
                _logger.LogInformation("User " + userId + " called method NewsController.Get()");

                var feedCollectionList = _newsServices.GetUserFeedCollections(userId);

                var json = JsonConvert.SerializeObject(feedCollectionList);

                return(Ok(json));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #6
0
 public AnswerQuestionBuilder WithInvalidUserId(string userid)
 {
     _requestUserProvider.GetUserId().Returns(userid);
     return(this);
 }
Beispiel #7
0
 public AskQuestionCommandBuilder WithInvalidUserId(string userId)
 {
     _requestUserProvider.GetUserId().Returns(userId);
     return(this);
 }