add(Friend):添加好友相关逻辑
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IM_API.Services
|
||||
@@ -17,30 +19,51 @@ namespace IM_API.Services
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public Task<bool> BlockeFriendAsync(int friendId)
|
||||
#region 拉黑好友
|
||||
public async Task<bool> BlockeFriendAsync(int friendId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(x => x.Id == friendId);
|
||||
if (friend == null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
friend.StatusEnum = FriendStatus.Blocked;
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> BlockFriendByUserIdAsync(int userId, int toUserId)
|
||||
#endregion
|
||||
#region 通过用户id拉黑好友
|
||||
public async Task<bool> BlockFriendByUserIdAsync(int userId, int toUserId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(x => x.UserId == userId && x.FriendId == toUserId);
|
||||
if (friend == null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
friend.StatusEnum = FriendStatus.Blocked;
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> DeleteFriendAsync(int friendId)
|
||||
#endregion
|
||||
#region 删除好友关系
|
||||
public async Task<bool> DeleteFriendAsync(int friendId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(x => x.Id == friendId);
|
||||
if (friend is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
_context.Friends.Remove(friend);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> DeleteFriendByUserIdAsync(int userId, int toUserId)
|
||||
#endregion
|
||||
#region 通过用户id删除好友关系
|
||||
public async Task<bool> DeleteFriendByUserIdAsync(int userId, int toUserId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(x => x.UserId == userId && x.FriendId == toUserId);
|
||||
if (friend is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
_context.Friends.Remove(friend);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region 获取好友列表
|
||||
|
||||
public async Task<List<FriendInfoDto>> GetFriendListAsync(int userId, int page, int limit, bool desc)
|
||||
{
|
||||
var query = _context.Friends.Where(x => x.UserId == userId);
|
||||
var query = _context.Friends.Where(x => x.UserId == userId && x.StatusEnum == FriendStatus.Added);
|
||||
if (desc)
|
||||
{
|
||||
query = query.OrderByDescending(x => x.UserId);
|
||||
@@ -48,20 +71,104 @@ namespace IM_API.Services
|
||||
var friendList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync();
|
||||
return _mapper.Map<List<FriendInfoDto>>(friendList);
|
||||
}
|
||||
|
||||
public Task<FriendRequest> GetFriendRequestListAsync(int userId, bool isReceived, int page, int limit, bool desc)
|
||||
#endregion
|
||||
#region 获取好友请求列表
|
||||
public async Task<List<FriendRequest>> GetFriendRequestListAsync(int userId, bool isReceived, int page, int limit, bool desc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var query = _context.FriendRequests.AsQueryable();
|
||||
//是否为请求方
|
||||
if (isReceived)
|
||||
{
|
||||
query = _context.FriendRequests.Where(x => x.ResponseUser == userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = _context.FriendRequests.Where(x => x.RequestUser == userId);
|
||||
}
|
||||
if (desc)
|
||||
{
|
||||
query = query.OrderByDescending(x => x.Id);
|
||||
}
|
||||
var friendRequestList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync();
|
||||
return friendRequestList;
|
||||
}
|
||||
|
||||
public Task<bool> HandleFriendRequestAsync(HandleFriendRequestDto requestDto)
|
||||
#endregion
|
||||
#region 处理好友请求
|
||||
public async Task<bool> HandleFriendRequestAsync(HandleFriendRequestDto requestDto)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
//查询好友请求记录
|
||||
var friendRequest = await _context.FriendRequests
|
||||
.Include(e => e.ResponseUserNavigation)
|
||||
.FirstOrDefaultAsync(x => x.Id == requestDto.RequestId);
|
||||
|
||||
if (friendRequest is null) throw new BaseException(CodeDefine.FRIEND_REQUEST_NOT_FOUND);
|
||||
|
||||
//查询好友关系
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(
|
||||
x => x.UserId == friendRequest.RequestUser && x.FriendId == friendRequest.ResponseUser
|
||||
);
|
||||
if (friend is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
|
||||
public Task<bool> SendFriendRequestAsync(FriendRequestDto friendRequest)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (friend.StatusEnum != FriendStatus.Pending) throw new BaseException(CodeDefine.FRIEND_REQUEST_EXISTS);
|
||||
//处理好友请求操作
|
||||
switch (requestDto.Action)
|
||||
{
|
||||
//拒绝后标记
|
||||
case HandleFriendRequestAction.Reject:
|
||||
friend.StatusEnum = FriendStatus.Declined;
|
||||
friendRequest.StateEnum = FriendRequestState.Declined;
|
||||
break;
|
||||
|
||||
//同意后标记
|
||||
case HandleFriendRequestAction.Accept:
|
||||
friend.StatusEnum = FriendStatus.Added;
|
||||
friendRequest.StateEnum = FriendRequestState.Passed;
|
||||
|
||||
//根据当前好友请求为被申请方添加一条好友记录(注意:好友记录为双向)
|
||||
var ResponseFriend = _mapper.Map<Friend>(friendRequest);
|
||||
if (!string.IsNullOrEmpty(requestDto.RemarkName)) ResponseFriend.RemarkName = requestDto.RemarkName;
|
||||
_context.Friends.Add(ResponseFriend);
|
||||
break;
|
||||
|
||||
//无效操作
|
||||
default:
|
||||
throw new BaseException(CodeDefine.INVALID_ACTION);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
#region 发起好友请求
|
||||
public async Task<bool> SendFriendRequestAsync(FriendRequestDto dto)
|
||||
{
|
||||
//查询用户是否存在
|
||||
bool isExist = await _context.Users.AnyAsync(x => x.Id == dto.ToUserId);
|
||||
if (!isExist) throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
bool isExistUser2 = await _context.Users.AnyAsync(x => x.Id == dto.FromUserId);
|
||||
if(!isExistUser2) throw new BaseException(CodeDefine.USER_NOT_FOUND);
|
||||
// 检查是否已有好友关系或待处理请求
|
||||
bool alreadyExists = await _context.FriendRequests.AnyAsync(x =>
|
||||
x.RequestUser == dto.FromUserId && x.ResponseUser == dto.ToUserId && x.StateEnum == FriendRequestState.Pending
|
||||
);
|
||||
if (alreadyExists)
|
||||
throw new BaseException(CodeDefine.FRIEND_REQUEST_EXISTS);
|
||||
|
||||
//检查是否被对方拉黑
|
||||
bool isBlocked = await _context.Friends.AnyAsync(x =>
|
||||
x.UserId == dto.FromUserId && x.FriendId == dto.ToUserId && x.StatusEnum == FriendStatus.Blocked
|
||||
);
|
||||
if (isBlocked)
|
||||
throw new BaseException(CodeDefine.FRIEND_REQUEST_REJECTED);
|
||||
|
||||
//生成实体
|
||||
var friendRequst = _mapper.Map<FriendRequest>(dto);
|
||||
var friend = _mapper.Map<Friend>(friendRequst);
|
||||
_context.FriendRequests.Add(friendRequst);
|
||||
_context.Friends.Add(friend);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user