Skip to content

Commit

Permalink
1.升级.NET8
Browse files Browse the repository at this point in the history
2.性能优化
  • Loading branch information
ldqk committed Nov 17, 2023
1 parent 6b270e3 commit 8ea7a14
Show file tree
Hide file tree
Showing 24 changed files with 2,311 additions and 2,259 deletions.
561 changes: 279 additions & 282 deletions src/Masuit.MyBlogs.Core/Common/UserAgent.cs

Large diffs are not rendered by default.

146 changes: 73 additions & 73 deletions src/Masuit.MyBlogs.Core/Configs/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,76 @@ namespace Masuit.MyBlogs.Core.Configs;
/// </summary>
public sealed class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<CategoryCommand, Category>().ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Category, CategoryDto_P>().ReverseMap();
CreateMap<Category, CategoryDto>().ReverseMap();
CreateMap<CategoryCommand, CategoryDto>().ReverseMap();

CreateMap<CommentCommand, Comment>().ForMember(c => c.Status, e => e.MapFrom(c => Status.Pending)).ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Comment, CommentDto>().ReverseMap();
CreateMap<CommentCommand, CommentDto>().ReverseMap();
CreateMap<Comment, CommentViewModel>().ForMember(c => c.CommentDate, e => e.MapFrom(c => c.CommentDate.ToString("yyyy-MM-dd HH:mm:ss"))).ReverseMap();

CreateMap<LeaveMessageCommand, LeaveMessage>().ForMember(c => c.Status, e => e.MapFrom(c => Status.Pending)).ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<LeaveMessage, LeaveMessageDto>().ReverseMap();
CreateMap<LeaveMessageCommand, LeaveMessageDto>().ReverseMap();
CreateMap<LeaveMessage, LeaveMessageViewModel>().ForMember(l => l.PostDate, e => e.MapFrom(l => l.PostDate.ToString("yyyy-MM-dd HH:mm:ss"))).ReverseMap();

CreateMap<Links, LinksDto>().ForMember(e => e.Loopbacks, e => e.MapFrom(m => m.Loopbacks.GroupBy(e =>
e.IP).Count())).ReverseMap();

CreateMap<MenuCommand, Menu>().ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Menu, MenuDto>().ForMember(m => m.Children, e => e.MapFrom(m => m.Children.OrderBy(c => c.Sort).ToList())).ReverseMap();

CreateMap<Misc, MiscDto>().ReverseMap();

CreateMap<Notice, NoticeDto>().ReverseMap();

CreateMap<PostCommand, Post>().ReverseMap();
CreateMap<Post, PostModelBase>();
CreateMap<Post, PostHistoryVersion>().ForMember(p => p.Id, e => e.Ignore()).ForMember(v => v.PostId, e => e.MapFrom(p => p.Id));
CreateMap<Post, PostDto>().ForMember(p => p.CategoryName, e => e.MapFrom(p => p.Category.Name)).ForMember(p => p.LimitMode, e => e.MapFrom(p => p.LimitMode ?? RegionLimitMode.All)).ForMember(p => p.Category, e => e.Ignore()).ReverseMap();
CreateMap<PostCommand, PostDto>().ReverseMap();
CreateMap<PostHistoryVersion, PostDto>().ForMember(p => p.CategoryName, e => e.MapFrom(p => p.Category.Name)).ReverseMap();
CreateMap<Post, PostDataModel>().ForMember(p => p.ModifyDate, e => e.MapFrom(p => p.ModifyDate))
.ForMember(p => p.PostDate, e => e.MapFrom(p => p.PostDate))
.ForMember(p => p.Status, e => e.MapFrom(p => p.Status.GetDisplay()))
.ForMember(p => p.ModifyCount, e => e.MapFrom(p => p.PostHistoryVersion.Count))
.ForMember(p => p.ViewCount, e => e.MapFrom(p => p.TotalViewCount))
.ForMember(p => p.Seminars, e => e.MapFrom(p => p.Seminar.Select(s => s.Id).ToArray()))
.ForMember(p => p.LimitDesc, e => e.MapFrom(p => p.LimitMode > RegionLimitMode.All ? string.Format(p.LimitMode.GetDescription(), p.Regions, p.ExceptRegions) : "无限制"));

CreateMap<SearchDetails, SearchDetailsDto>().ReverseMap();

CreateMap<UserInfo, UserInfoDto>();
CreateMap<UserInfoDto, UserInfo>()
.ForMember(u => u.Id, e => e.Ignore())
.ForMember(u => u.Password, e => e.Ignore())
.ForMember(u => u.SaltKey, e => e.Ignore());

CreateMap<LoginRecord, LoginRecordViewModel>().ReverseMap();

CreateMap<Seminar, SeminarDto>().ReverseMap();

CreateMap<PostMergeRequestCommandBase, PostMergeRequest>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.MergeState, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequestCommand, PostMergeRequest>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.MergeState, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequestCommand, Post>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.Status, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequest, PostMergeRequestDtoBase>().ForMember(p => p.PostTitle, e => e.MapFrom(r => r.Post.Title));
CreateMap<PostMergeRequest, PostMergeRequestDto>().ForMember(p => p.PostTitle, e => e.MapFrom(r => r.Post.Title));
CreateMap<PostMergeRequest, Post>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.Status, e => e.Ignore()).ReverseMap();
CreateMap<Post, PostMergeRequestDto>().ReverseMap();

CreateMap<Advertisement, AdvertisementViewModel>()
.ForMember(m => m.AverageViewCount, e => e.MapFrom(a => a.ClickRecords.Where(o => o.Time >= DateTime.Today.AddMonths(-1)).GroupBy(r => r.Time.Date).Select(g => g.Count()).DefaultIfEmpty().Average()))
.ForMember(m => m.ViewCount, e => e.MapFrom(a => a.ClickRecords.Count(o => o.Time >= DateTime.Today.AddMonths(-1))));
CreateMap<AdvertisementDto, Advertisement>().ForMember(a => a.ClickRecords, e => e.Ignore()).ForMember(a => a.Status, e => e.Ignore()).ForMember(a => a.UpdateTime, e => e.MapFrom(a => DateTime.Now)).ReverseMap();

CreateMap<Donate, DonateDto>();

CreateMap<PostVisitRecord, PostVisitRecordViewModel>().ForMember(m => m.Time, e => e.MapFrom(m => m.Time.ToString("yyyy-MM-dd HH:mm:ss")));

CreateMap<AdvertisementClickRecord, AdvertisementClickRecordViewModel>().ForMember(m => m.Time, e => e.MapFrom(m => m.Time.ToString("yyyy-MM-dd HH:mm:ss")));
}
}
public MappingProfile()
{
CreateMap<CategoryCommand, Category>().ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Category, CategoryDto_P>().ReverseMap();
CreateMap<Category, CategoryDto>().ReverseMap();
CreateMap<CategoryCommand, CategoryDto>().ReverseMap();

CreateMap<CommentCommand, Comment>().ForMember(c => c.Status, e => e.MapFrom(c => Status.Pending)).ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Comment, CommentDto>().ReverseMap();
CreateMap<CommentCommand, CommentDto>().ReverseMap();
CreateMap<Comment, CommentViewModel>().ForMember(c => c.CommentDate, e => e.MapFrom(c => c.CommentDate.ToString("yyyy-MM-dd HH:mm:ss"))).ReverseMap();

CreateMap<LeaveMessageCommand, LeaveMessage>().ForMember(c => c.Status, e => e.MapFrom(c => Status.Pending)).ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<LeaveMessage, LeaveMessageDto>().ReverseMap();
CreateMap<LeaveMessageCommand, LeaveMessageDto>().ReverseMap();
CreateMap<LeaveMessage, LeaveMessageViewModel>().ForMember(l => l.PostDate, e => e.MapFrom(l => l.PostDate.ToString("yyyy-MM-dd HH:mm:ss"))).ReverseMap();

CreateMap<Links, LinksDto>().ForMember(e => e.Loopbacks, e => e.MapFrom(m => m.Loopbacks.GroupBy(e =>
e.IP).Count())).ReverseMap();

CreateMap<MenuCommand, Menu>().ForMember(c => c.ParentId, e => e.MapFrom(c => c.ParentId > 0 ? c.ParentId : null)).ReverseMap();
CreateMap<Menu, MenuDto>().ForMember(m => m.Children, e => e.MapFrom(m => m.Children.OrderBy(c => c.Sort).ToList())).ReverseMap();

CreateMap<Misc, MiscDto>().ReverseMap();

CreateMap<Notice, NoticeDto>().ReverseMap();

CreateMap<PostCommand, Post>().ReverseMap();
CreateMap<Post, PostModelBase>();
CreateMap<Post, PostHistoryVersion>().ForMember(p => p.Id, e => e.Ignore()).ForMember(v => v.PostId, e => e.MapFrom(p => p.Id));
CreateMap<Post, PostDto>().ForMember(p => p.CategoryName, e => e.MapFrom(p => p.Category.Name)).ForMember(p => p.LimitMode, e => e.MapFrom(p => p.LimitMode ?? RegionLimitMode.All)).ForMember(p => p.Category, e => e.Ignore()).ReverseMap();
CreateMap<PostCommand, PostDto>().ReverseMap();
CreateMap<PostHistoryVersion, PostDto>().ForMember(p => p.CategoryName, e => e.MapFrom(p => p.Category.Name)).ReverseMap();
CreateMap<Post, PostDataModel>().ForMember(p => p.ModifyDate, e => e.MapFrom(p => p.ModifyDate))
.ForMember(p => p.PostDate, e => e.MapFrom(p => p.PostDate))
.ForMember(p => p.Status, e => e.MapFrom(p => p.Status.GetDisplay()))
.ForMember(p => p.ModifyCount, e => e.MapFrom(p => p.PostHistoryVersion.Count))
.ForMember(p => p.ViewCount, e => e.MapFrom(p => p.TotalViewCount))
.ForMember(p => p.Seminars, e => e.MapFrom(p => p.Seminar.Select(s => s.Id).ToArray()))
.ForMember(p => p.LimitDesc, e => e.MapFrom(p => p.LimitMode > RegionLimitMode.All ? string.Format(p.LimitMode.GetDescription(), p.Regions, p.ExceptRegions) : "无限制"));

CreateMap<SearchDetails, SearchDetailsDto>().ReverseMap();

CreateMap<UserInfo, UserInfoDto>();
CreateMap<UserInfoDto, UserInfo>()
.ForMember(u => u.Id, e => e.Ignore())
.ForMember(u => u.Password, e => e.Ignore())
.ForMember(u => u.SaltKey, e => e.Ignore());

CreateMap<LoginRecord, LoginRecordViewModel>().ReverseMap();

CreateMap<Seminar, SeminarDto>().ReverseMap();

CreateMap<PostMergeRequestCommandBase, PostMergeRequest>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.MergeState, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequestCommand, PostMergeRequest>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.MergeState, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequestCommand, Post>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.Status, e => e.Ignore()).ReverseMap();
CreateMap<PostMergeRequest, PostMergeRequestDtoBase>().ForMember(p => p.PostTitle, e => e.MapFrom(r => r.Post.Title));
CreateMap<PostMergeRequest, PostMergeRequestDto>().ForMember(p => p.PostTitle, e => e.MapFrom(r => r.Post.Title));
CreateMap<PostMergeRequest, Post>().ForMember(p => p.Id, e => e.Ignore()).ForMember(p => p.Status, e => e.Ignore()).ReverseMap();
CreateMap<Post, PostMergeRequestDto>().ReverseMap();

CreateMap<Advertisement, AdvertisementViewModel>()
.ForMember(m => m.AverageViewCount, e => e.MapFrom(a => a.ClickRecords.Where(o => o.Time >= DateTime.Today.AddMonths(-1)).GroupBy(r => r.Time.Date).Select(g => g.Count()).DefaultIfEmpty().Average()))
.ForMember(m => m.ViewCount, e => e.MapFrom(a => a.ClickRecords.Count(o => o.Time >= DateTime.Today.AddMonths(-1))));
CreateMap<AdvertisementDto, Advertisement>().ForMember(a => a.ClickRecords, e => e.Ignore()).ForMember(a => a.Status, e => e.Ignore()).ForMember(a => a.UpdateTime, e => e.MapFrom(a => DateTime.Now)).ReverseMap();

CreateMap<Donate, DonateDto>();

CreateMap<PostVisitRecord, PostVisitRecordViewModel>().ForMember(m => m.Time, e => e.MapFrom(m => m.Time.ToString("yyyy-MM-dd HH:mm:ss")));

CreateMap<AdvertisementClickRecord, AdvertisementClickRecordViewModel>().ForMember(m => m.Time, e => e.MapFrom(m => m.Time.ToString("yyyy-MM-dd HH:mm:ss")));
}
}
117 changes: 58 additions & 59 deletions src/Masuit.MyBlogs.Core/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,67 @@ namespace Masuit.MyBlogs.Core.Controllers;
/// </summary>
public sealed class CategoryController : BaseController
{
/// <summary>
/// CategoryService
/// </summary>
public ICategoryService CategoryService { get; set; }
/// <summary>
/// CategoryService
/// </summary>
public ICategoryService CategoryService { get; set; }

/// <summary>
/// 获取所有分类
/// </summary>
/// <returns></returns>
public ActionResult GetCategories()
{
var categories = CategoryService.GetQueryNoTracking(c => c.Status == Status.Available, c => c.Name).ToList();
var list = categories.ToTree(c => c.Id, c => c.ParentId);
return ResultData(Mapper.Map<List<CategoryDto>>(list));
}
/// <summary>
/// 获取所有分类
/// </summary>
/// <returns></returns>
public ActionResult GetCategories()
{
var categories = CategoryService.GetQuery<string, CategoryCommand>(c => c.Status == Status.Available, c => c.Name).ToList();
return ResultData(Mapper.Map<List<CategoryDto>>(categories).ToTree());
}

/// <summary>
/// 获取分类详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ActionResult> Get(int id)
{
var model = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类不存在!");
return ResultData(Mapper.Map<CategoryDto>(model));
}
/// <summary>
/// 获取分类详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ActionResult> Get(int id)
{
var model = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类不存在!");
return ResultData(Mapper.Map<CategoryDto>(model));
}

/// <summary>
/// 保存分类
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
[MyAuthorize]
public async Task<ActionResult> Save([FromBodyOrDefault] CategoryCommand cmd)
{
var cat = await CategoryService.GetByIdAsync(cmd.Id);
if (cat == null)
{
var category = Mapper.Map<Category>(cmd);
category.Path = cmd.ParentId > 0 ? (CategoryService[cmd.ParentId.Value].Path + "," + cmd.ParentId).Trim(',') : SnowFlake.NewId;
var b1 = await CategoryService.AddEntitySavedAsync(category) > 0;
return ResultData(null, b1, b1 ? "分类添加成功!" : "分类添加失败!");
}
/// <summary>
/// 保存分类
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
[MyAuthorize]
public async Task<ActionResult> Save([FromBodyOrDefault] CategoryCommand cmd)
{
var cat = await CategoryService.GetByIdAsync(cmd.Id);
if (cat == null)
{
var category = Mapper.Map<Category>(cmd);
category.Path = cmd.ParentId > 0 ? (CategoryService[cmd.ParentId.Value].Path + "," + cmd.ParentId).Trim(',') : SnowFlake.NewId;
var b1 = await CategoryService.AddEntitySavedAsync(category) > 0;
return ResultData(null, b1, b1 ? "分类添加成功!" : "分类添加失败!");
}

cat.Name = cmd.Name;
cat.Description = cmd.Description;
cat.ParentId = cmd.ParentId;
cat.Path = cmd.ParentId > 0 ? (CategoryService[cmd.ParentId.Value].Path + "," + cmd.ParentId).Trim(',') : SnowFlake.NewId;
bool b = await CategoryService.SaveChangesAsync() > 0;
return ResultData(null, b, b ? "分类修改成功!" : "分类修改失败!");
}
cat.Name = cmd.Name;
cat.Description = cmd.Description;
cat.ParentId = cmd.ParentId;
cat.Path = cmd.ParentId > 0 ? (CategoryService[cmd.ParentId.Value].Path + "," + cmd.ParentId).Trim(',') : SnowFlake.NewId;
bool b = await CategoryService.SaveChangesAsync() > 0;
return ResultData(null, b, b ? "分类修改成功!" : "分类修改失败!");
}

/// <summary>
/// 删除分类
/// </summary>
/// <param name="id"></param>
/// <param name="cid"></param>
/// <returns></returns>
[MyAuthorize]
public async Task<ActionResult> Delete(int id, int cid = 1)
{
bool b = await CategoryService.Delete(id, cid);
return ResultData(null, b, b ? "分类删除成功" : "分类删除失败");
}
/// <summary>
/// 删除分类
/// </summary>
/// <param name="id"></param>
/// <param name="cid"></param>
/// <returns></returns>
[MyAuthorize]
public async Task<ActionResult> Delete(int id, int cid = 1)
{
bool b = await CategoryService.Delete(id, cid);
return ResultData(null, b, b ? "分类删除成功" : "分类删除失败");
}
}
11 changes: 8 additions & 3 deletions src/Masuit.MyBlogs.Core/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public async Task<ActionResult> Category(int id, [Optional] OrderBy? orderBy, in
{
page = Math.Max(1, page);
var cat = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("文章分类未找到");
var cids = cat.Flatten().Select(c => c.Id).ToArray();
var cids = CategoryService.GetQuery(c => c.Status == Status.Available && c.Path.StartsWith(cat.Path + ",")).Select(c => c.Id).Cacheable().AsEnumerable().Append(id).ToArray();
var h24 = DateTime.Today.AddDays(-1);
var posts = orderBy switch
{
Expand All @@ -245,7 +245,6 @@ public async Task<ActionResult> Category(int id, [Optional] OrderBy? orderBy, in
};
var viewModel = GetIndexPageViewModel();
viewModel.Posts = posts;
ViewBag.Category = cat;
viewModel.PageParams = new Pagination(page, size, posts.TotalCount, orderBy);
viewModel.SidebarAds = AdsService.GetsByWeightedPrice(2, AdvertiseType.SideBar, Request.Location(), id);
viewModel.ListAdvertisement = AdsService.GetByWeightedPrice(AdvertiseType.ListItem, Request.Location(), id);
Expand All @@ -255,6 +254,7 @@ public async Task<ActionResult> Category(int id, [Optional] OrderBy? orderBy, in
item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
}

ViewBag.Category = cat;
return View(viewModel);
}

Expand Down Expand Up @@ -316,7 +316,12 @@ private HomePageViewModel GetIndexPageViewModel()
{
var postsQuery = PostService.GetQuery<PostDto>(PostBaseWhere()); //准备文章的查询
var notices = NoticeService.GetPagesFromCache<DateTime, NoticeDto>(1, 5, n => n.NoticeStatus == NoticeStatus.Normal, n => n.ModifyDate, false); //加载前5条公告
var cats = CategoryService.GetQuery(c => c.Status == Status.Available && c.Post.Count > 0).Include(c => c.Parent).OrderBy(c => c.Name).ThenBy(c => c.Path).AsNoTracking().Cacheable().ToPooledListScope(); //加载分类目录
var cats = CategoryService.GetQuery(c => c.Status == Status.Available && c.Post.Count > 0).OrderBy(c => c.Name).ThenBy(c => c.Path).AsNoTracking().Select(c => new Category
{
Id = c.Id,
Name = c.Name,
ParentId = c.ParentId,
}).Cacheable().ToPooledListScope(); //加载分类目录
var hotSearches = RedisHelper.Get<List<KeywordsRank>>("SearchRank:Week").AsNotNull().Take(10).ToPooledListScope(); //热词统计
var hot5Post = postsQuery.OrderBy((new Random().Next() % 3) switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/Masuit.MyBlogs.Core/Controllers/MsgController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task<ActionResult> GetMsgs([Range(1, int.MaxValue, ErrorMessage = "
parentTotal = total,
page,
size,
rows = Mapper.Map<IList<LeaveMessageViewModel>>(messages.OrderByDescending(c => c.PostDate).ToTree(c => c.Id, c => c.ParentId))
rows = Mapper.Map<List<LeaveMessageViewModel>>(messages.OrderByDescending(c => c.PostDate).ToTree(c => c.Id, c => c.ParentId))
});
}

Expand Down
Loading

0 comments on commit 8ea7a14

Please sign in to comment.