|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const { Contract } = require('fabric-contract-api'); |
| 8 | + |
| 9 | +class FabCar extends Contract { |
| 10 | + |
| 11 | + async initLedger(ctx) { |
| 12 | + console.info('============= START : Initialize Ledger ==========='); |
| 13 | + const cars = [ |
| 14 | + { |
| 15 | + color: 'blue', |
| 16 | + make: 'Toyota', |
| 17 | + model: 'Prius', |
| 18 | + owner: 'Tomoko', |
| 19 | + }, |
| 20 | + { |
| 21 | + color: 'red', |
| 22 | + make: 'Ford', |
| 23 | + model: 'Mustang', |
| 24 | + owner: 'Brad', |
| 25 | + }, |
| 26 | + { |
| 27 | + color: 'green', |
| 28 | + make: 'Hyundai', |
| 29 | + model: 'Tucson', |
| 30 | + owner: 'Jin Soo', |
| 31 | + }, |
| 32 | + { |
| 33 | + color: 'yellow', |
| 34 | + make: 'Volkswagen', |
| 35 | + model: 'Passat', |
| 36 | + owner: 'Max', |
| 37 | + }, |
| 38 | + { |
| 39 | + color: 'black', |
| 40 | + make: 'Tesla', |
| 41 | + model: 'S', |
| 42 | + owner: 'Adriana', |
| 43 | + }, |
| 44 | + { |
| 45 | + color: 'purple', |
| 46 | + make: 'Peugeot', |
| 47 | + model: '205', |
| 48 | + owner: 'Michel', |
| 49 | + }, |
| 50 | + { |
| 51 | + color: 'white', |
| 52 | + make: 'Chery', |
| 53 | + model: 'S22L', |
| 54 | + owner: 'Aarav', |
| 55 | + }, |
| 56 | + { |
| 57 | + color: 'violet', |
| 58 | + make: 'Fiat', |
| 59 | + model: 'Punto', |
| 60 | + owner: 'Pari', |
| 61 | + }, |
| 62 | + { |
| 63 | + color: 'indigo', |
| 64 | + make: 'Tata', |
| 65 | + model: 'Nano', |
| 66 | + owner: 'Valeria', |
| 67 | + }, |
| 68 | + { |
| 69 | + color: 'brown', |
| 70 | + make: 'Holden', |
| 71 | + model: 'Barina', |
| 72 | + owner: 'Shotaro', |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + for (let i = 0; i < cars.length; i++) { |
| 77 | + cars[i].docType = 'car'; |
| 78 | + await ctx.stub.putState('CAR' + i, Buffer.from(JSON.stringify(cars[i]))); |
| 79 | + console.info('Added <--> ', cars[i]); |
| 80 | + } |
| 81 | + console.info('============= END : Initialize Ledger ==========='); |
| 82 | + } |
| 83 | + |
| 84 | + async queryCar(ctx, carNumber) { |
| 85 | + const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state |
| 86 | + if (!carAsBytes || carAsBytes.length === 0) { |
| 87 | + throw new Error(`${carNumber} does not exist`); |
| 88 | + } |
| 89 | + console.log(carAsBytes.toString()); |
| 90 | + return carAsBytes.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + async createCar(ctx, carNumber, make, model, color, owner) { |
| 94 | + console.info('============= START : Create Car ==========='); |
| 95 | + |
| 96 | + const car = { |
| 97 | + color, |
| 98 | + docType: 'car', |
| 99 | + make, |
| 100 | + model, |
| 101 | + owner, |
| 102 | + }; |
| 103 | + |
| 104 | + await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car))); |
| 105 | + console.info('============= END : Create Car ==========='); |
| 106 | + } |
| 107 | + |
| 108 | + async queryAllCars(ctx) { |
| 109 | + const startKey = 'CAR0'; |
| 110 | + const endKey = 'CAR999'; |
| 111 | + |
| 112 | + const iterator = await ctx.stub.getStateByRange(startKey, endKey); |
| 113 | + |
| 114 | + const allResults = []; |
| 115 | + while (true) { |
| 116 | + const res = await iterator.next(); |
| 117 | + |
| 118 | + if (res.value && res.value.value.toString()) { |
| 119 | + console.log(res.value.value.toString('utf8')); |
| 120 | + |
| 121 | + const Key = res.value.key; |
| 122 | + let Record; |
| 123 | + try { |
| 124 | + Record = JSON.parse(res.value.value.toString('utf8')); |
| 125 | + } catch (err) { |
| 126 | + console.log(err); |
| 127 | + Record = res.value.value.toString('utf8'); |
| 128 | + } |
| 129 | + allResults.push({ Key, Record }); |
| 130 | + } |
| 131 | + if (res.done) { |
| 132 | + console.log('end of data'); |
| 133 | + await iterator.close(); |
| 134 | + console.info(allResults); |
| 135 | + return JSON.stringify(allResults); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + async changeCarOwner(ctx, carNumber, newOwner) { |
| 141 | + console.info('============= START : changeCarOwner ==========='); |
| 142 | + |
| 143 | + const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state |
| 144 | + if (!carAsBytes || carAsBytes.length === 0) { |
| 145 | + throw new Error(`${carNumber} does not exist`); |
| 146 | + } |
| 147 | + const car = JSON.parse(carAsBytes.toString()); |
| 148 | + car.owner = newOwner; |
| 149 | + |
| 150 | + await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car))); |
| 151 | + console.info('============= END : changeCarOwner ==========='); |
| 152 | + } |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +module.exports = FabCar; |
0 commit comments