SeminarController.cs 7.2 KB

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