/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Contract } = require('fabric-contract-api');
class FabCar extends Contract {
async initLedger(ctx) {
console.info('============= START : Initialize Ledger ===========');
console.info(ctx);
const cars = [
{
color: 'blue',
make: 'Toyota',
model: 'Prius',
owner: 'Tomoko',
},
{
color: 'red',
make: 'Ford',
model: 'Mustang',
owner: 'Brad',
},
{
color: 'green',
make: 'Hyundai',
model: 'Tucson',
owner: 'Jin Soo',
},
{
color: 'yellow',
make: 'Volkswagen',
model: 'Passat',
owner: 'Max',
},
{
color: 'black',
make: 'Tesla',
model: 'S',
owner: 'Adriana',
},
{
color: 'purple',
make: 'Peugeot',
model: '205',
owner: 'Michel',
},
{
color: 'white',
make: 'Chery',
model: 'S22L',
owner: 'Aarav',
},
{
color: 'violet',
make: 'Fiat',
model: 'Punto',
owner: 'Pari',
},
{
color: 'indigo',
make: 'Tata',
model: 'Nano',
owner: 'Valeria',
},
{
color: 'brown',
make: 'Holden',
model: 'Barina',
owner: 'Shotaro',
},
];
for (let i = 0; i < cars.length; i++) {
cars[i].docType = 'car';
await ctx.stub.putState('CAR' + i, Buffer.from(JSON.stringify(cars[i])));
console.info('Added <--> ', cars[i]);
}
console.info('============= END : Initialize Ledger ===========');
}
async queryCar(ctx, carNumber) {
const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
console.log(carAsBytes.toString());
return carAsBytes.toString();
}
async createCar(ctx, carNumber, make, model, color, owner) {
console.info('============= START : Create Car ===========');
// console.info(thisClass);
console.info(ctx);
// args 유효성 체크
// let method = thisClass['checkArgs'];
// await method(args, carNumber, 5);
// response 메세지 설정
// let method = thisClass['responseMsg'];
const car = {
color,
docType: 'car',
make,
model,
owner,
};
console.info("---responseMsg---");
await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
const response = {
status : '14001',
message: 'Success Asset Registration'
};
console.info('============= END : Create Car ===========');
return Buffer.from(JSON.stringify(response));
}
async queryAllCars(ctx) {
const startKey = 'CAR0';
const endKey = 'CAR999';
const allResults = [];
for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) {
const strValue = Buffer.from(value).toString('utf8');
let record;
try {
record = JSON.parse(strValue);
} catch (err) {
console.log(err);
record = strValue;
}
allResults.push({ Key: key, Record: record });
}
console.info(allResults);
return JSON.stringify(allResults);
}
async changeCarOwner(ctx, carNumber, newOwner) {
console.info('============= START : changeCarOwner ===========');
const carAsBytes = await ctx.stub.getState(carNumber); // get the car from chaincode state
if (!carAsBytes || carAsBytes.length === 0) {
throw new Error(`${carNumber} does not exist`);
}
const car = JSON.parse(carAsBytes.toString());
car.owner = newOwner;
await ctx.stub.putState(carNumber, Buffer.from(JSON.stringify(car)));
console.info('============= END : changeCarOwner ===========');
}
// Argument 유효성 검사
async checkArgs(args, key, length) {
var err = {};
// argument 개수 체크
if (args.length != length) {
err.status = 14011;
err.message = 'Incorrect number of arguments. Expecting ' + length;
throw new Error("#" + JSON.stringify(err));
// key 값 null 체크
}else if(!key.toString()) {
err.status = 14010;
err.message = 'Key value doesn\'t exist';
throw new Error("#" + JSON.stringify(err));
}
}
async responseMsg(str, num, n) {
console.info("---responseMsg---");
var err = {};
var success = {};
if (str == 'success') {
switch (num) {
case 14001:
success.status = 14001;
success.message = 'Success Asset Registration';
break;
case 14002:
success.status = 14002;
success.message = 'Success Transaction Registration';
break;
case 14003:
success.status = 14003;
success.message = 'Success Asset Transference';
break;
}
return success;
} else if (str == 'err') {
switch (num) {
case 14010:
err.status = 14010;
err.message = 'Key value doesn\'t exist';
break;
case 14011:
err.status = 14011;
err.message = 'Incorrect number of arguments. Expecting ' + n;
break;
case 14013:
err.status = 14013;
err.message = 'There is no Asset Information';
break;
case 14020:
err.status = 14020;
err.message = 'Item does not exist';
}
throw new Error("#" + JSON.stringify(err));
}
}
}
module.exports = FabCar;
'일기 혹은 일지 > 업무관련' 카테고리의 다른 글
HLF2.0 fabric 체인코드 node로 설치시 에러 (0) | 2020.07.02 |
---|---|
iOS 인증서 갱신 (0) | 2020.06.01 |
엑셀함수 (0) | 2020.02.18 |
[Elasticsearch] Index vs Indices (0) | 2019.11.25 |
Elasticsearch.yml (0) | 2019.11.25 |