Add get all assets endpoint

Signed-off-by: James Taylor <jamest@uk.ibm.com>
This commit is contained in:
James Taylor 2021-07-15 12:08:29 +01:00
parent d810128fcd
commit 400367e8a6
2 changed files with 34 additions and 7 deletions

View file

@ -8,6 +8,8 @@ The REST API is intended to work with the [basic asset transfer example](https:/
To install the basic asset transfer chaincode on a local Fabric network, follow the [Using the Fabric test network](https://hyperledger-fabric.readthedocs.io/en/release-2.2/test_network.html) tutorial
**Note:** these instructions should work with the release-2.2 branch of `fabric-samples` but later versions require some changes
To build and start the sample REST server, you'll need to [download and install an LTS version of node](https://nodejs.org/en/download/)
Clone this repository and change to the `fabric-rest-sample/asset-transfer-basic/rest-api-typescript` directory before running the following commands
@ -44,38 +46,44 @@ npm run start:dev
If everything went well, you can now make REST calls!
For example, check whether an asset exists...
For example, get all assets...
```shell
curl -v -X OPTIONS http://localhost:3000/api/assets/asset7
curl http://localhost:3000/api/assets
```
Check whether an asset exists...
```shell
curl --include --request OPTIONS http://localhost:3000/api/assets/asset7
```
Create an asset...
```shell
curl --header "Content-Type: application/json" --request POST --data '{"id":"asset7","color":"red","size":42,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets
curl --include --header "Content-Type: application/json" --request POST --data '{"id":"asset7","color":"red","size":42,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets
```
Read an asset...
```shell
curl -v http://localhost:3000/api/assets/asset7
curl http://localhost:3000/api/assets/asset7
```
Update an asset...
```shell
curl --header "Content-Type: application/json" --request PUT --data '{"id":"asset7","color":"red","size":11,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets/asset7
curl --include --header "Content-Type: application/json" --request PUT --data '{"id":"asset7","color":"red","size":11,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets/asset7
```
Transfer an asset...
```shell
curl --header "Content-Type: application/json" --request PATCH --data '[{"op":"replace","path":"/owner","value":"Ashleigh"}]' http://localhost:3000/api/assets/asset7
curl --include --header "Content-Type: application/json" --request PATCH --data '[{"op":"replace","path":"/owner","value":"Ashleigh"}]' http://localhost:3000/api/assets/asset7
```
Delete an asset...
```shell
curl -v -X DELETE http://localhost:3000/api/assets/asset7
curl --include --request DELETE http://localhost:3000/api/assets/asset7
```

View file

@ -27,6 +27,25 @@ const { ACCEPTED, BAD_REQUEST, INTERNAL_SERVER_ERROR, NOT_FOUND, OK } =
export const assetsRouter = express.Router();
assetsRouter.get('/', async (req: Request, res: Response) => {
logger.debug('Get all assets request received');
try {
const contract: Contract = req.app.get('contract');
const data = await contract.evaluateTransaction('GetAllAssets');
const assets = JSON.parse(data.toString());
return res.status(OK).json(assets);
} catch (err) {
logger.error(err, 'Error processing get all assets request');
return res.status(INTERNAL_SERVER_ERROR).json({
status: getReasonPhrase(INTERNAL_SERVER_ERROR),
timestamp: new Date().toISOString(),
});
}
});
assetsRouter.post(
'/',
body().isObject().withMessage('body must contain an asset object'),