Files
caliverse_server/ServerCommon/Helper/DataCopyHelper.cs
2025-05-01 07:20:41 +09:00

83 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerCore;
using ServerBase;
using MetaAssets;
namespace ServerCommon;
public static class DataCopyHelper
{
#region : Meta => Doc
public static async Task<Result> copyDocFromMetas<TDoc>(TDoc toDoc, List<MetaAssets.IMetaData> fromMetaDatas)
where TDoc : DynamoDbDocBase
{
var result = new Result();
var err_msg = string.Empty;
var to_doc = toDoc as ICopyDocFromMeta;
if (null == to_doc)
{
err_msg = $"Failed to cast ICopyDocFromMeta !!! : {typeof(TDoc).Name}";
result.setFail(ServerErrorCode.ClassDoesNotImplementInterfaceInheritance, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
foreach (var meta_data in fromMetaDatas)
{
if (false == to_doc.copyDocFromMeta(meta_data))
{
err_msg = $"Failed to copyDocFromMeta() !!!, to:{typeof(TDoc).Name}, from:{meta_data.getTypeName()}";
result.setFail(ServerErrorCode.MetaDataCopyToDynamoDbDocFailed, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
}
return await Task.FromResult(result);
}
#endregion
#region : Meta => EntityAttribute
public static async Task<Result> copyEntityAttributeFromMetas<TEntityAttributeType>(TEntityAttributeType toDocBase, List<MetaAssets.IMetaData> metaDatas)
where TEntityAttributeType : EntityAttributeBase
{
var result = new Result();
var err_msg = string.Empty;
var to_copy = toDocBase as ICopyEntityAttributeFromMeta;
if (null == to_copy)
{
err_msg = $"Failed to cast ICopyEntityAttributeFromMeta !!! : {typeof(TEntityAttributeType).Name} !!!";
result.setFail(ServerErrorCode.ClassDoesNotImplementInterfaceInheritance, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
foreach (var meta_data in metaDatas)
{
if (false == to_copy.copyEntityAttributeFromMeta(meta_data))
{
err_msg = $"Failed to copyEntityAttributeFromMeta() !!!, to:{typeof(TEntityAttributeType).Name}, from:{meta_data.getTypeName()}";
result.setFail(ServerErrorCode.MetaDataCopyToEntityAttributeFailed, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
}
return await Task.FromResult(result);
}
#endregion
}