示例#1
0
        public string GenerateHash(string tick)
        {
            var nonhash = string.Empty;

            if (tick.IsNullOrWhitespace())
            {
                return(nonhash);
            }

            var spamHash = _db.SelectKey <SpamHash>(DBTableNames.SpamHashes, tick);

            if (spamHash == null || spamHash.Pass || !spamHash.Hash.IsNullOrWhitespace())
            {
                return(nonhash);
            }

            spamHash.Hash = new Random().NextDouble().ToString();
            _db.Update(DBTableNames.SpamHashes, spamHash);

            return(spamHash.Hash);
        }
示例#2
0
        public CommandResult Execute(NewPostCommand command)
        {
            var markdown = new MarkdownSharp.Markdown();
            //TODO:应该验证TitleSlug是否唯一
            var post = new BlogPost
            {
                Id                = ObjectId.NewObjectId(),
                AuthorEmail       = command.Author.Email,
                AuthorDisplayName = command.Author.DisplayName,
                MarkDown          = command.MarkDown,
                Content           = markdown.Transform(command.MarkDown),
                PubDate           = command.PubDate.CloneToUtc(),
                Status            = command.Published ? PublishStatus.Published : PublishStatus.Draft,
                Title             = command.Title,
                TitleSlug         = command.TitleSlug.IsNullOrWhitespace() ? command.Title.Trim().ToSlug() : command.TitleSlug.Trim().ToSlug(),
                DateUTC           = DateTime.UtcNow
            };

            if (!command.Tags.IsNullOrWhitespace())
            {
                var tags = command.Tags.Trim().Split(',').Select(s => s.Trim());
                post.Tags = tags.Select(s => s.ToSlug()).ToArray();
                foreach (var tag in tags)
                {
                    var slug     = tag.ToSlug();
                    var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug);
                    if (tagEntry == null)
                    {
                        tagEntry = new Tag
                        {
                            Slug      = slug,
                            Name      = tag,
                            PostCount = 1
                        };
                        _db.Insert(DBTableNames.Tags, tagEntry);
                    }
                    else
                    {
                        tagEntry.PostCount++;
                        _db.Update(DBTableNames.Tags, tagEntry);
                    }
                }
            }
            else
            {
                post.Tags = new string[] { }
            };

            var result = _db.Insert(DBTableNames.BlogPosts, post);

            return(CommandResult.SuccessResult);
        }
    }
示例#3
0
        public CommandResult Execute(ChangePasswordCommand command)
        {
            var author = _db.SelectKey <Author>(DBTableNames.Authors, command.AuthorId);

            if (Hasher.GetMd5Hash(command.OldPassword) != author.HashedPassword)
            {
                return(new CommandResult("旧密码不正确!"));
            }

            author.HashedPassword = Hasher.GetMd5Hash(command.NewPassword);
            _db.Update(DBTableNames.Authors, author);
            return(CommandResult.SuccessResult);
        }
示例#4
0
        public CommandResult Execute(ChangeProfileCommand command)
        {
            var author = _db.SelectKey <Author>(DBTableNames.Authors, command.AuthorId);

            if (author == null)
            {
                return(new CommandResult("用户信息不存在"));
            }
            author.DisplayName = command.NewDisplayName;
            author.Email       = command.NewEmail;

            _db.Update(DBTableNames.Authors, author);
            return(CommandResult.SuccessResult);
        }
示例#5
0
        /// <summary>
        /// 保存一条数据
        /// </summary>
        public void SaveData <T>(T dbObj, object keyValue) where T : class
        {
            Type   type      = typeof(T);
            string tableName = type.Name;
            string keyName   = _keyMap[type];

            if (_autoBox.SelectCount(string.Format("from {0} where {1} == ?", tableName, keyName), keyValue) <= 0)
            {
                _autoBox.Insert(tableName, dbObj);
            }
            else
            {
                _autoBox.Update(tableName, dbObj);
            }
        }
示例#6
0
        public BlogPostDetailsViewModel Project(BlogPostDetailsBindingModel input)
        {
            var post = _db.Select <BlogPost>("from " + DBTableNames.BlogPosts + " where TitleSlug==?", input.Permalink).FirstOrDefault();

            if (post == null)
            {
                return(null);
            }
            post.ViewCount++;
            _db.Update(DBTableNames.BlogPosts, post);

            var comments = _db.Select <BlogComment>("from " + DBTableNames.BlogComments + " where PostId ==?", post.Id)
                           .OrderBy(o => o.CreatedTime)
                           .ToArray();

            return(new BlogPostDetailsViewModel
            {
                BlogPost = post,
                Comments = comments
            });
        }
示例#7
0
        public CommandResult Execute(EditPostCommand command)
        {
            var post = _db.SelectKey <BlogPost>(DBTableNames.BlogPosts, command.PostId);

            if (post == null)
            {
                throw new ApplicationException("Post with id: {0} was not found".FormatWith(command.PostId));
            }
            if (post.Tags != null)
            {
                foreach (var tag in post.Tags)
                {
                    var slug     = tag.ToSlug();
                    var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug);
                    if (tagEntry != null)
                    {
                        tagEntry.PostCount--;
                        _db.Update(DBTableNames.Tags, tagEntry);
                    }
                }
            }

            var markdown = new MarkdownSharp.Markdown();

            //TODO:应该验证TitleSlug是否是除了本文外唯一的

            post.MarkDown  = command.MarkDown;
            post.Content   = markdown.Transform(command.MarkDown);
            post.PubDate   = command.PubDate.CloneToUtc();
            post.Status    = command.Published ? PublishStatus.Published : PublishStatus.Draft;
            post.Title     = command.Title;
            post.TitleSlug = command.TitleSlug.Trim().ToSlug();
            if (!command.Tags.IsNullOrWhitespace())
            {
                var tags = command.Tags.Trim().Split(',').Select(s => s.Trim());
                post.Tags = tags.Select(s => s.ToSlug()).ToArray();
                foreach (var tag in tags)
                {
                    var slug     = tag.ToSlug();
                    var tagEntry = _db.SelectKey <Tag>(DBTableNames.Tags, slug);
                    if (tagEntry == null)
                    {
                        tagEntry = new Tag
                        {
                            Slug      = slug,
                            Name      = tag,
                            PostCount = 1
                        };
                        _db.Insert(DBTableNames.Tags, tagEntry);
                    }
                    else
                    {
                        tagEntry.PostCount++;
                        _db.Update(DBTableNames.Tags, tagEntry);
                    }
                }
            }
            else
            {
                post.Tags = new string[] { }
            };
            _db.Update(DBTableNames.BlogPosts, post);

            return(CommandResult.SuccessResult);
        }
    }