2023-12-16 11:45:53 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Volo.Abp;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
using Volo.Abp.Uow;
|
|
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Argee;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Bbs.Application.Services
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 点赞功能
|
|
|
|
|
|
/// </summary>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public class AgreeService : ApplicationService, IApplicationService
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public AgreeService(ISqlSugarRepository<AgreeEntity> repository, ISqlSugarRepository<DiscussEntity> discssRepository)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
|
|
|
|
|
_discssRepository = discssRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private ISqlSugarRepository<AgreeEntity> _repository { get; set; }
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private ISqlSugarRepository<DiscussEntity> _discssRepository { get; set; }
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 点赞,返回true为点赞+1,返回false为点赞-1
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[UnitOfWork]
|
2023-12-16 11:45:53 +08:00
|
|
|
|
[Authorize]
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task<AgreeDto> PostOperateAsync(Guid discussId)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
var entity = await _repository.GetFirstAsync(x => x.DiscussId == discussId && x.CreatorId == this.CurrentUser.Id);
|
2023-04-15 22:44:33 +08:00
|
|
|
|
//判断是否已经点赞过
|
|
|
|
|
|
if (entity is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
//没点赞过,添加记录即可,,修改总点赞数量
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
return new AgreeDto(true);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
//点赞过,删除即可,修改总点赞数量
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
return new AgreeDto(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|