Example #1
0
        /// <inheritdoc cref="IRequestHandler{TRequest,TResponse}.Handle" />
        public async Task <IRequestResult <Story> > Handle(GetStoryQuery request, CancellationToken cancellationToken)
        {
            var authenticated = request.User.Identity.IsAuthenticated;
            var queryable     = context.Stories.AsNoTracking();

            if (request.IncludeAuthors)
            {
                if (request.IncludeComments)
                {
                    queryable = queryable
                                .Include(entity => entity.Comments)
                                .ThenInclude(entity => entity.Author);
                }

                queryable = queryable.Include(entity => entity.Author);
            }

            if (request.IncludeComments)
            {
                queryable = queryable.Include(entity => entity.Comments);
            }

            var story = await queryable
                        .Where(entity => (authenticated || entity.IsPublic) && entity.Slug == request.Slug)
                        .FirstOrDefaultAsync(cancellationToken);

            if (null == story)
            {
                return(new RequestResult <Story>());
            }

            return(RequestResult.Success(mapper.Map <Story>(story)));
        }
        public async Task <IActionResult> GetStory(int storyId)
        {
            var getStoryQuery = new GetStoryQuery(storyId);
            var result        = await mediator.Send(getStoryQuery);

            return(StatusCode((int)result.Code, result.Value));
        }
Example #3
0
        public async Task <IActionResult> Get(string slug, [FromCommaSeparatedQuery(Name = "include")] IEnumerable <string> includes)
        {
            var flags = EnumFlags.Parse <StoryIncludes>(includes);
            var query = new GetStoryQuery(User, slug)
            {
                IncludeAuthors  = StoryIncludes.Authors == (flags & StoryIncludes.Authors),
                IncludeComments = StoryIncludes.Comments == (flags & StoryIncludes.Comments)
            };

            var result = await mediator.Send(query, HttpContext.RequestAborted);

            if (false == result.IsSuccess())
            {
                return(NotFound());
            }

            return(Ok(mapper.Map <StoryModel>(result.Data)));
        }
Example #4
0
        public async Task <IActionResult> GetStory([FromRoute] Guid id)
        {
            var command = new GetStoryQuery(id);

            return(Ok(await _Mediator.Send(command)));
        }
Example #5
0
 public void Setup()
 {
     query = new GetStoryQuery(_context, _serviceProvider.GetService <IMapper>());
 }