Exemplo n.º 1
0
        public async Task <ActionResult> DeleteArticle(Guid?Id)
        {
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    return(Json(new { status = "fail", result = message }, JsonRequestBehavior.AllowGet));//返回错误信息
                }
            }
            string userId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null || userId.Trim() == "")                                                            //用户未登录
            {
                return(Json(new { status = "fail", result = "获取不到用户信息,请检查登陆状态" }, JsonRequestBehavior.AllowGet)); //返回错误信息
            }
            IArticleManager articleManager = new ArticleManager();

            if (Id == null || !await articleManager.ExistsArticle(Id.Value))         //文章id找不到则跳转文章不存在错误页面
            {
                return(Json(new { status = "fail", result = "未能找到对应ID的文章,请稍后再试" })); //返回错误信息
            }//要经过上面的判断否则会出错
            var data = await articleManager.GetOneArticleById(Id.Value);

            if (data.userId != Guid.Parse(userId))                             //文章作者才可删除文章
            {
                return(Json(new { status = "fail", result = "非本人文章不可进行删除" })); //返回错误信息
            }
            await articleManager.RemoveArticle(Id.Value);

            return(Json(new { status = "ok", result = "删除成功!" }));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> GetArticleDetails(Guid?id)
        {
            var articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))
            {
                return(Json(new { result = "未能找到对应ID的文章,请稍后再试", status = "fail" }, JsonRequestBehavior.AllowGet));//返回错误信息
            }
            ArticleDto articleData = await articleManager.GetOneArticleById(id.Value);

            var userManager             = new UserManager();
            UserInformationDto userInfo = await userManager.GetUserById(articleData.userId);

            //查询是否已经关注
            string userid = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userid, out message);
            }
            string userId        = Session["userId"] == null ? userid : Session["userId"].ToString();
            var    articlesCount = await articleManager.GetArticleDataCount(userInfo.Id);                            //查找文章总数

            var categoriesCount = await articleManager.GetCategoryDataCount(userInfo.Id);                            //查找分类总数

            var isCurrentUser = userId.Trim() == "" ? false : userInfo.Id == Guid.Parse(userId) ? true : false;      //是否为当前登陆用户
            var isFocused     = userId == "" ? false : await userManager.IsFocused(Guid.Parse(userId), userInfo.Id); //id为空也视为没关注

            var tenTags = await articleManager.GetCategoriesByCount(userInfo.Id, 10);                                //返回10个分类

            return(Json(new { status = "ok", articleData, userInfo, articlesCount, categoriesCount, isCurrentUser, isFocused, tenTags }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> ArticleDetails(Guid?id)
        {
            var articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))
            {
                ErrorController.message = "未能找到对应ID的文章,请稍后再试";
                return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
            }
            ArticleDto data = await articleManager.GetOneArticleById(id.Value);

            var userManager         = new UserManager();
            UserInformationDto user = await userManager.GetUserById(data.userId);

            ViewBag.User          = user;
            ViewBag.ArticlesCount = await articleManager.GetArticleDataCount(user.Id);    //查找文章总数

            ViewBag.CategoriesCount = await articleManager.GetCategoryDataCount(user.Id); //查找分类总数

            //查询是否已经关注
            string userid = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userid, out message);
            }
            string userId = Session["userId"] == null ? userid : Session["userId"].ToString();

            ViewBag.IsCurrentUser = userId.Trim() == "" ? false : user.Id == Guid.Parse(userId) ? true : false;      //是否为当前登陆用户
            ViewBag.IsFocused     = userId == "" ? false : await userManager.IsFocused(Guid.Parse(userId), user.Id); //id为空也视为没关注

            ViewBag.TenTags = await articleManager.GetCategoriesByCount(user.Id, 10);                                //返回10个分类

            return(View(data));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> EditArticle(Guid?articleId, string title, string content, string introContent, Guid[] categoryIds)
        {
            //id和内容和标题为空、未登录、登陆id不是这篇文章的拥有者、分类id不属于自己
            if (articleId == null || articleId == Guid.Empty || title == null || content == null || title.Trim() == "" || content.Trim() == "") //提交的信息为空
            {
                return(Json(new { status = "fail", result = "提交的信息不完整,请重试" }, JsonRequestBehavior.AllowGet));                                   //返回错误信息
            }
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    return(Json(new { status = "fail", result = message }, JsonRequestBehavior.AllowGet));//返回错误信息
                }
            }
            string userId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null || userId.Trim() == "")                                                            //用户未登录
            {
                return(Json(new { status = "fail", result = "获取不到用户信息,请检查登陆状态" }, JsonRequestBehavior.AllowGet)); //返回错误信息
            }
            //查找提交的文章id是否存在,是否属于当前登陆用户
            IArticleManager articleManager = new ArticleManager();

            if (!await articleManager.ExistsArticle(articleId.Value))                                              //文章id找不到则跳转文章不存在错误页面
            {
                return(Json(new { status = "fail", result = "未能找到对应ID的文章,请稍后再试" }, JsonRequestBehavior.AllowGet)); //返回错误信息
            }
            var data = await articleManager.GetOneArticleById(articleId.Value);                                    //要经过上面的判断否则会出错

            if (data.userId != Guid.Parse(userId))                                                                 //文章作者才可编辑文章
            {
                return(Json(new { status = "fail", result = "非本人文章不可进行编辑" }, JsonRequestBehavior.AllowGet));       //返回错误信息
            }
            //提交的分类信息不为空,循环自己所有的分类,对比是否正确
            if (categoryIds != null)
            {
                List <BlogCategoryDto> categoryDtoes = await articleManager.GetAllCategories(Guid.Parse(userId)); //获取分类对象集合

                List <Guid> currentUserCategoryIds = new List <Guid>();                                           //将分类对象中的分类id整合进一个集合中
                foreach (BlogCategoryDto category in categoryDtoes)
                {
                    currentUserCategoryIds.Add(category.Id);
                }
                for (int i = 0; i < categoryIds.Length; i++)              //循环检查提交的分类id是否和自身的分类id有对应
                {
                    if (!currentUserCategoryIds.Contains(categoryIds[i])) //如果提交的分类id与自身的分类id没有匹配项,提示错误
                    {
                        return(Json(new { status = "fail", result = "提交的分类与用户所拥有的分类不匹配,请重试!" }, JsonRequestBehavior.AllowGet));
                    }
                }
            }
            await articleManager.EditArticle(articleId.Value, title, content, introContent, categoryIds);

            return(Json(new { status = "ok", result = "提交成功!", articleId = articleId.Value }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> ArticleDetails(Guid?id)
        {
            ArticleManager articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))
            {
                return(RedirectToAction(nameof(ArticleList)));
            }
            ViewBag.Comments = await articleManager.GetCommentByArticleId(id.Value);

            return(View(await articleManager.GetOneArticleById(id.Value)));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> GetComments(Guid?id, int pageIndex, int pageSize)
        {
            var articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))
            {
                return(Json(new { status = "fail", result = "未能找到对应文章ID的评论,请稍后再试" }, JsonRequestBehavior.AllowGet));
            }
            //查找所有评论
            List <CommentDto> data = await articleManager.GetCommentByArticleId(id.Value, pageIndex - 1, pageSize);

            int commentCount = await articleManager.GetCommentCountByArticleId(id.Value);

            int pageCount = commentCount % pageSize == 0 ? commentCount / pageSize : commentCount / pageSize + 1;//总页数

            return(Json(new { status = "ok", pageCurrentIndex = pageIndex, pageSize, pageCount, pageMatchCommentCount = commentCount, data }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 7
0
        public async Task <ActionResult> EditArticle(Guid?id)
        {
            //未登录、文章id错误、id为空不可进入
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    ErrorController.message = message;
                    return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
                }
            }
            string userId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null || userId.Trim() == "")//用户未登录
            {
                ErrorController.message = "获取不到用户信息,请检查登陆状态";
                return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
            }

            //获取当前文章实体
            IArticleManager articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))//文章id找不到则跳转文章不存在错误页面
            {
                ErrorController.message = "未能找到对应ID的文章,请稍后再试";
                return(RedirectToAction("IllegalOperationError", "Error")); //返回错误页面
            }
            var data = await articleManager.GetOneArticleById(id.Value);    //要经过上面的判断否则会出错

            if (data.userId != Guid.Parse(userId))                          //文章作者才可编辑文章
            {
                ErrorController.message = "非本人文章不可进行编辑";
                return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
            }
            //将实体内容放进viewmodel中输出到前端
            ViewBag.UserId  = userId;
            ViewBag.Article = data;
            return(View());
        }
Exemplo n.º 8
0
        public async Task <ActionResult> GetEditArticle(Guid?id)
        {
            //未登录、文章id错误、id为空不可进入
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    return(Json(new { result = "未能找到对应ID的文章,请稍后再试", status = "fail" }, JsonRequestBehavior.AllowGet));//返回错误信息
                }
            }
            string userId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId == null || userId.Trim() == "")                                                            //用户未登录
            {
                return(Json(new { result = "获取不到用户信息,请检查登陆状态", status = "fail" }, JsonRequestBehavior.AllowGet)); //返回错误信息
            }

            //获取当前文章实体
            IArticleManager articleManager = new ArticleManager();

            if (id == null || !await articleManager.ExistsArticle(id.Value))                                       //文章id找不到则跳转文章不存在错误页面
            {
                return(Json(new { result = "未能找到对应ID的文章,请稍后再试", status = "fail" }, JsonRequestBehavior.AllowGet)); //返回错误信息
            }
            var article = await articleManager.GetOneArticleById(id.Value);                                        //要经过上面的判断否则会出错

            if (article.userId != Guid.Parse(userId))                                                              //文章作者才可编辑文章
            {
                ErrorController.message = "非本人文章不可进行编辑";
                return(RedirectToAction("IllegalOperationError", "Error"));//返回错误页面
            }
            return(Json(new { status = "ok", userId, article }, JsonRequestBehavior.AllowGet));
        }