public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var query = new GetPostDetailsQuery {
                PostId = id.Value
            };
            var post = await _mediator.Send(query);

            if (post == null)
            {
                return(NotFound());
            }

            if (post.Status == PostStatus.Approved || post.Status == PostStatus.Pending)
            {
                return(RedirectToAction(nameof(Index)));
            }

            ViewData["StatusOptions"] = new SelectList(GetPostStatusOptions(), post.Status);
            return(View(post));
        }
        public async Task <ActionResult <PostDto> > Get(int id)
        {
            var query = new GetPostDetailsQuery {
                PostId = id
            };
            var post = await _mediator.Send(query);

            if (post == null)
            {
                return(NotFound());
            }
            return(Ok(post));
        }
Esempio n. 3
0
        public IActionResult ViewPost(Guid Id)
        {
            try
            {
                var CurrentUser = _context.Users.Find(userManager.GetUserId(User));
                if (CurrentUser is null)
                {
                    throw new DomainException(ErrorMessages.NotSignedIn);
                }

                var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id);
                if (existingAccount.AccountStatus.Equals(Status.Suspended))
                {
                    signInManager.SignOutAsync();
                }

                var existingPost = _postStore.ViewPost(Id);

                if (existingPost is null)
                {
                    throw new DomainException(ErrorMessages.PostDoesNotExist);
                }

                var OwnerOfPost = _userStore.GetByIdentityUserId(existingPost.UserId);

                GetPostDetailsQuery GetPostDetailsQuery = new GetPostDetailsQuery(CurrentUser, OwnerOfPost,
                                                                                  existingPost, _userStore, _commentStore, existingAccount);

                return(View(GetPostDetailsQuery.Handle()));
            }
            catch (DomainException ex)
            {
                _logger.LogError(ex.Message);
                if (ex.Message.Equals(ErrorMessages.NotSignedIn))
                {
                    return(RedirectToAction(ActionName.Login, ControllerName.Accounts));
                }

                return(RedirectToAction(ActionName.NotFound, ControllerName.Accounts));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts));
            }
        }
        // GET: Posts/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var query = new GetPostDetailsQuery {
                PostId = id.Value
            };
            var post = await _mediator.Send(query);

            return(post == null?NotFound() : (IActionResult)View(new PostDetailsViewModel
            {
                Post = post,
                NewComment = new CreateCommentDto()
            }));
        }
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var query = new GetPostDetailsQuery {
                PostId = id.Value
            };
            var post = await _mediator.Send(query);

            if (post == null)
            {
                return(NotFound());
            }

            return(View(post));
        }