feat:完成点赞功能

This commit is contained in:
橙子
2023-03-20 23:43:49 +08:00
parent b3b3ca3fe4
commit 2d31aeecd1
14 changed files with 238 additions and 11 deletions

View File

@@ -4,6 +4,17 @@
<name>Yi.BBS.Application</name>
</assembly>
<members>
<member name="T:Yi.BBS.Application.Exhibition.AgreeService">
<summary>
点赞功能
</summary>
</member>
<member name="M:Yi.BBS.Application.Exhibition.AgreeService.PostOperateAsync(System.Int64)">
<summary>
点赞,返回true为点赞+1返回false为点赞-1
</summary>
<returns></returns>
</member>
<member name="T:Yi.BBS.Application.Exhibition.BannerService">
<summary>
Banner服务实现

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);
}
}
}
}