using Easy.Admin.Application.Auth; using Easy.Admin.Application.Client.Dtos; namespace Easy.Admin.Application.Client; /// /// 评论 /// [ApiDescriptionSettings("博客前端接口")] public class CommentController : IDynamicApiController { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarRepository _praiseRepository; private readonly IHttpContextAccessor _httpContextAccessor; private readonly AuthManager _authManager; public CommentController(ISqlSugarRepository repository, ISqlSugarRepository praiseRepository, ApiService apiService, IHttpContextAccessor httpContextAccessor, AuthManager authManager) { _repository = repository; _praiseRepository = praiseRepository; _httpContextAccessor = httpContextAccessor; _authManager = authManager; } /// /// 评论列表 /// /// [HttpGet] [AllowAnonymous] public async Task> Get([FromQuery] CommentPageQueryInput dto) { long userId = _authManager.UserId; var result = await _repository.AsQueryable().LeftJoin((c, account) => c.AccountId == account.Id) .Where(c => c.ModuleId == dto.Id && c.RootId == null) //排除回复的评论 .OrderByDescending(c => c.Id) .Select((c, account) => new CommentOutput { Id = c.Id, Content = c.Content, PraiseTotal = SqlFunc.Subqueryable().Where(x => x.ObjectId == c.Id).Count(), IsPraise = SqlFunc.Subqueryable().Where(x => x.ObjectId == c.Id && x.AccountId == userId).Any(), ReplyCount = SqlFunc.Subqueryable().Where(s => s.RootId == c.Id).Count(), IP = c.IP, Avatar = account.Avatar, AccountId = account.Id, NickName = account.Name, IsBlogger = account.IsBlogger, Geolocation = c.Geolocation, CreatedTime = c.CreatedTime }) //.Mapper(it => //{ // if (it.ReplyCount > 0) // { // it.ReplyList = ReplyList(new CommentPageQueryInput() // { // PageNo = 1, // Id = it.Id // }).GetAwaiter().GetResult(); // } //}) .ToPagedListAsync(dto); return result; } /// /// 回复分页 /// /// /// [HttpGet] [AllowAnonymous] public async Task> ReplyList([FromQuery] CommentPageQueryInput dto) { long userId = _authManager.UserId; return await _repository.AsQueryable().LeftJoin((c, a1) => c.AccountId == a1.Id) .LeftJoin((c, a1, a2) => c.ReplyAccountId == a2.Id) .Where(c => c.RootId == dto.Id) .OrderBy(c => c.Id) .Select((c, a1, a2) => new ReplyOutput { Id = c.Id, Content = c.Content, ParentId = c.ParentId, AccountId = c.AccountId, ReplyAccountId = c.ReplyAccountId, IsBlogger = a1.IsBlogger, NickName = a1.Name, RelyNickName = a2.Name, RootId = c.RootId, Avatar = a1.Avatar, PraiseTotal = SqlFunc.Subqueryable().Where(x => x.ObjectId == c.Id).Count(), IsPraise = SqlFunc.Subqueryable().Where(x => x.ObjectId == c.Id && x.AccountId == userId).Any(), IP = c.IP, Geolocation = c.Geolocation, CreatedTime = c.CreatedTime }).ToPagedListAsync(dto); } /// /// 评论、回复 /// /// /// [HttpPost] public async Task Add(AddCommentInput dto) { string address = _httpContextAccessor.HttpContext.GetGeolocation(); var comments = dto.Adapt(); comments.AccountId = _authManager.UserId; comments.IP = _httpContextAccessor.HttpContext.GetRemoteIp(); comments.Geolocation = address; await _repository.InsertAsync(comments); } /// /// 删除留言 /// /// /// [HttpDelete("delete")] public async Task Delete(KeyDto dto) { await _repository.UpdateAsync(x => new Comments() { DeleteMark = true }, x => x.Id == dto.Id); } /// /// 点赞/取消点赞 /// /// 对象ID /// [HttpPost] public async Task Praise(KeyDto dto) { if (await _praiseRepository.IsAnyAsync(x => x.ObjectId == dto.Id)) { return await _praiseRepository.DeleteAsync(x => x.ObjectId == dto.Id) ? false : throw Oops.Oh("糟糕,取消失败了..."); } var praise = new Praise() { AccountId = _authManager.UserId, ObjectId = dto.Id, }; return await _praiseRepository.InsertAsync(praise) ? true : throw Oops.Oh("糟糕,点赞失败了..."); } }