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;
|
2025-06-02 02:12:38 +08:00
|
|
|
|
using Volo.Abp.Users;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Argee;
|
2024-01-11 18:51:53 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Forum;
|
2025-06-02 02:12:38 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Managers;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
2024-01-11 18:51:53 +08:00
|
|
|
|
namespace Yi.Framework.Bbs.Application.Services.Forum
|
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
|
|
|
|
{
|
2025-06-02 02:12:38 +08:00
|
|
|
|
public AgreeService(ISqlSugarRepository<AgreeEntity> repository, ISqlSugarRepository<DiscussAggregateRoot> discssRepository, BbsUserManager bbsUserManager)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
|
|
|
|
|
_discssRepository = discssRepository;
|
2025-06-02 02:12:38 +08:00
|
|
|
|
_bbsUserManager = bbsUserManager;
|
2023-04-15 22:44:33 +08:00
|
|
|
|
}
|
2025-06-02 02:12:38 +08:00
|
|
|
|
private readonly BbsUserManager _bbsUserManager;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private ISqlSugarRepository<AgreeEntity> _repository { get; set; }
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<DiscussAggregateRoot> _discssRepository { get; set; }
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 点赞,返回true为点赞+1,返回false为点赞-1
|
2025-06-02 02:12:38 +08:00
|
|
|
|
/// Todo: 可放入领域层,但是没必要,这个项目太简单了
|
2023-04-15 22:44:33 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
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
|
|
|
|
{
|
2025-06-02 02:12:38 +08:00
|
|
|
|
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
|
2024-01-11 18:51:53 +08:00
|
|
|
|
var entity = await _repository.GetFirstAsync(x => x.DiscussId == discussId && x.CreatorId == 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
|
|
|
|
|
|
{
|
|
|
|
|
|
//点赞过,删除即可,修改总点赞数量
|
2024-05-23 23:40:55 +08:00
|
|
|
|
await _repository.DeleteAsync(entity);
|
2023-04-15 22:44:33 +08:00
|
|
|
|
var discussEntity = await _discssRepository.GetByIdAsync(discussId);
|
|
|
|
|
|
if (discussEntity is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("主题为空");
|
|
|
|
|
|
}
|
|
|
|
|
|
discussEntity.AgreeNum -= 1;
|
|
|
|
|
|
await _discssRepository.UpdateAsync(discussEntity);
|
|
|
|
|
|
|
|
|
|
|
|
return new AgreeDto(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|