添加项目文件。
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\IM.Commons\IM.Commons.csproj" />
|
||||
<ProjectReference Include="..\DomainCommons\IM.DomainCommons.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,54 @@
|
||||
using ContactService.Domain.Events;
|
||||
using ContactService.Domain.ValueObjects;
|
||||
using IM.DomainCommons;
|
||||
|
||||
namespace ContactService.Domain.Entities
|
||||
{
|
||||
public class Friend : AggregateRootEntity
|
||||
{
|
||||
public UserProfile Owner { get; private set; }
|
||||
public UserProfile Target { get; private set; }
|
||||
/// <summary>
|
||||
/// 好友备注名
|
||||
/// </summary>
|
||||
public string? RemarkName { get; private set; }
|
||||
public FriendStatus Status { get; private set; }
|
||||
|
||||
private Friend() { }
|
||||
|
||||
public Friend(UserProfile owner, UserProfile target, string? remarkName)
|
||||
{
|
||||
Owner = owner;
|
||||
Target = target;
|
||||
RemarkName = remarkName;
|
||||
Status = FriendStatus.Added;
|
||||
|
||||
AddDomainEvent(new FriendAddedDomainEvent(this));
|
||||
}
|
||||
|
||||
public void setRemarkName(string? remarkName)
|
||||
{
|
||||
if (remarkName.Length > 20)
|
||||
{
|
||||
throw new DomainException("备注名过长");
|
||||
}
|
||||
RemarkName = remarkName ?? RemarkName;
|
||||
}
|
||||
|
||||
public void Block()
|
||||
{
|
||||
Status = FriendStatus.Blocked;
|
||||
AddDomainEvent(new FriendBlockDomainEvent(this));
|
||||
}
|
||||
|
||||
public void UpdateUserInfo(UserProfile profile)
|
||||
{
|
||||
if (profile.Id != Target.Id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Target = profile;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using ContactService.Domain.Events;
|
||||
using IM.DomainCommons;
|
||||
|
||||
namespace ContactService.Domain.Entities
|
||||
{
|
||||
public class FriendRequest : AggregateRootEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 申请人
|
||||
/// </summary>
|
||||
public Guid OwnerId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被申请人
|
||||
/// </summary>
|
||||
public Guid TargetId { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 申请附言
|
||||
/// </summary>
|
||||
public string Description { get; private set; } = "申请添加好友";
|
||||
|
||||
/// <summary>
|
||||
/// 申请状态(0:待通过,1:拒绝,2:同意,3:拉黑)
|
||||
/// </summary>
|
||||
public FriendRequestStatus State { get; private set; } = FriendRequestStatus.Pending;
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? RemarkName { get; private set; }
|
||||
|
||||
private FriendRequest() { }
|
||||
|
||||
public FriendRequest(Guid ownerId, Guid targetId, string? description, string? remarkName)
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
TargetId = targetId;
|
||||
Description = description ?? Description;
|
||||
RemarkName = remarkName;
|
||||
AddDomainEvent(new FriendRequestCreatedDomainEvent(this));
|
||||
}
|
||||
public void Accept(string remarkName)
|
||||
{
|
||||
if (State != FriendRequestStatus.Pending)
|
||||
{
|
||||
throw new DomainException("只能处理待处理的好友请求");
|
||||
}
|
||||
State = FriendRequestStatus.Passed;
|
||||
AddDomainEvent(new FriendRequestStateUpdateDomainEvent(this,remarkName));
|
||||
}
|
||||
|
||||
public void Reject()
|
||||
{
|
||||
if (State != FriendRequestStatus.Pending)
|
||||
{
|
||||
throw new DomainException("只能处理待处理的好友请求");
|
||||
}
|
||||
State = FriendRequestStatus.Declined;
|
||||
AddDomainEvent(new FriendRequestStateUpdateDomainEvent(this));
|
||||
}
|
||||
public void Block()
|
||||
{
|
||||
if (State != FriendRequestStatus.Pending)
|
||||
{
|
||||
throw new DomainException("只能处理待处理的好友请求");
|
||||
}
|
||||
State = FriendRequestStatus.Blocked;
|
||||
AddDomainEvent(new FriendRequestStateUpdateDomainEvent(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace ContactService.Domain.Events
|
||||
{
|
||||
public record FriendAddedDomainEvent(Friend Friend) : INotification;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace ContactService.Domain.Events
|
||||
{
|
||||
public record FriendBlockDomainEvent(Friend Friend) : INotification;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace ContactService.Domain.Events
|
||||
{
|
||||
public record FriendRequestCreatedDomainEvent(FriendRequest Request) : INotification;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace ContactService.Domain.Events
|
||||
{
|
||||
public record FriendRequestStateUpdateDomainEvent(FriendRequest Request,string? AcceptRemarkName = default) : INotification;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using ContactService.Domain.ValueObjects;
|
||||
using IM.Commons;
|
||||
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public class FriendDomainService
|
||||
{
|
||||
private readonly IFriendReposity reposity;
|
||||
|
||||
public FriendDomainService(IFriendReposity reposity)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
}
|
||||
|
||||
public async Task<Result<Friend?>> CreateAsync(UserProfile owner, UserProfile target, string? remarkName)
|
||||
{
|
||||
var isExist = await reposity.CheckOwnerIdAndTargetIdAsync(owner.Id, target.Id);
|
||||
if (isExist)
|
||||
{
|
||||
return Result<Friend?>.Fail(ResultCode.ALREADY_FRIENDS);
|
||||
}
|
||||
var friend = new Friend(owner, target, remarkName);
|
||||
var res = await reposity.CreateAsync(friend);
|
||||
return Result<Friend?>.Success(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using ContactService.Domain.Entities;
|
||||
using IM.Commons;
|
||||
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public class FriendRequestDomainService
|
||||
{
|
||||
private readonly IFriendRequestReposity reposity;
|
||||
|
||||
public FriendRequestDomainService(IFriendRequestReposity reposity)
|
||||
{
|
||||
this.reposity = reposity;
|
||||
}
|
||||
|
||||
public async Task<Result<FriendRequest?>> CreateAsync(Guid ownerId, Guid targetId, string? description, string? remarkName)
|
||||
{
|
||||
var friendRequest = new FriendRequest(ownerId, targetId, description, remarkName);
|
||||
await reposity.CreateAsync(friendRequest);
|
||||
return Result<FriendRequest?>.Success(friendRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public enum FriendRequestStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 待处理
|
||||
/// </summary>
|
||||
Pending = 0,
|
||||
/// <summary>
|
||||
/// 已通过
|
||||
/// </summary>
|
||||
Passed = 2,
|
||||
/// <summary>
|
||||
/// 已拒绝
|
||||
/// </summary>
|
||||
Declined = 1,
|
||||
/// <summary>
|
||||
/// 拉黑
|
||||
/// </summary>
|
||||
Blocked = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public enum FriendStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 待处理
|
||||
/// </summary>
|
||||
Pending = 0,
|
||||
/// <summary>
|
||||
/// 已添加
|
||||
/// </summary>
|
||||
Added = 1,
|
||||
/// <summary>
|
||||
/// 已拒绝
|
||||
/// </summary>
|
||||
Declined = 2,
|
||||
/// <summary>
|
||||
/// 已拉黑
|
||||
/// </summary>
|
||||
Blocked = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using ContactService.Domain.Entities;
|
||||
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public interface IFriendReposity
|
||||
{
|
||||
Task<Friend?> FindByIdAsync(Guid id);
|
||||
Task<Friend?> FindByOwnerAndTargetAsync(Guid ownerId, Guid targetId);
|
||||
Task<IEnumerable<Friend>> FindByTargetAsync(Guid targetId);
|
||||
Task<IEnumerable<Friend>> FindByOwnerAsync(Guid ownerId);
|
||||
|
||||
Task<Friend> CreateAsync(Friend friend);
|
||||
|
||||
Task<bool> CheckOwnerIdAndTargetIdAsync(Guid ownerId, Guid targetId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using ContactService.Domain.Entities;
|
||||
|
||||
namespace ContactService.Domain
|
||||
{
|
||||
public interface IFriendRequestReposity
|
||||
{
|
||||
Task<bool> CreateAsync(FriendRequest friendRequest);
|
||||
Task<FriendRequest?> FindByIdAsync(Guid id);
|
||||
Task<IEnumerable<FriendRequest>> FindByOwnerIdAsync(Guid ownerId);
|
||||
Task<IEnumerable<FriendRequest>> FindByTargetIdAsync(Guid targetId);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace ContactService.Domain.ValueObjects
|
||||
{
|
||||
public class UserProfile
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public string NickName { get; private set; }
|
||||
public string? Avatar { get; private set; }
|
||||
public UserProfile() { }
|
||||
|
||||
public UserProfile(Guid id, string nickName, string? avatar)
|
||||
{
|
||||
Id = id;
|
||||
NickName = nickName;
|
||||
Avatar = avatar;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user