Browse Source

重构部分代码

懒得勤快 5 years ago
parent
commit
da2faebb38

+ 1 - 1
src/Masuit.MyBlogs.Core.sln

@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.LuceneEFCore.SearchE
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.Core", "..\..\Masuit.Tools\Masuit.Tools.Core\Masuit.Tools.Core.csproj", "{D0D32A1B-13C8-49E6-AC5A-112AD981FD1A}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.MyBlogs.Core", "Masuit.MyBlogs.Core\Masuit.MyBlogs.Core.csproj", "{2F8270E4-5E57-4CE4-AB5F-8008F9FC8C7C}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.MyBlogs.Core", "Masuit.MyBlogs.Core\Masuit.MyBlogs.Core.csproj", "{2F8270E4-5E57-4CE4-AB5F-8008F9FC8C7C}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 9 - 15
src/Masuit.MyBlogs.Core/Controllers/AdvertisementController.cs

@@ -28,7 +28,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [HttpGet("{id:int}")]
         public async Task<IActionResult> Redirect(int id)
         {
-            var ad = AdsService.GetById(id) ?? throw new NotFoundException("广告不存在");
+            var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("推广链接不存在");
             if (!HttpContext.Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get<string>("ads" + id)))
             {
                 HttpContext.Session.Set("ads" + id, id.ToString());
@@ -40,7 +40,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         }
 
         /// <summary>
-        /// 获取文章分页
+        /// 获取分页
         /// </summary>
         /// <returns></returns>
         [MyAuthorize]
@@ -68,7 +68,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         public async Task<IActionResult> Save(AdvertisementDto model)
         {
             model.CategoryId = model.CategoryId?.Replace("null", "");
-            var entity = AdsService.GetById(model.Id);
+            var entity = await AdsService.GetByIdAsync(model.Id);
             if (entity != null)
             {
                 Mapper.Map(model, entity);
@@ -76,7 +76,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 return ResultData(null, b1, b1 ? "修改成功" : "修改失败");
             }
 
-            bool b = AdsService.AddEntitySaved(model.Mapper<Advertisement>()) != null;
+            bool b = await AdsService.AddEntitySavedAsync(model.Mapper<Advertisement>()) > 0;
             return ResultData(null, b, b ? "添加成功" : "添加失败");
         }
 
@@ -86,9 +86,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="id"></param>
         /// <returns></returns>
         [HttpPost("{id}"), HttpGet("{id}"), MyAuthorize]
-        public IActionResult Delete(int id)
+        public async Task<IActionResult> Delete(int id)
         {
-            bool b = AdsService.DeleteByIdSaved(id);
+            bool b = await AdsService.DeleteByIdSavedAsync(id) > 0;
             return ResultData(null, b, b ? "删除成功" : "删除失败");
         }
 
@@ -101,15 +101,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize, HttpPost("{id}")]
         public ActionResult ChangeState(int id)
         {
-            var ad = AdsService.GetById(id);
-            if (ad != null)
-            {
-                ad.Status = ad.Status == Status.Available ? Status.Unavailable : Status.Available;
-                return ResultData(null, AdsService.SaveChanges() > 0, ad.Status == Status.Available ? $"【{ad.Title}】已上架!" : $"【{ad.Title}】已下架!");
-            }
-
-            return ResultData(null, false, "广告不存在");
+            var ad = AdsService.GetById(id) ?? throw new NotFoundException("广告不存在!");
+            ad.Status = ad.Status == Status.Available ? Status.Unavailable : Status.Available;
+            return ResultData(null, AdsService.SaveChanges() > 0, ad.Status == Status.Available ? $"【{ad.Title}】已上架!" : $"【{ad.Title}】已下架!");
         }
-
     }
 }

+ 10 - 8
src/Masuit.MyBlogs.Core/Controllers/CategoryController.cs

@@ -1,11 +1,13 @@
 using EFSecondLevelCache.Core;
 using Masuit.MyBlogs.Core.Common;
+using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
 using Microsoft.AspNetCore.Mvc;
 using System.Linq;
+using System.Threading.Tasks;
 
 namespace Masuit.MyBlogs.Core.Controllers
 {
@@ -36,9 +38,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="id"></param>
         /// <returns></returns>
         [ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" })]
-        public ActionResult Get(int id)
+        public async Task<ActionResult> Get(int id)
         {
-            var model = CategoryService.GetById(id);
+            var model = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类不存在!");
             return ResultData(model.Mapper<CategoryOutputDto>());
         }
 
@@ -47,15 +49,15 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="model"></param>
         /// <returns></returns>
-        public ActionResult Add(Category model)
+        public async Task<ActionResult> Add(Category model)
         {
             bool exist = CategoryService.Any(c => c.Name.Equals(model.Name));
             if (exist)
             {
                 return ResultData(null, false, $"分类{model.Name}已经存在!");
             }
-            var cat = CategoryService.AddEntitySaved(model);
-            if (cat != null)
+            var b = await CategoryService.AddEntitySavedAsync(model) > 0;
+            if (b)
             {
                 return ResultData(null, true, "分类添加成功!");
             }
@@ -68,12 +70,12 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="dto"></param>
         /// <returns></returns>
-        public ActionResult Edit(CategoryInputDto dto)
+        public async Task<ActionResult> Edit(CategoryInputDto dto)
         {
-            Category cat = CategoryService.GetById(dto.Id);
+            Category cat = await CategoryService.GetByIdAsync(dto.Id) ?? throw new NotFoundException("分类不存在!");
             cat.Name = dto.Name;
             cat.Description = dto.Description;
-            bool b = CategoryService.SaveChanges() > 0;
+            bool b = await CategoryService.SaveChangesAsync() > 0;
             return ResultData(null, b, b ? "分类修改成功!" : "分类修改失败!");
         }
 

+ 14 - 19
src/Masuit.MyBlogs.Core/Controllers/CommentController.cs

@@ -19,6 +19,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text.RegularExpressions;
+using System.Threading.Tasks;
 
 namespace Masuit.MyBlogs.Core.Controllers
 {
@@ -38,19 +39,14 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="dto"></param>
         /// <returns></returns>
         [HttpPost, ValidateAntiForgeryToken]
-        public ActionResult Put(CommentInputDto dto)
+        public async Task<ActionResult> Put(CommentInputDto dto)
         {
             if (Regex.Match(dto.Content, CommonHelper.BanRegex).Length > 0)
             {
                 return ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!");
             }
 
-            Post post = PostService.GetById(dto.PostId);
-            if (post is null)
-            {
-                return ResultData(null, false, "评论失败,文章不存在!");
-            }
-
+            Post post = await PostService.GetByIdAsync(dto.PostId) ?? throw new NotFoundException("评论失败,文章未找到");
             if (post.DisableComment)
             {
                 return ResultData(null, false, "本文已禁用评论功能,不允许任何人回复!");
@@ -104,7 +100,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
                 if (!comment.IsMaster)
                 {
-                    MessageService.AddEntitySaved(new InternalMessage()
+                    await MessageService.AddEntitySavedAsync(new InternalMessage()
                     {
                         Title = $"来自【{comment.NickName}】的新文章评论",
                         Content = comment.Content,
@@ -153,22 +149,21 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="id"></param>
         /// <returns></returns>
         [HttpPost]
-        public ActionResult CommentVote(int id)
+        public async Task<ActionResult> CommentVote(int id)
         {
-            Comment cm = CommentService.Get(c => c.Id == id && c.Status == Status.Pended);
             if (HttpContext.Session.Get("cm" + id) != null)
             {
                 return ResultData(null, false, "您刚才已经投过票了,感谢您的参与!");
             }
 
-            if (cm == null)
+            var cm = await CommentService.GetAsync(c => c.Id == id && c.Status == Status.Pended) ?? throw new NotFoundException("评论不存在!");
+            cm.VoteCount++;
+            bool b = CommentService.SaveChanges() > 0;
+            if (b)
             {
-                return ResultData(null, false, "非法操作");
+                HttpContext.Session.Set("cm" + id, id.GetBytes());
             }
 
-            cm.VoteCount++;
-            HttpContext.Session.Set("cm" + id, id.GetBytes());
-            bool b = CommentService.SaveChanges() > 0;
             return ResultData(null, b, b ? "投票成功" : "投票失败");
         }
 
@@ -227,12 +222,12 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="id"></param>
         /// <returns></returns>
         [MyAuthorize]
-        public ActionResult Pass(int id)
+        public async Task<ActionResult> Pass(int id)
         {
-            Comment comment = CommentService.GetById(id);
+            Comment comment = await CommentService.GetByIdAsync(id) ?? throw new NotFoundException("评论不存在!");
             comment.Status = Status.Pended;
-            Post post = PostService.GetById(comment.PostId);
-            bool b = CommentService.SaveChanges() > 0;
+            Post post = await PostService.GetByIdAsync(comment.PostId);
+            bool b = await CommentService.SaveChangesAsync() > 0;
             if (b)
             {
                 var pid = comment.ParentId == 0 ? comment.Id : CommentService.GetParentCommentIdByChildId(id);

+ 9 - 7
src/Masuit.MyBlogs.Core/Controllers/DonateController.cs

@@ -1,9 +1,11 @@
-using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Extensions;
+using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.Tools;
 using Microsoft.AspNetCore.Mvc;
 using System;
 using System.Linq;
+using System.Threading.Tasks;
 
 namespace Masuit.MyBlogs.Core.Controllers
 {
@@ -35,9 +37,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        public ActionResult Get(int id)
+        public async Task<ActionResult> Get(int id)
         {
-            Donate donate = DonateService.GetById(id);
+            Donate donate = await DonateService.GetByIdAsync(id) ?? throw new NotFoundException("条目不存在!");
             return ResultData(donate);
         }
 
@@ -46,13 +48,13 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="donate"></param>
         /// <returns></returns>
-        public ActionResult Save(Donate donate)
+        public async Task<ActionResult> Save(Donate donate)
         {
-            var entry = DonateService.GetById(donate.Id);
+            var entry = await DonateService.GetByIdAsync(donate.Id);
             bool b;
             if (entry is null)
             {
-                b = DonateService.AddEntitySaved(donate) != null;
+                b = await DonateService.AddEntitySavedAsync(donate) > 0;
             }
             else
             {
@@ -64,7 +66,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 entry.QQorWechat = donate.QQorWechat;
                 entry.QQorWechatDisplay = donate.QQorWechatDisplay;
                 entry.Via = donate.Via;
-                b = DonateService.SaveChanges() > 0;
+                b = await DonateService.SaveChangesAsync() > 0;
             }
             return ResultData(null, b, b ? "保存成功!" : "保存失败!");
         }

+ 8 - 2
src/Masuit.MyBlogs.Core/Controllers/ErrorController.cs

@@ -76,8 +76,14 @@ namespace Masuit.MyBlogs.Core.Controllers
                             return true;
                         });
                         break;
-                    case NotFoundException _:
-                        return View("Index");
+                    case NotFoundException ex:
+                        Response.StatusCode = 404;
+                        return Request.Method.ToLower().Equals("get") ? (ActionResult)View("Index") : Json(new
+                        {
+                            StatusCode = 404,
+                            Success = false,
+                            ex.Message
+                        });
                     default:
                         LogManager.Error($"异常源:{feature.Error.Source},异常类型:{feature.Error.GetType().Name},\n请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(req.Path)},客户端用户代理:{req.Headers["User-Agent"]},客户端IP:{ip}\t", feature.Error);
                         break;

+ 2 - 7
src/Masuit.MyBlogs.Core/Controllers/MiscController.cs

@@ -128,12 +128,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Delete(int id)
         {
-            var post = MiscService.GetById(id);
-            if (post is null)
-            {
-                return ResultData(null, false, "杂项页已经被删除!");
-            }
-
+            var post = MiscService.GetById(id) ?? throw new NotFoundException("杂项页已被删除!");
             var srcs = post.Content.MatchImgSrcs().Where(s => s.StartsWith("/"));
             foreach (var path in srcs)
             {
@@ -158,7 +153,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public async Task<ActionResult> Edit(Misc misc)
         {
-            var entity = MiscService.GetById(misc.Id);
+            var entity = MiscService.GetById(misc.Id) ?? throw new NotFoundException("杂项页未找到");
             entity.ModifyDate = DateTime.Now;
             entity.Title = misc.Title;
             entity.Content = await ImagebedClient.ReplaceImgSrc(misc.Content.ClearImgAttributes());

+ 3 - 8
src/Masuit.MyBlogs.Core/Controllers/NoticeController.cs

@@ -87,13 +87,8 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Delete(int id)
         {
-            var post = NoticeService.GetById(id);
-            if (post is null)
-            {
-                return ResultData(null, false, "公告已经被删除!");
-            }
-
-            var srcs = post.Content.MatchImgSrcs().Where(s => s.StartsWith("/"));
+            var notice = NoticeService.GetById(id) ?? throw new NotFoundException("公告已经被删除!");
+            var srcs = notice.Content.MatchImgSrcs().Where(s => s.StartsWith("/"));
             foreach (var path in srcs)
             {
                 try
@@ -117,7 +112,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public async Task<ActionResult> Edit(Notice notice)
         {
-            var entity = NoticeService.GetById(notice.Id);
+            var entity = NoticeService.GetById(notice.Id) ?? throw new NotFoundException("公告已经被删除!");
             entity.ModifyDate = DateTime.Now;
             entity.Title = notice.Title;
             entity.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes());

+ 37 - 56
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -61,9 +61,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="kw"></param>
         /// <returns></returns>
         [Route("{id:int}/{kw}"), Route("{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")]
-        public ActionResult Details(int id, string kw)
+        public async Task<ActionResult> Details(int id, string kw)
         {
-            var post = PostService.Get(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");
+            var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");
             ViewBag.Keyword = post.Keyword + "," + post.Label;
             var modifyDate = post.ModifyDate;
             ViewBag.Next = PostService.GetFromCache<DateTime, PostModelBase>(p => p.ModifyDate > modifyDate && (p.Status == Status.Pended || CurrentUser.IsAdmin), p => p.ModifyDate);
@@ -96,9 +96,9 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="size"></param>
         /// <returns></returns>
         [Route("{id:int}/history"), Route("{id:int}/history/{page:int}/{size:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "page", "size" }, VaryByHeader = "Cookie")]
-        public ActionResult History(int id, int page = 1, int size = 20)
+        public async Task<ActionResult> History(int id, int page = 1, int size = 20)
         {
-            var post = PostService.Get(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)).Mapper<PostOutputDto>() ?? throw new NotFoundException("文章未找到");
+            var post = await PostService.GetAsync(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)) ?? throw new NotFoundException("文章未找到");
             ViewBag.Primary = post;
             var list = PostHistoryVersionService.GetPages(page, size, out int total, v => v.PostId == id, v => v.ModifyDate, false).ToList();
             ViewBag.Total = total;
@@ -115,11 +115,11 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="hid"></param>
         /// <returns></returns>
         [Route("{id:int}/history/{hid:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "hid" }, VaryByHeader = "Cookie")]
-        public ActionResult HistoryVersion(int id, int hid)
+        public async Task<ActionResult> HistoryVersion(int id, int hid)
         {
-            var post = PostHistoryVersionService.Get(v => v.Id == hid) ?? throw new NotFoundException("文章未找到");
-            ViewBag.Next = PostHistoryVersionService.Get(p => p.PostId == id && p.ModifyDate > post.ModifyDate, p => p.ModifyDate);
-            ViewBag.Prev = PostHistoryVersionService.Get(p => p.PostId == id && p.ModifyDate < post.ModifyDate, p => p.ModifyDate, false);
+            var post = await PostHistoryVersionService.GetAsync(v => v.Id == hid) ?? throw new NotFoundException("文章未找到");
+            ViewBag.Next = await PostHistoryVersionService.GetAsync(p => p.PostId == id && p.ModifyDate > post.ModifyDate, p => p.ModifyDate);
+            ViewBag.Prev = await PostHistoryVersionService.GetAsync(p => p.PostId == id && p.ModifyDate < post.ModifyDate, p => p.ModifyDate, false);
             ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage, post.CategoryId);
             return CurrentUser.IsAdmin ? View("HistoryVersion_Admin", post) : View(post);
         }
@@ -132,11 +132,11 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="v2"></param>
         /// <returns></returns>
         [Route("{id:int}/history/{v1:int}-{v2:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id", "v1", "v2" }, VaryByHeader = "Cookie")]
-        public ActionResult CompareVersion(int id, int v1, int v2)
+        public async Task<ActionResult> CompareVersion(int id, int v1, int v2)
         {
             var main = PostService.Get(p => p.Id == id && (p.Status == Status.Pended || CurrentUser.IsAdmin)).Mapper<PostHistoryVersion>() ?? throw new NotFoundException("文章未找到");
-            var left = v1 <= 0 ? main : PostHistoryVersionService.Get(v => v.Id == v1) ?? throw new NotFoundException("文章未找到");
-            var right = v2 <= 0 ? main : PostHistoryVersionService.Get(v => v.Id == v2) ?? throw new NotFoundException("文章未找到");
+            var left = v1 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v1) ?? throw new NotFoundException("文章未找到");
+            var right = v2 <= 0 ? main : await PostHistoryVersionService.GetAsync(v => v.Id == v2) ?? throw new NotFoundException("文章未找到");
             main.Id = id;
             var diff = new HtmlDiff.HtmlDiff(right.Content, left.Content);
             var diffOutput = diff.Build();
@@ -151,24 +151,22 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        public ActionResult VoteDown(int id)
+        public async Task<ActionResult> VoteDown(int id)
         {
             if (HttpContext.Session.Get("post-vote" + id) != null)
             {
                 return ResultData(null, false, "您刚才已经投过票了,感谢您的参与!");
             }
 
-            Post post = PostService.GetById(id);
-            if (post == null)
+            Post post = await PostService.GetByIdAsync(id) ?? throw new NotFoundException("文章未找到");
+            post.VoteDownCount = post.VoteDownCount + 1;
+            var b = PostService.SaveChanges() > 0;
+            if (b)
             {
-                return ResultData(null, false, "非法操作");
+                HttpContext.Session.Set("post-vote" + id, id.GetBytes());
             }
 
-            HttpContext.Session.Set("post-vote" + id, id.GetBytes());
-            post.VoteDownCount += 1;
-            var b = PostService.SaveChanges() > 0;
             return ResultData(null, b, b ? "投票成功!" : "投票失败!");
-
         }
 
         /// <summary>
@@ -176,24 +174,22 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        public ActionResult VoteUp(int id)
+        public async Task<ActionResult> VoteUp(int id)
         {
             if (HttpContext.Session.Get("post-vote" + id) != null)
             {
                 return ResultData(null, false, "您刚才已经投过票了,感谢您的参与!");
             }
 
-            Post post = PostService.GetById(id);
-            if (post == null)
+            Post post = await PostService.GetByIdAsync(id) ?? throw new NotFoundException("文章未找到");
+            post.VoteUpCount += 1;
+            var b = await PostService.SaveChangesAsync() > 0;
+            if (b)
             {
-                return ResultData(null, false, "非法操作");
+                HttpContext.Session.Set("post-vote" + id, id.GetBytes());
             }
 
-            HttpContext.Session.Set("post-vote" + id, id.GetBytes());
-            post.VoteUpCount += 1;
-            var b = PostService.SaveChanges() > 0;
             return ResultData(null, b, b ? "投票成功!" : "投票失败!");
-
         }
 
         /// <summary>
@@ -216,7 +212,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [HttpPost, ValidateAntiForgeryToken]
         public async Task<ActionResult> Publish(PostInputDto post, [Required(ErrorMessage = "验证码不能为空")]string code)
         {
-            if (RedisHelper.Get("code:" + post.Email) != code)
+            if (await RedisHelper.GetAsync("code:" + post.Email) != code)
             {
                 return ResultData(null, false, "验证码错误!");
             }
@@ -245,7 +241,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 return ResultData(null, false, "文章发表失败!");
             }
 
-            RedisHelper.Expire("code:" + p.Email, 1);
+            await RedisHelper.ExpireAsync("code:" + p.Email, 1);
             var content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/publish.html")
                 .Replace("{{link}}", Url.Action("Details", "Post", new { id = p.Id }, Request.Scheme))
                 .Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
@@ -465,7 +461,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Fixtop(int id)
         {
-            Post post = PostService.GetById(id);
+            Post post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             post.IsFixedTop = !post.IsFixedTop;
             bool b = PostService.SaveChanges() > 0;
             return b ? ResultData(null, true, post.IsFixedTop ? "置顶成功!" : "取消置顶成功!") : ResultData(null, false, "操作失败!");
@@ -479,7 +475,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Pass(int id)
         {
-            Post post = PostService.GetById(id);
+            Post post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             post.Status = Status.Pended;
             post.ModifyDate = DateTime.Now;
             post.PostDate = DateTime.Now;
@@ -528,7 +524,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Delete(int id)
         {
-            var post = PostService.GetById(id);
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             post.Status = Status.Deleted;
             bool b = PostService.SaveChanges(true) > 0;
             return ResultData(null, b, b ? "删除成功!" : "删除失败!");
@@ -542,7 +538,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Restore(int id)
         {
-            var post = PostService.GetById(id);
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             post.Status = Status.Pended;
             bool b = PostService.SaveChanges() > 0;
             SearchEngine.LuceneIndexer.Add(post);
@@ -557,12 +553,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Truncate(int id)
         {
-            var post = PostService.GetById(id);
-            if (post is null)
-            {
-                return ResultData(null, false, "文章已经被删除!");
-            }
-
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             var srcs = post.Content.MatchImgSrcs();
             foreach (var path in srcs)
             {
@@ -590,7 +581,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Get(int id)
         {
-            Post post = PostService.GetById(id);
+            Post post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             PostOutputDto model = post.Mapper<PostOutputDto>();
             model.Seminars = post.Seminar.Select(s => s.Seminar.Title).Join(",");
             return ResultData(model);
@@ -868,8 +859,8 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult AddSeminar(int id, int sid)
         {
-            var post = PostService.GetById(id);
-            Seminar seminar = SeminarService.GetById(sid);
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
+            Seminar seminar = SeminarService.GetById(sid) ?? throw new NotFoundException("专题未找到");
             post.Seminar.Add(new SeminarPost()
             {
                 Post = post,
@@ -890,8 +881,8 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult RemoveSeminar(int id, int sid)
         {
-            var post = PostService.GetById(id);
-            Seminar seminar = SeminarService.GetById(sid);
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
+            Seminar seminar = SeminarService.GetById(sid) ?? throw new NotFoundException("专题未找到");
             post.Seminar.Remove(new SeminarPost()
             {
                 Post = post,
@@ -923,12 +914,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult Revert(int id)
         {
-            var history = PostHistoryVersionService.GetById(id);
-            if (history == null)
-            {
-                return ResultData(null, false, "版本不存在");
-            }
-
+            var history = PostHistoryVersionService.GetById(id) ?? throw new NotFoundException("版本不存在");
             history.Post.Category = history.Category;
             history.Post.CategoryId = history.CategoryId;
             history.Post.Content = history.Content;
@@ -959,12 +945,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         [MyAuthorize]
         public ActionResult DisableComment(int id)
         {
-            var post = PostService.GetById(id);
-            if (post is null)
-            {
-                return ResultData(null, false, "文章不存在");
-            }
-
+            var post = PostService.GetById(id) ?? throw new NotFoundException("文章未找到");
             post.DisableComment = !post.DisableComment;
             return ResultData(null, SearchEngine.SaveChanges() > 0, post.DisableComment ? $"已禁用【{post.Title}】这篇文章的评论功能!" : $"已启用【{post.Title}】这篇文章的评论功能!");
         }

+ 1 - 1
src/Masuit.MyBlogs.Core/Models/Entity/Post.cs

@@ -107,7 +107,7 @@ namespace Masuit.MyBlogs.Core.Models.Entity
         /// <summary>
         /// ·´¶ÔÊý
         /// </summary>
-        [DatabaseGenerated(DatabaseGeneratedOption.Computed), DefaultValue(0), ConcurrencyCheck]
+        [DefaultValue(0), ConcurrencyCheck]
         public int VoteDownCount { get; set; }
 
         /// <summary>

+ 2 - 2
src/Masuit.MyBlogs.Core/Views/Post/History.cshtml

@@ -2,7 +2,7 @@
 @using Masuit.MyBlogs.Core.Models.Entity
 @model List<Masuit.MyBlogs.Core.Models.Entity.PostHistoryVersion>
 @{
-    var post = (PostOutputDto)ViewBag.Primary;
+    var post = (Post)ViewBag.Primary;
     ViewBag.Title = post.Title + "的历史版本";
     Layout = "~/Views/Shared/_Layout.cshtml";
     Advertisement ad = ViewBag.Ads;
@@ -35,7 +35,7 @@
                     @Html.ActionLink(post.Title, "Details", "Post", new { id = post.Id }, null) <span class="text-red">(最新版本)</span>
                 </td>
                 <td>
-                    @Html.ActionLink(post.CategoryName, "Category", "Home", new { id = post.CategoryId }, null)
+                    @Html.ActionLink(post.Category.Name, "Category", "Home", new { id = post.CategoryId }, null)
                 </td>
                 <td>@post.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss")</td>
                 <td>@post.Modifier</td>

+ 8 - 8
src/Masuit.MyBlogs.Core/wwwroot/Scripts/global/article.js

@@ -382,14 +382,14 @@ function bindVote() {
 					});
 				}
 			}
-		});
-	}, () => {
-        window.notie.alert({
-		    type: 3,
-		    text: "请求失败,请稍候再试!",
-		    time: 4
-	    });
-    });
+		}, () => {
+            window.notie.alert({
+				type: 3,
+				text: "请求失败,请稍候再试!",
+				time: 4
+			});
+        });
+	});
 }
 
 //递归加载评论

File diff suppressed because it is too large
+ 0 - 0
src/Masuit.MyBlogs.Core/wwwroot/Scripts/global/article.min.js


+ 14 - 6
src/Masuit.MyBlogs.Core/wwwroot/ng-views/controllers/main.js

@@ -226,12 +226,20 @@
 				}
 				$scope.CheckLogin(res.data);
 			}
-		}, function() {
-			window.notie.alert({
-				type:3,
-				text:'服务请求失败!',
-				time:4
-			});
+		}, function(e) {
+            if (e.data) {
+                window.notie.alert({
+				    type:3,
+				    text:e.data.Message,
+				    time:4
+			    });
+            } else {
+                window.notie.alert({
+				    type:3,
+				    text:'服务请求失败!',
+				    time:4
+			    });
+            }
 			$scope.loadingDone();
 		});
 	}

Some files were not shown because too many files changed in this diff