SeminarController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Masuit.MyBlogs.Core.Common;
  2. using Masuit.MyBlogs.Core.Extensions;
  3. using Masuit.MyBlogs.Core.Models;
  4. using Masuit.Tools.AspNetCore.ModelBinder;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Linq.Dynamic.Core;
  8. using System.Runtime.InteropServices;
  9. namespace Masuit.MyBlogs.Core.Controllers;
  10. /// <summary>
  11. /// 专题页
  12. /// </summary>
  13. public sealed class SeminarController : BaseController
  14. {
  15. /// <summary>
  16. /// 专题
  17. /// </summary>
  18. public ISeminarService SeminarService { get; set; }
  19. /// <summary>
  20. /// 文章
  21. /// </summary>
  22. public IPostService PostService { get; set; }
  23. /// <summary>
  24. /// 专题页
  25. /// </summary>
  26. /// <param name="id"></param>
  27. /// <param name="page"></param>
  28. /// <param name="size"></param>
  29. /// <param name="orderBy"></param>
  30. /// <returns></returns>
  31. [Route("special/{id:int}"), Route("c/{id:int}", Order = 1), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "orderBy" }, VaryByHeader = "Cookie")]
  32. public async Task<ActionResult> Index(int id, [Optional] OrderBy? orderBy, [Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
  33. {
  34. var s = await SeminarService.GetByIdAsync(id) ?? throw new NotFoundException("专题未找到");
  35. var h24 = DateTime.Today.AddDays(-1);
  36. var posts = orderBy switch
  37. {
  38. OrderBy.Trending => await PostService.GetQuery(PostBaseWhere().And(p => p.Seminar.Any(x => x.Id == id))).OrderByDescending(p => p.PostVisitRecordStats.Where(e => e.Date >= h24).Sum(e => e.Count)).ToPagedListAsync<Post, PostDto>(page, size, MapperConfig),
  39. _ => await PostService.GetQuery(PostBaseWhere().And(p => p.Seminar.Any(x => x.Id == id))).OrderBy($"{nameof(Post.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").ToPagedListAsync<Post, PostDto>(page, size, MapperConfig)
  40. };
  41. ViewBag.Id = s.Id;
  42. ViewBag.Title = s.Title;
  43. ViewBag.Desc = s.Description;
  44. ViewBag.SubTitle = s.SubTitle;
  45. ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location(), keywords: s.Title);
  46. ViewData["page"] = new Pagination(page, size, posts.TotalCount, orderBy);
  47. PostService.SolvePostsCategory(posts.Data);
  48. foreach (var item in posts.Data)
  49. {
  50. item.PostDate = item.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  51. item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  52. }
  53. return View(posts);
  54. }
  55. #region 管理端
  56. /// <summary>
  57. /// 保存专题
  58. /// </summary>
  59. /// <param name="seminar"></param>
  60. /// <returns></returns>
  61. [MyAuthorize]
  62. public ActionResult Save([FromBodyOrDefault] Seminar seminar)
  63. {
  64. if (seminar.Id > 0 ? SeminarService.Any(s => s.Id != seminar.Id && s.Title == seminar.Title) : SeminarService.Any(s => s.Title == seminar.Title))
  65. {
  66. return ResultData(null, false, $"{seminar.Title} 已经存在了");
  67. }
  68. var entry = SeminarService.GetById(seminar.Id);
  69. bool b;
  70. if (entry is null)
  71. {
  72. b = SeminarService.AddEntitySaved(seminar) != null;
  73. }
  74. else
  75. {
  76. entry.Description = seminar.Description;
  77. entry.Title = seminar.Title;
  78. entry.SubTitle = seminar.SubTitle;
  79. b = SeminarService.SaveChanges() > 0;
  80. }
  81. return ResultData(null, b, b ? "保存成功" : "保存失败");
  82. }
  83. /// <summary>
  84. /// 删除专题
  85. /// </summary>
  86. /// <param name="id"></param>
  87. /// <returns></returns>
  88. [MyAuthorize]
  89. public async Task<ActionResult> Delete(int id)
  90. {
  91. bool b = await SeminarService.DeleteByIdAsync(id) > 0;
  92. return ResultData(null, b, b ? "删除成功" : "删除失败");
  93. }
  94. /// <summary>
  95. /// 获取专题详情
  96. /// </summary>
  97. /// <param name="id"></param>
  98. /// <returns></returns>
  99. [MyAuthorize]
  100. public async Task<ActionResult> Get(int id)
  101. {
  102. Seminar seminar = await SeminarService.GetByIdAsync(id);
  103. return ResultData(seminar.Mapper<SeminarDto>());
  104. }
  105. /// <summary>
  106. /// 专题分页列表
  107. /// </summary>
  108. /// <param name="page"></param>
  109. /// <param name="size"></param>
  110. /// <returns></returns>
  111. [MyAuthorize]
  112. public ActionResult GetPageData([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
  113. {
  114. var list = SeminarService.GetPages<int, SeminarDto>(page, size, s => true, s => s.Id, false);
  115. return Ok(list);
  116. }
  117. /// <summary>
  118. /// 获取所有专题
  119. /// </summary>
  120. /// <returns></returns>
  121. [MyAuthorize]
  122. public ActionResult GetAll()
  123. {
  124. var list = SeminarService.GetAll<string, SeminarDto>(s => s.Title).ToList();
  125. return ResultData(list);
  126. }
  127. /// <summary>
  128. /// 给专题添加文章
  129. /// </summary>
  130. /// <param name="id"></param>
  131. /// <param name="pid"></param>
  132. /// <returns></returns>
  133. [MyAuthorize]
  134. public async Task<ActionResult> AddPost(int id, int pid)
  135. {
  136. Seminar seminar = await SeminarService.GetByIdAsync(id);
  137. Post post = await PostService.GetByIdAsync(pid);
  138. seminar.Post.Add(post);
  139. bool b = await SeminarService.SaveChangesAsync() > 0;
  140. return ResultData(null, b, b ? $"已成功将【{post.Title}】添加到专题【{seminar.Title}】" : "添加失败!");
  141. }
  142. /// <summary>
  143. /// 移除文章
  144. /// </summary>
  145. /// <param name="id"></param>
  146. /// <param name="pid"></param>
  147. /// <returns></returns>
  148. [MyAuthorize]
  149. public async Task<ActionResult> RemovePost(int id, int pid)
  150. {
  151. Seminar seminar = await SeminarService.GetByIdAsync(id);
  152. Post post = await PostService.GetByIdAsync(pid);
  153. //bool b = await seminarPostService.DeleteEntitySavedAsync(s => s.SeminarId == id && s.PostId == pid) > 0;
  154. seminar.Post.Remove(post);
  155. var b = await SeminarService.SaveChangesAsync() > 0;
  156. return ResultData(null, b, b ? $"已成功将【{post.Title}】从专题【{seminar.Title}】移除" : "添加失败!");
  157. }
  158. #endregion 管理端
  159. }