爆肝,重构框架,你懂得
This commit is contained in:
46
Yi.Framework.Net6/Yi.Framework.Service/BBS/AgreeService.cs
Normal file
46
Yi.Framework.Net6/Yi.Framework.Service/BBS/AgreeService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using SqlSugar;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Interface.BBS;
|
||||
using Yi.Framework.Model.BBS.Entitys;
|
||||
using Yi.Framework.Repository;
|
||||
using Yi.Framework.Service.Base;
|
||||
|
||||
namespace Yi.Framework.Service.BBS
|
||||
{
|
||||
public partial class AgreeService : BaseService<AgreeEntity>, IAgreeService
|
||||
{
|
||||
public AgreeService(IRepository<AgreeEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// 点赞操作
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> OperateAsync(long articleOrCommentId, long userId)
|
||||
{
|
||||
var _articleRepositoty = _repository.ChangeRepository<Repository<ArticleEntity>>();
|
||||
var article = await _articleRepositoty.GetByIdAsync(articleOrCommentId);
|
||||
if (await _repository.IsAnyAsync(u => u.UserId == userId && u.ArticleOrCommentId == articleOrCommentId))
|
||||
{
|
||||
//已点赞,取消点赞
|
||||
await _repository.UseTranAsync(async () =>
|
||||
{
|
||||
await _repository.DeleteAsync(u => u.UserId == userId && u.ArticleOrCommentId == articleOrCommentId);
|
||||
await _articleRepositoty.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleOrCommentId, AgreeNum = article.AgreeNum - 1 });
|
||||
|
||||
});
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
//未点赞,添加点赞记录
|
||||
await _repository.UseTranAsync(async () =>
|
||||
{
|
||||
await _repository.InsertReturnSnowflakeIdAsync(new AgreeEntity { UserId = userId, ArticleOrCommentId = articleOrCommentId });
|
||||
await _articleRepositoty.UpdateIgnoreNullAsync(new ArticleEntity { Id = articleOrCommentId, AgreeNum = article.AgreeNum + 1 });
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Yi.Framework.Net6/Yi.Framework.Service/BBS/ArticleService.cs
Normal file
33
Yi.Framework.Net6/Yi.Framework.Service/BBS/ArticleService.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using SqlSugar;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Interface.BBS;
|
||||
using Yi.Framework.Model.BBS.Entitys;
|
||||
using Yi.Framework.Repository;
|
||||
using Yi.Framework.Service.Base;
|
||||
|
||||
namespace Yi.Framework.Service.BBS
|
||||
{
|
||||
public partial class ArticleService : BaseService<ArticleEntity>, IArticleService
|
||||
{
|
||||
public ArticleService(IRepository<ArticleEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
public async Task<PageModel<List<ArticleEntity>>> SelctPageList(ArticleEntity entity, PageParModel page)
|
||||
{
|
||||
RefAsync<int> total = 0;
|
||||
var data = await _repository._DbQueryable
|
||||
.Includes(x => x.User)
|
||||
//.WhereIF(!string.IsNullOrEmpty(config.ConfigName), u => u.ConfigName.Contains(config.ConfigName))
|
||||
//.WhereIF(!string.IsNullOrEmpty(config.ConfigKey), u => u.ConfigKey.Contains(config.ConfigKey))
|
||||
.WhereIF(page.StartTime is not null && page.EndTime is not null, u => u.CreateTime >= page.StartTime && u.CreateTime <= page.EndTime)
|
||||
.WhereIF(entity.IsDeleted is not null, u => u.IsDeleted == entity.IsDeleted)
|
||||
.OrderBy(u => u.CreateTime, OrderByType.Desc)
|
||||
.ToPageListAsync(page.PageNum, page.PageSize, total);
|
||||
|
||||
return new PageModel<List<ArticleEntity>>(data, total);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Yi.Framework.Net6/Yi.Framework.Service/BBS/CommentService.cs
Normal file
35
Yi.Framework.Net6/Yi.Framework.Service/BBS/CommentService.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using SqlSugar;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Interface.BBS;
|
||||
using Yi.Framework.Model.BBS.Entitys;
|
||||
using Yi.Framework.Repository;
|
||||
using Yi.Framework.Service.Base;
|
||||
|
||||
namespace Yi.Framework.Service.BBS
|
||||
{
|
||||
public partial class CommentService : BaseService<CommentEntity>, ICommentService
|
||||
{
|
||||
public CommentService(IRepository<CommentEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
//添加一个评论
|
||||
public async Task<bool> AddAsync(CommentEntity comment)
|
||||
{
|
||||
//如果是一级评论:不用处理
|
||||
|
||||
//如果是二级评论:ParentId父节点评论数+1
|
||||
return await _repository.UseTranAsync(async () =>
|
||||
{
|
||||
if (comment.ParentId != 0)
|
||||
{
|
||||
var parentData = await _repository.GetByIdAsync(comment.ParentId);
|
||||
parentData.CommentNum += 1;
|
||||
await _repository.AsUpdateable(parentData).UpdateColumns(u => new { u.CommentNum }).ExecuteCommandAsync();
|
||||
}
|
||||
await _repository.InsertReturnSnowflakeIdAsync(comment);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user