Files
caliverse_server/BrokerApiTest/DocQuery/UserTestDoc.cs
2025-05-01 07:23:28 +09:00

91 lines
3.2 KiB
C#

using ServerCommon;
using ServerCore; using ServerBase;
namespace BrokerTest;
public sealed class UserTestDoc : DynamoDbDocBase
{
private const string m_prefix_of_pk = "user_test#";
private const string m_prefix_of_sk = "";
public UserTestDoc() : base(nameof(UserTestDoc))
{
// doc에 저장되는 attrib을 생성한다.
appendAttribWrapper(new AttribWrapper<UserTestAttrib>());
}
public UserTestDoc(string userId, string name = "") : base(nameof(UserTestDoc))
{
// doc에 저장되는 attrib을 생성한다.
appendAttribWrapper(new AttribWrapper<UserTestAttrib>());
// prefix를 제외한 key를 설정정한다.
setCombinationKeyForPK(userId);
// 파티션 키와 소트 키를 안전하게 설정한다.
// 이 과정에서 onCheckAndSetPK, onCheckAndSetSK 메소드가 호출된다.
fillUpPrimaryKey(onMakePK(), onMakeSK());
// 기본 값 설정
var attrib = getAttrib<UserTestAttrib>();
NullReferenceCheckHelper.throwIfNull(attrib, () => $"attrib is null !!! - {toBasicString()}");
attrib.UserId = userId;
attrib.Name = name;
}
public async Task<Result> insertUser(DynamoDbClient dynamoDbClient)
{
var result = await dynamoDbClient.simpleInsertDocumentWithDocType(this);
if (result.isFail())
{
var err_msg = $"Failed to insertUser() !!! : {result.toBasicString()}";
Log.getLogger().error(err_msg);
}
else
{
Log.getLogger().info($"Insert User Test !!! - {this.toBasicString()}");
}
return result;
}
public async Task<(Result, UserTestDoc?)> findOne(DynamoDbClient dynamoDbClient)
{
var config = dynamoDbClient.makeQueryConfigForReadByPKOnly(getPK());
var (result, result_doc) = await dynamoDbClient.simpleQueryDocTypeWithQueryOperationConfig<UserTestDoc>(config);
if (result.isFail())
{
var err_msg = $"Failed to simpleQueryDocTypesWithQueryOperationConfig() !!! : {result.toBasicString()}";
Log.getLogger().error(err_msg);
return (result, null);
}
return (result, result_doc);
}
public async Task<(Result, UserTestDoc?)> updateOne(DynamoDbClient dynamoDbClient)
{
setQueryType(QueryType.Update);
var result = await dynamoDbClient.simpleUpdateDocumentWithDocType(this);
if (result.isFail())
{
var err_msg = $"Failed to simpleUpdateDocumentWithDocType() !!! : {result.toBasicString()}";
Log.getLogger().error(err_msg);
return (result, null);
}
Log.getLogger().info($"Insert User Test !!! - {this.toBasicString()}");
return (result, this);
}
protected override string onGetPrefixOfPK() => m_prefix_of_pk;
protected override string onGetPrefixOfSK() => m_prefix_of_sk;
protected override ServerErrorCode onCheckAndSetPK(string partitionKey)
{
getPrimaryKey().fillUpPK(partitionKey);
return ServerErrorCode.Success;
}
protected override ServerErrorCode onCheckAndSetSK(string sortKey)
{
return ServerErrorCode.Success;
}
}