Beispiel #1
0
        public ProcessResult <Post> UpsertPost(Post post)
        {
            try
            {
                using var store = new BlogDataStore(_settings);
                store.Posts.DeleteMany(x => x.Key == post.Key);
                store.Posts.DeleteMany(x => x.Title == post.Title);
                store.Posts.DeleteMany(x => x.UrlFriendlyTitle == post.UrlFriendlyTitle);

                store.PostInfoEntries.DeleteMany(x => x.Key == post.Key);
                store.PostInfoEntries.DeleteMany(x => x.Title == post.Title);
                store.PostInfoEntries.DeleteMany(x => x.UrlFriendlyTitle == post.UrlFriendlyTitle);

                store.Tags.DeleteMany(x => x.PostKey == post.Key);

                post.Key = Guid.NewGuid();
                store.Tags.InsertBulk(post.AsTagEntities());
                store.PostInfoEntries.Insert(post.AsPostInfoEntry());
                store.Posts.Insert(post);

                return(new ProcessResult <Post>().AsOk(data: post));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Beispiel #2
0
        public UserModel GetUserByUsername(string username)
        {
            if (username.Equals(UserModelBase.Admin, StringComparison.InvariantCultureIgnoreCase))
            {
                return new UserModel
                       {
                           MobilePhone    = UserModelBase.Admin,
                           HashedPassword = _settings.AdminHashedPassword,
                           IsAdmin        = true
                       }
            }
            ;

            try
            {
                username = username.ToLower();

                using var store = new BlogDataStore(_settings);
                var user = store.Users
                           .FindOne(x => x.MobilePhone.ToLower() == username.ToLower());
                return(user);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(null);
            }
        }
Beispiel #3
0
        public ICollection <PostInfoEntry> ListPostInfoEntriesByKeywordsInTitle(IEnumerable <string> keywords,
                                                                                bool includeUnpublishedPosts = false)
        {
            try
            {
                if (keywords == null || !keywords.Any())
                {
                    return(new List <PostInfoEntry>());
                }

                var keywordList = keywords.ToList();

                using var store = new BlogDataStore(_settings);

                var expression = keywordList.ContainedBy(Title).IncludeUnpublishedPosts(includeUnpublishedPosts);

                var entries = store.PostInfoEntries.Find(expression,
                                                         limit: _settings.PostsFromSearchCount)
                              .OrderByDescending(x => x.TimeCreated)
                              .ToList();

                return(entries);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(new List <PostInfoEntry>());
            }
        }
Beispiel #4
0
 public ProcessResult <Post> GetPostByUrlFriendlyTitle(string urlFriendlyTitle)
 {
     try
     {
         using var store = new BlogDataStore(_settings);
         var post = GetPostByUrlFriendlyTitle(store, urlFriendlyTitle);
         return(new ProcessResult <Post>().AsOk(post));
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
Beispiel #5
0
 public ProcessResult <Post> GetPostByKey(Guid postKey)
 {
     try
     {
         using var store = new BlogDataStore(_settings);
         var post = GetPostByKey(store, postKey);
         return(new ProcessResult <Post>().AsOk(post));
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
Beispiel #6
0
 public ProcessResult <Post> DeletePost(Guid postKey)
 {
     try
     {
         using var store = new BlogDataStore(_settings);
         store.Posts.Delete(postKey);
         store.Tags.DeleteMany(x => x.PostKey == postKey);
         return(new ProcessResult <Post>().AsOk());
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
Beispiel #7
0
 public ICollection <string> ListAllTags()
 {
     try
     {
         using var store = new BlogDataStore(_settings);
         var tags = store.Tags.FindAll().Select(x => x.Text).Distinct().ToList();
         return(tags);
     }
     catch (Exception e)
     {
         _logger.LogError(e, e.Message);
         return(new List <string>());
     }
 }
Beispiel #8
0
        public ICollection <PostInfoEntry> ListPostInfoEntriesByTags(IEnumerable <string> keywords,
                                                                     bool includeUnpublishedPosts = false)
        {
            try
            {
                var keywordList = keywords.ToList();

                using var store = new BlogDataStore(_settings);
                var expression = keywordList.ContainedBy(Text);

                var postKeys = store.Tags.Find(expression)
                               .Select(x => x.PostKey)
                               .Distinct()
                               .ToList();
                if (postKeys.Count == 0)
                {
                    return(new List <PostInfoEntry>());
                }

                var firstKey = postKeys.First();
                expression = postKeys.Count == 1
                        ? Query.EQ(Id, firstKey)
                        : Query.In(Id, postKeys.Select(x => new BsonValue(x)));

                expression = expression.IncludeUnpublishedPosts(includeUnpublishedPosts);

                var entries = store.PostInfoEntries
                              .Find(expression, limit: 1000)
                              .OrderByDescending(x => x.TimeCreated)
                              .ToList();

                return(entries);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(new List <PostInfoEntry>());
            }
        }
Beispiel #9
0
        public ICollection <Post> ListStickyPosts(int?postCount = null, bool includeUnpublishedPosts = false)
        {
            try
            {
                postCount ??= _settings.LatestPostsCount;
                using var store = new BlogDataStore(_settings);

                var query = includeUnpublishedPosts
                    ? store.Posts.Query().Where(x => x.IsSticky)
                    : store.Posts.Query().Where(x => x.IsSticky && x.IsPublished);

                var posts = query
                            .OrderByDescending(x => x.TimeCreated)
                            .Limit(postCount.Value)
                            .ToList();
                return(posts);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(new List <Post>());
            }
        }
Beispiel #10
0
 private static Post GetPostByUrlFriendlyTitle(BlogDataStore store, string urlFriendlyTitle)
 {
     return(store.Posts.FindOne(x => x.UrlFriendlyTitle == urlFriendlyTitle));
 }
Beispiel #11
0
        private static Post GetPostByKey(BlogDataStore store, Guid postKey)
        {
            var post = store.Posts.FindById(postKey);

            return(post);
        }