/// <summary> /// Invoke view component for adding comment. /// </summary> /// <param name="id">Post identifier.</param> /// <returns>Page with full post.</returns> public async Task <IViewComponentResult> InvokeAsync(int id) { var userName = HttpContext.User.Identity.Name; // Anauthorized user. if (userName == null) { return(Content(string.Empty)); } var userId = await _identityService.GetUserIdByNameAsync(userName); var authorQuery = new GetAuthorByUserIdQuery { UserId = userId }; var author = await _mediator.Send(authorQuery); var model = new CommentViewModel { PostId = id, AuthorId = author.Id, Author = author.FirstName + " " + author.LastName, }; return(View("AddComment", model)); }
public async Task Handle_GivenValidUserId_ReturnsAuthorDTO() { // Arrange var author = new AuthorDTO { Id = 1, UserId = "QWERTY1234567890_One", FirstName = "FirstName_One", LastName = "LastName_One", BirthDate = new DateTime(1988, 01, 01), }; var query = new GetAuthorByUserIdQuery { UserId = "QWERTY1234567890_One" }; // Act var handler = new GetAuthorByUserIdQuery.GetAuthorByUserIdQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeOfType <AuthorDTO>(); result.ShouldNotBeNull(); result.Id.ShouldBe(author.Id); result.UserId.ShouldBe(author.UserId); result.FirstName.ShouldBe(author.FirstName); result.LastName.ShouldBe(author.LastName); result.BirthDate.ShouldBe(author.BirthDate); }
public async Task Handle_GivenInvalidUserId_ReturnsNull() { // Arrange var query = new GetAuthorByUserIdQuery { UserId = "InvalidUserId" }; // Act var handler = new GetAuthorByUserIdQuery.GetAuthorByUserIdQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeNull(); }
/// <summary> /// Show page to edit user's info. /// </summary> /// <param name="id">User identifier.</param> /// <returns>View with EditUserViewModel.</returns> public async Task <IActionResult> Edit(string id) { var authorQuery = new GetAuthorByUserIdQuery { UserId = id }; var authorDTO = await _mediator.Send(authorQuery); if (authorDTO == null) { return(NotFound()); } var email = await _identityService.GetEmailByIdAsync(authorDTO.UserId); authorDTO.Email = email; var model = _mapper.Map <AuthorDTO, AuthorViewModel>(authorDTO); return(View(model)); }
/// <summary> /// Invoke login panel. /// </summary> /// <returns>Login view component.</returns> public async Task <IViewComponentResult> InvokeAsync() { var userName = HttpContext.User.Identity.Name; if (userName == null) { AuthorViewModel emptyModel = null; return(View("Login", emptyModel)); } var userId = await _identityService.GetUserIdByNameAsync(userName); var authorQuery = new GetAuthorByUserIdQuery { UserId = userId }; var authorDTO = await _mediator.Send(authorQuery); var model = _mapper.Map <AuthorDTO, AuthorViewModel>(authorDTO); return(View("Login", model)); }
public async Task <IActionResult> Create(CreatePostViewModel model) { if (ModelState.IsValid) { // Add new topic. var topicDTO = new TopicDTO { Text = model.Topic }; var topicCommand = new CreateTopicCommand { Model = topicDTO }; int topicId; try { topicId = await _mediator.Send(topicCommand); } catch (RequestValidationException failures) { foreach (var error in failures.Failures) { ModelState.AddModelError(string.Empty, error.Value[0]); } return(View(model)); } // Get current author. var userId = await _identityService.GetUserIdByNameAsync(HttpContext.User.Identity.Name); var authorQuery = new GetAuthorByUserIdQuery { UserId = userId }; var author = await _mediator.Send(authorQuery); // Create new post. var postDTO = new PostDTO { Title = model.Title, Text = model.Text, AuthorId = author.Id, TopicId = topicId, Date = DateTime.Now, }; var postCommand = new CreatePostCommand { Model = postDTO }; int postId; try { postId = await _mediator.Send(postCommand); } catch (RequestValidationException failures) { foreach (var error in failures.Failures) { ModelState.AddModelError(string.Empty, error.Value[0]); } return(View(model)); } return(RedirectToAction("Read", "Posts", new { id = postId })); } return(View(model)); }