我通过创建事务名称createUser来添加partcipant。我的参与者由我使用uuid((随机生成的userid标识
您可以在这里看到我的用户伙伴模型和事务。
participant User identified by userId {
o String userId
o String name
o String email
o Strign password
o String isVerified
}
transaction createUser {
o String userId
o String name
o String email
o Strign password
o String isVerified
}
当我使用电子邮件作为标识符,同时添加两个具有相同电子邮件id的用户时,会抛出一个错误>>具有相同电子邮件id的用户已经存在。但在我的国家,大多数用户没有电子邮件。所以我决定创建一个具有随机userId的用户。
问题是选择userId作为标识符,我无法检查电子邮件id之前是否注册。
我的事务逻辑代码在这里是为了更好地理解。
const model = 'org.composer.app'
//generating random userId
function uuid() {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}
//create User Transaction logic
async function createUser (tx){
const userRegistry = await getParticipantRegistry(model+'.user.User')
const exist = await userRegistry.exists('tx.email')
if(exist){
throw new Error("already exist")
}else{
const user = getFactory().newResource(model+'.user','User',uuid())
user.name = tx.name
user.email = tx.email
user.password = tx.password
user.isVerified = tx.isVerified
await userRegistry.add(user)
}
}
========================================
根据评论和回答,我正在更新帖子。我认为电子邮件应该是党派性的,而不是资产,但不管怎样,人们都说它应该是资产,所以我把它作为资产保存在这里
asset Email identified by email {
o String email
}
participant User identified by userId {
o String userId
o String name
--> Email email
o Strign password
o String isVerified
}
transaction createUser {
o String userId
o String name
o String email
o Strign password
o String isVerified
}
const model = 'org.composer.app'
//generating random userId
function uuid() {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}
//create User Transaction logic
async function createUser (tx){
const userRegistry = await getParticipantRegistry(model+'.user.User')
const emailRegistry = await getAssetRigistry(model+'.email','Email')
const exist = emailRegistry.exist(tx.email)
if (exist){
throw new Error('Email already exist')
}else{
const user = getFactory().newResource(model+'.user','User',uuid())
user.name = tx.name
user.email = tx.email
user.password = tx.password
user.isVerified = tx.isVerified
await userRegistry.add(user)
}
}
您可以在此处使用查询:
const model = 'org.composer.app'
//generating random userId
function uuid() {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
}
//create User Transaction logic
async function createUser (tx){
const userRegistry = await getParticipantRegistry(model+'.user.User')
let mailQuery = buildQuery('SELECT org.composer.app.User WHERE (email == _$inputValue)');
let assets = await query(mailQuery, { inputValue: tx.email });
// if assets.length != 0 that means mail id already exist
if(assets.length != 0){
throw new Error("email already exist");
}else{
const user = getFactory().newResource(model+'.user','User',uuid());
user.name = tx.name;
user.email = tx.email;
user.password = tx.password;
user.isVerified = tx.isVerified;
await userRegistry.add(user);
}
}