Files
Yi.Admin/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Forum/AgreeService.cs

72 lines
2.5 KiB
C#
Raw Normal View History

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;
2024-01-11 18:51:53 +08:00
using Yi.Framework.Bbs.Domain.Entities.Forum;
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
{
2024-05-22 14:35:08 +08:00
public AgreeService(ISqlSugarRepository<AgreeEntity> repository, ISqlSugarRepository<DiscussAggregateRoot> 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
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
2024-03-07 11:44:27 +08:00
/// Todo: 可放入领域层
2023-04-15 22:44:33 +08:00
/// </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
{
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
{
//点赞过,删除即可,修改总点赞数量
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);
}
}
}
}