SeminarController.cs 7.2 KB

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