Merge "[FAB-16607] Update FabCar to reflect CC updates"

This commit is contained in:
Matthew White 2019-09-19 13:01:35 +00:00 committed by Gerrit Code Review
commit 7010c503ac
3 changed files with 24 additions and 50 deletions

View file

@ -30,7 +30,7 @@
<property name="fileExtensions" value="java, properties, xml"/> <property name="fileExtensions" value="java, properties, xml"/>
<module name="SuppressionFilter"> <module name="SuppressionFilter">
<property name="file" value="config/checkstyle/suppressions.xml"/> <property name="file" value="${config_loc}/suppressions.xml"/>
<property name="optional" value="false"/> <property name="optional" value="false"/>
</module> </module>

View file

@ -108,33 +108,20 @@ class FabCar extends Contract {
async queryAllCars(ctx) { async queryAllCars(ctx) {
const startKey = 'CAR0'; const startKey = 'CAR0';
const endKey = 'CAR999'; const endKey = 'CAR999';
const iterator = await ctx.stub.getStateByRange(startKey, endKey);
const allResults = []; const allResults = [];
while (true) { for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) {
const res = await iterator.next(); const strValue = Buffer.from(value).toString('utf8');
let record;
if (res.value && res.value.value.toString()) { try {
console.log(res.value.value.toString('utf8')); record = JSON.parse(strValue);
} catch (err) {
const Key = res.value.key; console.log(err);
let Record; record = strValue;
try {
Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
Record = res.value.value.toString('utf8');
}
allResults.push({ Key, Record });
}
if (res.done) {
console.log('end of data');
await iterator.close();
console.info(allResults);
return JSON.stringify(allResults);
} }
allResults.push({ Key: key, Record: record });
} }
console.info(allResults);
return JSON.stringify(allResults);
} }
async changeCarOwner(ctx, carNumber, newOwner) { async changeCarOwner(ctx, carNumber, newOwner) {

View file

@ -107,33 +107,20 @@ export class FabCar extends Contract {
public async queryAllCars(ctx: Context): Promise<string> { public async queryAllCars(ctx: Context): Promise<string> {
const startKey = 'CAR0'; const startKey = 'CAR0';
const endKey = 'CAR999'; const endKey = 'CAR999';
const iterator = await ctx.stub.getStateByRange(startKey, endKey);
const allResults = []; const allResults = [];
while (true) { for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) {
const res = await iterator.next(); const strValue = Buffer.from(value).toString('utf8');
let record;
if (res.value && res.value.value.toString()) { try {
console.log(res.value.value.toString('utf8')); record = JSON.parse(strValue);
} catch (err) {
const Key = res.value.key; console.log(err);
let Record; record = strValue;
try {
Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
Record = res.value.value.toString('utf8');
}
allResults.push({ Key, Record });
}
if (res.done) {
console.log('end of data');
await iterator.close();
console.info(allResults);
return JSON.stringify(allResults);
} }
allResults.push({ Key: key, Record: record });
} }
console.info(allResults);
return JSON.stringify(allResults);
} }
public async changeCarOwner(ctx: Context, carNumber: string, newOwner: string) { public async changeCarOwner(ctx: Context, carNumber: string, newOwner: string) {