/// <summary> /// 设置文章置顶 /// </summary> /// <param name="articalId"></param> /// <param name="isSetTop"></param> /// <returns></returns> public async Task <IActionResult> OnPostSetTopAsync(long articalId, bool isSetTop) { var artical = await ArticalService.GetArticalByIdAsync(articalId).ConfigureAwait(false); if (artical == null) { return(NotFound()); } //是否被授权 AuthorizationResult authorizationResult = await AuthorizationService.AuthorizeAsync( User, Permissons.Permisson.Article_SetTop).ConfigureAwait(false); if (!authorizationResult.Succeeded) { return(Forbid()); } //文章置顶 var result = await ArticalService.SetArticleTopAsync(articalId, isSetTop).ConfigureAwait(false); if (!result.Succeeded) { AddModelError(result.ErrorMessage); return(BadRequest(ModelState)); } return(this.Content(string.Empty)); }
/// <summary> /// 删除文章评论 /// </summary> /// <param name="articalId"></param> /// <param name="commentId"></param> /// <returns></returns> public async Task <IActionResult> OnPostDeleteArticalComment(long commentId) { if (commentId == 0) { ModelState.AddModelError(string.Empty, "无效Id"); return(BadRequest(ModelState)); } //是否被授权 AuthorizationResult authorizationResult = await AuthorizationService.AuthorizeAsync( User, Permissons.Permisson.Article_Comment_Delete).ConfigureAwait(false); if (!authorizationResult.Succeeded) { return(Forbid()); } var result = await ArticalService.DeleteArticalCommentAsync(commentId).ConfigureAwait(false); if (result.Succeeded) { return(Content("ok")); } else { ModelState.AddModelError(string.Empty, result.ErrorMessage); return(BadRequest()); } }
//删除文章 public async Task <IActionResult> OnPostDeleteAsync(long articalId) { if (articalId > 0) { var artical = await ArticalService.GetArticalByIdAsync(articalId).ConfigureAwait(false); if (artical == null) { return(NotFound()); } //是否被授权 AuthorizationResult authorizationResult = await AuthorizationService.AuthorizeAsync( User, Permissons.Permisson.Article_Delete).ConfigureAwait(false); if (!authorizationResult.Succeeded) { return(Forbid()); } //删除文章 var resone = await ArticalService.DeleteArticalAsync(articalId).ConfigureAwait(false); if (resone.Succeeded) { return(new NoContentResult()); } ModelState.AddModelError(string.Empty, resone.ErrorMessage); } return(BadRequest(ModelState)); }
/// <summary> /// 添加评论 /// </summary> /// <returns></returns> public async Task <IActionResult> OnPostAsync(AddArticalCommentDto addArticalCommentDto) { do { if (!TryValidateModel(addArticalCommentDto)) { break; } var respone = await ArticalService.AddArticalCommentAsync(addArticalCommentDto).ConfigureAwait(false); if (respone.Succeeded) { return(Content(addArticalCommentDto.CommentContent)); } } while (false); return(BadRequest(ModelState)); }
public async Task OnGetAsync() { if (!ClassificationId.HasValue) { //mostVisitArtical = articalService.GetArticlesAsync() //.OrderByDescending(e => e.VisitCount) //.Take(5); mostVisitArtical = await ArticalService.GetArticlesAsync(func : a => { return(a.OrderByDescending(e => e.VisitCount)); }, take : 5).ConfigureAwait(false); } else { ArticalClassificationInfo = await ArticalService.GetArticalClassificationByIdAsync(ClassificationId.Value).ConfigureAwait(false); mostVisitArtical = await ArticalService.GetArticlesAsync(func : a => { return(a.Where(e => e.ClassificationId == ClassificationId).OrderByDescending(e => e.VisitCount)); }, take : 5).ConfigureAwait(false); } }
public async Task <IActionResult> OnGetAsync() { //获取这篇文章 Artical = await ArticalService.GetArticalByIdAsync(ArticalId).ConfigureAwait(false); if (Artical == null) { return(NotFound()); } //作者信息 Author = await UserClient.FindByIdAsync(new UserIdReq { UserId = Artical.UserId }); //文章分类信息 ClassificationInfo = await ArticalService.GetArticalClassificationByIdAsync(Artical.ClassificationId).ConfigureAwait(false); //阅读量+1 await ArticalService.UpdateArticalAsync(ArticalId, new UpdateArticalDto { ArticalId = ArticalId, VisitCount = Artical.VisitCount + 1, ClassificationId = Artical.ClassificationId, Content = Artical.Content, Title = Artical.Title }).ConfigureAwait(false); //评论列表 Comments = ArticalService.GetArticalComments(Artical.Id).OrderByDescending(e => e.CommentTime); //评论数量 CommentsCount = await Comments.CountAsync().ConfigureAwait(false); //获取作者其他文章且不包含这篇文章 AuthorOtherArticals = await ArticalService.GetArticlesAsync( articles => { return(articles.Where(e => e.Id != ArticalId).OrderByDescending(e => e.PublishTime)); }, take : 5).ConfigureAwait(false); return(Page()); }
public async Task <IActionResult> OnGetAsync() { //修改 if (Id.HasValue) { var artical = await ArticalService.GetArticalByIdAsync(Id.Value).ConfigureAwait(false); //更新文章 if (artical == null) { return(NotFound()); } //禁止修改别人的文章 if (artical.UserId != CurUserId) { return(Forbid()); } AddOrUpdateArtical = new AddOrUpdateArticalModel { ClassificationId = artical.ClassificationId, Content = artical.Content, Title = artical.Title }; } //有发布系统公告权限? if ((await AuthorizationService.AuthorizeAsync(User, Permissons.Permisson.Article_System) .ConfigureAwait(false)).Succeeded) { ArticalClassifications = await ArticalService.GetArticalClassificationsAsync().ConfigureAwait(false); } else { ArticalClassifications = (await ArticalService.GetArticalClassificationsAsync()).Where(e => e.Name != "系统公告").ToList(); } return(Page()); }
/// <summary> /// 获取文章 /// </summary> /// <param name="skipCount"></param> /// <returns></returns> public async Task <IActionResult> OnGetLoadArticalsAsync(int skipCount) { //using (MiniProfiler.Current.Step("使用从数据库中查询的数据,进行Http请求")) { // using (MiniProfiler.Current.CustomTiming("HTTP", "GET ")) { //await Task.Delay(2000); List <ArticalDto> articals = null; if (!ClassificationId.HasValue) { //没有分类 articals = await ArticalService.GetArticlesAsync(arg => { return(arg.OrderByDescending(e => e.IsSetTop) .ThenByDescending(e => e.SetTopDateTime) .ThenByDescending(e => e.PublishTime)); }, skip : skipCount, take : TakeCount).ConfigureAwait(false); } else { //有分类 articals = await ArticalService.GetArticlesAsync(arg => { return(arg.Where(e => e.ClassificationId == ClassificationId) .OrderByDescending(e => e.IsSetTop) .ThenByDescending(e => e.SetTopDateTime) .ThenByDescending(e => e.PublishTime)); }, skip : skipCount, take : TakeCount).ConfigureAwait(false); } return(new PartialViewResult { ViewName = "_ArticalListPartial", ViewData = new ViewDataDictionary <IList <ArticalDto> >(ViewData, articals) }); } } }
//添加/更新文章 public async Task <IActionResult> OnPostAsync(AddOrUpdateArticalModel AddOrUpdateArtical) { do { //bool isRunning = cac.Get<bool>($"OnPostAddArticalAsync"); //if (isRunning) //{ // ModelState.AddModelError("", "两次发帖时间间隔最短为30秒"); // break; //} //cache.Set($"OnPostAddArticalAsync", true, TimeSpan.FromSeconds(30)); if (!TryValidateModel(AddOrUpdateArtical)) { break; } //给图片添加响应式和水平居中 var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(AddOrUpdateArtical.Content); //htmlDoc = removeNodes(htmlDoc,new string[] { "//iframe", "//ins" }); var imgs = htmlDoc.DocumentNode.SelectNodes("//img"); if (imgs != null) { foreach (var img in imgs) { var src = img.Attributes.FirstOrDefault(e => e.Name == "src"); //移除所有属性 img.Attributes.RemoveAll(); img.AddClass("img-fluid d-block mx-auto"); img.SetAttributeValue("style", "width:90%"); img.SetAttributeValue("src", src.Value); } AddOrUpdateArtical.Content = htmlDoc.DocumentNode.OuterHtml; } if (!Id.HasValue) { Result <long> respone = await ArticalService.AddArticalAsync(new AddArticalDto { UserId = CurUserId, ClassificationId = AddOrUpdateArtical.ClassificationId, Content = AddOrUpdateArtical.Content, Title = AddOrUpdateArtical.Title.Trim() }).ConfigureAwait(false); if (!respone.Succeeded) { ModelState.AddModelError("", respone.ErrorMessage); break; } return(Content($"/Forum/ArticalDetail/{respone.Data}")); } else { var respone = await ArticalService.UpdateArticalAsync(Id.Value, new UpdateArticalDto { ArticalId = Id.Value, Content = AddOrUpdateArtical.Content, ClassificationId = AddOrUpdateArtical.ClassificationId, Title = AddOrUpdateArtical.Title.Trim() }).ConfigureAwait(false); if (!respone.Succeeded) { ModelState.AddModelError("", respone.ErrorMessage); break; } return(Content($"/Forum/ArticalDetail/{Id}")); } } while (false); return(BadRequest(ModelState)); }
public ArticalController() { _articalService = new ArticalService(); }