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; } }
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; } }
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)); }
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)); }
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)); } }
public AnswerQuestionBuilder WithInvalidUserId(string userid) { _requestUserProvider.GetUserId().Returns(userid); return(this); }
public AskQuestionCommandBuilder WithInvalidUserId(string userId) { _requestUserProvider.GetUserId().Returns(userId); return(this); }