feat:完成评论功能搭建

This commit is contained in:
陈淳
2023-03-23 18:15:30 +08:00
parent 8213f6f8d7
commit 4babe3e05d
24 changed files with 219 additions and 2 deletions

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cike.AutoWebApi.Setting;
using Yi.BBS.Application.Contracts.Exhibition.Dtos.Argee;
using Yi.BBS.Domain.Exhibition.Entities;
using Yi.BBS.Domain.Forum.Entities;
using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Ddd.Services;
using Yi.Framework.Ddd.Services.Abstract;
using Yi.Framework.Uow;
namespace Yi.BBS.Application.Exhibition
{
/// <summary>
/// 点赞功能
/// </summary>
[AppService]
public class AgreeService : ApplicationService, IApplicationService, IAutoApiService
{
[Autowired]
private IRepository<AgreeEntity> _repository { get; set; }
[Autowired]
private IRepository<DiscussEntity> _discssRepository { get; set; }
[Autowired]
private ICurrentUser _currentUser { get; set; }
[Autowired]
private IUnitOfWorkManager _unitOfWorkManager { get; set; }
/// <summary>
/// 点赞,返回true为点赞+1返回false为点赞-1
/// </summary>
/// <returns></returns>
public async Task<ArgeeDto> PostOperateAsync(long discussId)
{
var entity = await _repository.GetFirstAsync(x => x.DiscussId == discussId && x.CreatorId == _currentUser.Id);
//判断是否已经点赞过
if (entity is null)
{
using (var uow = _unitOfWorkManager.CreateContext())
{
//没点赞过,添加记录即可,,修改总点赞数量
await _repository.InsertAsync(new AgreeEntity(discussId));
var discussEntity = await _discssRepository.GetByIdAsync(discussId);
if (discussEntity is null)
{
throw new UserFriendlyException("主题为空");
}
discussEntity.AgreeNum += 1;
await _discssRepository.UpdateAsync(discussEntity);
uow.Commit();
}
return new ArgeeDto(true);
}
else
{
using (var uow = _unitOfWorkManager.CreateContext())
{
//点赞过,删除即可,修改总点赞数量
await _repository.DeleteByIdAsync(entity.Id);
var discussEntity = await _discssRepository.GetByIdAsync(discussId);
if (discussEntity is null)
{
throw new UserFriendlyException("主题为空");
}
discussEntity.AgreeNum -= 1;
await _discssRepository.UpdateAsync(discussEntity);
uow.Commit();
}
return new ArgeeDto(false);
}
}
}
}