Esempio n. 1
0
 public async Task <IActionResult> Post(string slug)
 {
     try
     {
         PostDetail post = null;
         if (Guid.TryParse(slug, out Guid id))
         {
             post = _postsRepository.FindById(id);
         }
         else
         {
             post = _postsRepository.GetPostBySlug(slug);
         }
         if (post != null)
         {
             post.Content = _postsRepository.FixContent(post.Content);
             try
             {
                 ///转化Markdown
                 post.Content = await _nodeService.InvokeAsync <string>("./Node/Parser", post.Content);
             }
             catch (Exception ex)
             {
             }
         }
         return(View(post));
     }
     catch
     {
         _logger.LogWarning($"Couldn't find the ${slug} post");
     }
     return(Redirect("/"));
 }
Esempio n. 2
0
        public IActionResult Posts(Guid id)
        {
            var result = _postsRepository.FindById(id);

            if (result == null)
            {
                NotFound();
            }
            return(Ok(result));
        }
Esempio n. 3
0
        public IActionResult Post(Guid id)
        {
            var        cacheKey = $"Root_Post_{id}";
            string     cached;
            PostDetail result = null;

            if (_memoryCache.TryGetValue(cacheKey, out cached))
            {
                try
                {
                    result = JsonConvert.DeserializeObject <PostDetail>(cached);
                }
                catch
                {
                    result = null;
                }
            }
            try
            {
                if (result == null)
                {
                    var post = _repo.FindById(id);
                    if (post != null)
                    {
                        post.Content = _repo.FixContent(post.Content);
                        result       = post;
                        cached       = JsonConvert.SerializeObject(result);
                        _memoryCache.Set(cacheKey, cached, new MemoryCacheEntryOptions()
                        {
                            SlidingExpiration = TimeSpan.FromHours(12)
                        });
                    }
                }
                return(View(result));
            }
            catch
            {
                _logger.LogWarning($"Couldn't find the ${id} post");
            }
            return(Redirect("/"));
        }
Esempio n. 4
0
        public RootQuery(
            IPostsRepository postsRepository,
            IApplicationUsersRepository applicationUserRepository)
        {
            Name = "Query";

            Field <ListGraphType <ApplicationUserType> >(
                "AllApplicationUser",
                resolve: context => applicationUserRepository.GetAll()
                );

            Field <ApplicationUserType>(
                "ApplicationUser",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "email"
            }),
                resolve: context => applicationUserRepository
                .FindByEmail(context.GetArgument <string>("email"))
                );

            Field <ListGraphType <PostType> >(
                "AllPost",
                resolve: context => postsRepository.GetAll()
                );

            Field <PostType>(
                "Post",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "id"
            }),
                resolve: context =>
                postsRepository.FindById(context.GetArgument <string>("id"))
                );
        }