后端:添加添加好友处理事件

文档:添加测试环境文档
This commit is contained in:
2026-01-30 22:13:56 +08:00
parent a590aa0a26
commit 2d22b4bd27
12 changed files with 115 additions and 26 deletions
+23
View File
@@ -175,5 +175,28 @@ namespace IM_API.Services
return true;
}
#endregion
#region
public async Task MakeFriendshipAsync(int userAId, int userBId, string? remarkName)
{
bool userAexist = await _context.Friends.AnyAsync(x => x.UserId == userAId && x.FriendId == userBId);
if (!userAexist)
{
User? userbInfo = await _context.Users.FirstOrDefaultAsync(x => x.Id == userBId);
if (userbInfo is null) throw new BaseException(CodeDefine.USER_NOT_FOUND);
Friend friendA = new Friend()
{
Avatar = userbInfo.Avatar,
Created = DateTime.UtcNow,
FriendId = userbInfo.Id,
RemarkName = remarkName ?? userbInfo.NickName,
StatusEnum = FriendStatus.Added,
UserId = userAId
};
_context.Friends.Add(friendA);
await _context.SaveChangesAsync();
}
}
#endregion
}
}