Exemplo n.º 1
0
        public JsonResult Edit(Blog newModel)
        {
            newModel = UrlCommon.DecodeModel(newModel);
            if (string.IsNullOrWhiteSpace(newModel.BlogId))
            {
                return(ResponseResult(-1, "要编辑的文章ID不正确(无法获取)"));
            }
            var         model = blogApplication.FindById(newModel.BlogId);
            IList <Tag> tags  = null;

            if (!string.IsNullOrWhiteSpace(Request["Tags"]))
            {
                //反序列化tag
                tags = JavaScriptCommon.DeSerialize <IList <Tag> >(UrlCommon.Decode(Request["Tags"]));
            }
            else
            {
                return(ResponseResult(1, new { msg = "标签不允许为空" }));
            }
            //TODO 应该先对两个blog进行修改,如果发现是一样的就不修改blog了
            string validateResult = blogApplication.ValidateAndCorrectSubmit(newModel, classifyApplication);

            if (validateResult == null)
            {
                return(SaidCommon.Transaction(() =>
                {
                    blogApplication.EditBlog(newModel, model, tags, tagApplication, blogTagsApplication);
                    if (blogTagsApplication.Commit())
                    {
                        // 清理 cache,因为前台读取的时候引用了 cache
                        if (CacheHelper.GetCache(model.BlogId) != null)
                        {
                            CacheHelper.RemoveAllCache(model.BlogId);
                        }
                        return ResponseResult(new { id = newModel.BlogId });
                    }
                    return ResponseResult(2, "修改Blog失败");
                }));
            }
            else
            {
                return(ResponseResult(1, new { msg = validateResult }));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteTag(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return(ResponseResult(1, "要删除的标签标志不正确"));
            }
            var model = tagApplication.FindById(id);

            if (model == null)
            {
                return(ResponseResult(2, "没有找到要删除的标签信息"));
            }
            try
            {
                return(SaidCommon.Transaction(() =>
                {
                    var blogTags = blogTagsApplication.FindByTagId(model.TagId);
                    if (blogTags != null && blogTags.Count() > 0)
                    {
                        blogTagsApplication.DeleteByBlogTagId(model.TagId);
                        //if (!blogTagsApplication.Commit())
                        //{
                        //    throw new Exception("删除标签失败,删除标签和Blog对应的关系失败");
                        //}
                    }

                    tagApplication.Delete(id);
                    if (tagApplication.Commit())
                    {
                        return ResponseResult();
                    }
                    throw new Exception("删除标签失败,服务器异常");
                }));
            }
            catch (Exception e)
            {
                return(ResponseResult(3, e.Message));
            }
        }
Exemplo n.º 3
0
        public JsonResult AddBlog(Blog model)
        {
            //if (string.IsNullOrWhiteSpace(model.ClassifyId))
            //    return ResponseResult(1, "没有填写分类信息");

            //修正编码数据
            model = UrlCommon.DecodeModel(model);
            IList <Tag> tags = null;

            if (!String.IsNullOrWhiteSpace(Request["Tags"]))
            {
                //反序列化tag
                tags = JavaScriptCommon.DeSerialize <IList <Tag> >(UrlCommon.Decode(Request["Tags"]));
            }
            else
            {
                return(ResponseResult(1, new { msg = "标签不允许为空" }));
            }

            string validateResult = blogApplication.ValidateAndCorrectSubmit(model, classifyApplication);

            if (validateResult == null)
            {
                return(SaidCommon.Transaction(() =>
                {
                    blogApplication.AddBlog(model, tags, blogTagsApplication, tagApplication);
                    if (blogApplication.Commit())
                    {
                        return ResponseResult(new { id = model.BlogId });
                    }
                    return ResponseResult(2);
                }));
            }
            else
            {
                return(ResponseResult(1, new { msg = validateResult }));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 物理删除一篇文章
        /// </summary>
        /// <param name="id">文章id</param>
        /// <returns></returns>
        public JsonResult RealDelete(string id)
        {
            Blog model = blogApplication.FindById(id);

            if (model == null)
            {
                return(ResponseResult(1, "要删除的文章不存在(数据库未检索到该文章ID)"));
            }
            try
            {
                return(SaidCommon.Transaction(() =>
                {
                    var blogTags = blogTagsApplication.FindByBlogId(model.BlogId);
                    if (blogTags != null && blogTags.Count() > 0)
                    {
                        blogTagsApplication.DeleteByBlogId(model.BlogId);
                        //if (!blogTagsApplication.Commit())
                        //{
                        //    throw new Exception("删除文章失败(删除Blog和标签对应的关系失败)");
                        //};
                    }
                    return SaidCommon.Transaction(() =>
                    {
                        blogApplication.DeleteBlog(model, blogTagsApplication);
                        if (blogApplication.Commit())
                        {
                            return ResponseResult();
                        }
                        throw new Exception("从数据库中删除文章失败");
                    });
                }));
            }
            catch (Exception e)
            {
                return(ResponseResult(2, e.Message));
            }
        }
Exemplo n.º 5
0
        public JsonResult DeleteComment(string commentId)
        {
            if (this.AdminId != null && !string.IsNullOrWhiteSpace(commentId))
            {
                try
                {
                    return(SaidCommon.Transaction(() =>
                    {
                        var comment = commentApplication.FindNoCache(commentId);
                        if (comment != null)
                        {
                            //标记删除
                            comment.IsDel = 1;
                            //if (comment.Blog.BComment > 0)
                            //    comment.Blog.BComment--;

                            commentApplication.Update(comment);
                            if (commentApplication.Commit())
                            {
                                /*
                                 *  这个bug仍然没搞定:
                                 *
                                 *  Attaching an entity of type 'Said.Models.Blog' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
                                 *
                                 *  参见这里:
                                 *  http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent
                                 *
                                 *  EF对每个查询有缓存,这里的改动是EF缓存实体里的改动,改动的blog包含在两个实体中(blog和comment.blog),导致了EF上下文不一致,才出现了这个问题
                                 *  这个问题尚未解决
                                 */

                                var blog = blogApplication.FindById(comment.BlogId);
                                if (blog.BComment > 0)
                                {
                                    blog.BComment--;
                                }
                                blogApplication.Update(blog);
                                if (blogApplication.Commit())
                                {
                                    return ResponseResult();
                                }
                                else
                                {
                                    throw new Exception("删除评论失败,修改Blog对象异常");
                                }
                            }
                            else
                            {
                                logManager.Error(new { msg = "删除评论失败", blogId = comment.BlogId, commentId = comment.CommentId });
                                throw new Exception("删除评论失败");
                            }
                        }
                        return ResponseResult(2);
                    }));
                }
                catch (Exception e)
                {
                    logManager.Error(e);
                    return(ResponseResult(1));
                }
            }
            else
            {
                return(ResponseResult(1));
            }
        }
Exemplo n.º 6
0
        public JsonResult Reply(string blogId, string commentId, string replyId, string nickName, string site, string email, string context)
        {
            //TODO 这里要防反复提交,刷评论,DDos攻击之类的
            nickName = UrlCommon.Decode(nickName);
            site     = UrlCommon.Decode(site);
            email    = UrlCommon.Decode(email);
            context  = UrlCommon.Decode(context);
            if (string.IsNullOrWhiteSpace(blogId))
            {
                return(ResponseResult(1, "文章不正确"));
            }
            if (string.IsNullOrWhiteSpace(commentId) && string.IsNullOrWhiteSpace(replyId))
            {
                return(ResponseResult(1, "要回复的评论不正确"));
            }
            //验证输入的文本
            string validateContextResultString = commentApplication.CheckContext(context);

            if (validateContextResultString != null)
            {
                return(ResponseResult(1, validateContextResultString));
            }

            //事务需要对源进行监听,这里从数据库中获取了Blog,需要让事务监听到
            try
            {
                return(SaidCommon.Transaction(() =>
                {
                    //从数据库检索Blog是否存在
                    var blog = blogApplication.FindById(blogId.Trim());
                    if (blog == null)
                    {
                        throw new Exception("用户回复:文章不正确");
                    }
                    //准备数据
                    var inputUser = new User
                    {
                        UserID = this.UserId,
                        Name = nickName,
                        Site = site,
                        EMail = email
                    };
                    Reply toReply = null;
                    Comment comment = null;
                    string toUserEmail = string.Empty;
                    string toUserNickName = string.Empty;

                    if (!string.IsNullOrWhiteSpace(replyId))//如果有针对回复的ID,则以回复ID为准
                    {
                        toReply = replyApplicaiton.Find(replyId);
                        if (toReply == null)
                        {
                            throw new Exception("用户回复:回复的信息不正确");
                        }
                        if (toReply.UserId == this.UserId)
                        {
                            throw new Exception("用户不允许回复自己的评论");
                        }
                        toUserEmail = toReply.User.EMail;
                        toUserNickName = toReply.User.Name;
                    }
                    else  //否则以评论ID为准
                    {
                        comment = commentApplication.Find(commentId);
                        if (comment == null)
                        {
                            throw new Exception("用户回复:回复的评论不正确");
                        }
                        if (comment.UserId == this.UserId)
                        {
                            throw new Exception("用户不允许回复自己的评论");
                        }
                        toUserEmail = comment.User.EMail;
                        toUserNickName = comment.User.Name;
                    }
                    User user = null;
                    string validateUserResultString = userApplication.CheckAndTrimInput(inputUser, out user);
                    if (validateUserResultString != null)
                    {
                        return ResponseResult(8, validateUserResultString);
                    }
                    blog.BComment++;
                    blogApplication.Update(blog);
                    //这里拿到的user是已经修剪处理好的user了
                    userApplication.Update(user);

                    Reply reply = new Reply
                    {
                        BlogId = blog.BlogId,
                        ReplyId = SaidCommon.GUID,
                        CommentId = toReply == null ? comment.CommentId : toReply.CommentId,
                        Context = context,
                        SourceContext = context,
                        Date = DateTime.Now,
                        UserId = user.UserID,
                        ReplyType = toReply == null ? 0 : 1,
                        ToReplyId = toReply == null ? null : toReply.ReplyId
                    };
                    replyApplicaiton.Add(reply);
                    if (!replyApplicaiton.Commit())
                    {
                        throw new Exception("用户回复:添加回复对象失败");
                    }
                    // 发送邮件
                    EmailCommon.SendReplyEmailAsync(toUserEmail, string.Format("Said - 您在文章《{0}》的评论中收到新的回复", blog.BTitle), context, toUserNickName, string.Format("{2}://{0}/blog/{1}.html?sgs=email-more#comment", Request.Url.Authority, blog.BlogId, Request.Url.Scheme), blog.BTitle, string.Format("{2}://{0}/blog/{1}.html?sgs=email-more#comment", Request.Url.Authority, blog.BlogId, Request.Url.Scheme));
                    return ResponseResult(new
                    {
                        king = this.AdminId != null,
                        cid = reply == null ? comment.CommentId : reply.CommentId,
                        rid = reply == null ? string.Empty : reply.ReplyId
                    });
                }));
            }
            catch (Exception e)
            {
                logManager.Error("用户评论失败", e);
                return(ResponseResult(3, "评论失败"));
            }
        }
Exemplo n.º 7
0
        public JsonResult Comment(string blogId, string nickName, string site, string email, string context)
        {
            //TODO 这里要防反复提交,刷评论,DDos攻击之类的
            nickName = UrlCommon.Decode(nickName);
            site     = UrlCommon.Decode(site);
            email    = UrlCommon.Decode(email);
            context  = UrlCommon.Decode(context);
            if (string.IsNullOrWhiteSpace(blogId))
            {
                return(ResponseResult(1, "用户评论:文章不正确"));
            }
            //验证输入的文本
            string validateContextResultString = commentApplication.CheckContext(context);

            if (validateContextResultString != null)
            {
                return(ResponseResult(1, validateContextResultString));
            }

            //事务需要对源进行监听,这里从数据库中获取了Blog,需要让事务监听到
            try
            {
                return(SaidCommon.Transaction(() =>
                {
                    //从数据库检索Blog是否存在
                    var blog = blogApplication.FindById(blogId.Trim());
                    if (blog == null)
                    {
                        throw new Exception("用户评论:文章不正确");
                    }
                    //准备数据
                    var inputUser = new User
                    {
                        UserID = this.UserId,
                        Name = nickName,
                        Site = site,
                        EMail = email,
                        //有可能当前用户本来是普通用户,但是管理员新开了页面登录了后台,这样角色的身份就不一样了,这里需要同步把用户角色,并且把用户key同步过去
                        Rule = this.AdminId != null ? 1 : 0,
                        SecretKey = this.AdminId
                    };
                    User user = null;
                    string validateUserResultString = userApplication.CheckAndTrimInput(inputUser, out user);
                    if (validateUserResultString != null)
                    {
                        return ResponseResult(8, validateUserResultString);
                    }
                    blog.BComment++;
                    blogApplication.Update(blog);
                    //这里拿到的user是已经修剪处理好的user了
                    userApplication.Update(user);
                    Comment comment = new Comment
                    {
                        BlogId = blog.BlogId,
                        CommentId = SaidCommon.GUID,
                        Date = DateTime.Now,
                        SourceContext = context,
                        Context = context,
                        UserId = user.UserID
                    };
                    commentApplication.Add(comment);
                    if (!commentApplication.Commit())
                    {
                        throw new Exception("用户评论:评论失败");
                    }
                    // 发送邮件
                    EmailCommon.SendReplyEmailAsync("*****@*****.**", string.Format("Said - 用户评论了文章《{0}》", blog.BTitle), context, "linkFly", string.Format("{2}://{0}/blog/{1}.html?sgs=email-more#comment", Request.Url.Authority, blog.BlogId, Request.Url.Scheme), blog.BTitle, string.Format("{2}://{0}/blog/{1}.html?sgs=email-more#comment", Request.Url.Authority, blog.BlogId, Request.Url.Scheme));
                    return ResponseResult(new { king = this.AdminId != null, id = comment.CommentId });
                }));
            }
            catch (Exception e)
            {
                logManager.Error(e);
                return(ResponseResult(1, "评论失败"));
            }
        }