From fb2bcb309e29dfca9b18855238c7bfdea89df389 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Fri, 16 Jul 2021 15:54:54 +0100 Subject: [PATCH] Update CreateAsset txn in basic js and ts sample The Golang and Java sample chaincode returned an error when trying to create an asset which already exists JavaScript and TypeScript samples should now do the same Signed-off-by: James Taylor --- .../chaincode-javascript/lib/assetTransfer.js | 5 +++++ .../chaincode-typescript/src/assetTransfer.ts | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js index 821e413a..a1a27aa4 100644 --- a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js +++ b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js @@ -65,6 +65,11 @@ class AssetTransfer extends Contract { // CreateAsset issues a new asset to the world state with given details. async CreateAsset(ctx, id, color, size, owner, appraisedValue) { + const exists = await this.AssetExists(ctx, id); + if (exists) { + throw new Error(`The asset ${id} already exists`); + } + const asset = { ID: id, Color: color, diff --git a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts index b21f12ec..55b19b77 100644 --- a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts +++ b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts @@ -65,6 +65,11 @@ export class AssetTransferContract extends Contract { // CreateAsset issues a new asset to the world state with given details. @Transaction() public async CreateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number): Promise { + const exists = await this.AssetExists(ctx, id); + if (exists) { + throw new Error(`The asset ${id} already exists`); + } + const asset = { ID: id, Color: color,