feat: add Mapster mapping and update network tree request
All checks were successful
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m11s
All checks were successful
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m11s
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkTree;
|
||||
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Common.Mappings;
|
||||
|
||||
public class NetworkMembershipProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
// Request mapping
|
||||
config.NewConfig<GetNetworkTreeRequest, GetNetworkTreeQuery>()
|
||||
.Map(dest => dest.UserId, src => src.UserId)
|
||||
.Map(dest => dest.MaxDepth, src => src.MaxDepth != null && src.MaxDepth.Value > 0 ? src.MaxDepth.Value : 3);
|
||||
|
||||
// Response mapping: تبدیل درخت به لیست مسطح
|
||||
config.NewConfig<NetworkTreeDto, GetNetworkTreeResponse>()
|
||||
.MapWith(src => ConvertTreeToResponse(src));
|
||||
}
|
||||
|
||||
private static GetNetworkTreeResponse ConvertTreeToResponse(NetworkTreeDto? treeDto)
|
||||
{
|
||||
var response = new GetNetworkTreeResponse();
|
||||
|
||||
if (treeDto == null)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
// تبدیل درخت به لیست مسطح
|
||||
FlattenTree(treeDto, response.Nodes, null);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static void FlattenTree(NetworkTreeDto node, Google.Protobuf.Collections.RepeatedField<NetworkTreeNodeModel> nodesList, long? parentId)
|
||||
{
|
||||
var protoNode = new NetworkTreeNodeModel
|
||||
{
|
||||
UserId = node.UserId,
|
||||
UserName = $"{node.FirstName} {node.LastName}".Trim(),
|
||||
NetworkLeg = (int)(node.LegPosition ?? NetworkLeg.Left),
|
||||
NetworkLevel = node.CurrentDepth,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
if (parentId.HasValue)
|
||||
{
|
||||
protoNode.ParentId = parentId.Value;
|
||||
}
|
||||
|
||||
nodesList.Add(protoNode);
|
||||
|
||||
// بازگشتی برای فرزندان
|
||||
if (node.LeftChild != null)
|
||||
{
|
||||
FlattenTree(node.LeftChild, nodesList, node.UserId);
|
||||
}
|
||||
|
||||
if (node.RightChild != null)
|
||||
{
|
||||
FlattenTree(node.RightChild, nodesList, node.UserId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user