|
|
@@ -29,93 +29,107 @@ app.post("/price", (req, res) => {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
-app.get("/users/:userId/assets", async (req, res) => {
|
|
|
- let irohaResponse = await fetchUsersAssets(req.params.userId);
|
|
|
+app.get("/users/:userId/assets", async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ let irohaResponse = await fetchUsersAssets(req.params.userId);
|
|
|
|
|
|
- res.status(irohaResponse.status);
|
|
|
+ res.status(irohaResponse.status);
|
|
|
|
|
|
- if (irohaResponse.headers.get("content-length") < 1) {
|
|
|
- res.send();
|
|
|
+ if (irohaResponse.headers.get("content-length") < 1) {
|
|
|
+ res.send();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ res.send(await irohaResponse.json());
|
|
|
+ }
|
|
|
}
|
|
|
- else {
|
|
|
- res.send(await irohaResponse.json());
|
|
|
+ catch (err) {
|
|
|
+ next(err);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
-app.get("/users/:userId/assets/:assetId", async (req, res) => {
|
|
|
- let assets = (await fetchUsersAssets(req.params.userId)).assets;
|
|
|
-
|
|
|
- let parsedAssetName = "";
|
|
|
- let i = 0;
|
|
|
- for (i; i < assets.length; i++) {
|
|
|
- parsedAssetName = assets[i].assetId.split('#')[0];
|
|
|
- if (parsedAssetName === req.params.assetId) {
|
|
|
- res.status(200);
|
|
|
- res.send(assets[i]);
|
|
|
- return;
|
|
|
+app.get("/users/:userId/assets/:assetId", async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ let assets = (await fetchUsersAssets(req.params.userId)).assets;
|
|
|
+
|
|
|
+ let parsedAssetName = "";
|
|
|
+ let i = 0;
|
|
|
+ for (i; i < assets.length; i++) {
|
|
|
+ parsedAssetName = assets[i].assetId.split('#')[0];
|
|
|
+ if (parsedAssetName === req.params.assetId) {
|
|
|
+ res.status(200);
|
|
|
+ res.send(assets[i]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- res.status(204);
|
|
|
- res.send();
|
|
|
+ res.status(204);
|
|
|
+ res.send();
|
|
|
+ } catch (err) {
|
|
|
+ next(err);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
-app.post("/buy", async (req, res) => {
|
|
|
-
|
|
|
- if (!req.body) {
|
|
|
- res.status(400);
|
|
|
- throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
|
|
|
- }
|
|
|
+app.post("/buy", async (req, res, next) => {
|
|
|
|
|
|
- if (!req.body.extent) {
|
|
|
- res.status(400);
|
|
|
- throw Error(JSON.stringify({ error: { name: "Error, request body has no \"extent\" property!" } }));
|
|
|
- }
|
|
|
+ try {
|
|
|
+ if (!req.body) {
|
|
|
+ res.status(400);
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request has no body!" } }));
|
|
|
+ }
|
|
|
|
|
|
- if (!req.body.user) {
|
|
|
- res.status(400);
|
|
|
- throw Error(JSON.stringify({ error: { name: "Error, request body has no \"user\" property!" } }));
|
|
|
- }
|
|
|
+ if (!req.body.extent) {
|
|
|
+ res.status(400);
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request body has no \"extent\" property!" } }));
|
|
|
+ }
|
|
|
|
|
|
- if (!req.body.privateKey) {
|
|
|
- res.status(400);
|
|
|
- throw Error(JSON.stringify({ error: { name: "Error, user must provide \"privateKey\" to sign transaction!" } }));
|
|
|
- }
|
|
|
+ if (!req.body.user) {
|
|
|
+ res.status(400);
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, request body has no \"user\" property!" } }));
|
|
|
+ }
|
|
|
|
|
|
- let price = getPrice(req.body.extent);
|
|
|
+ if (!req.body.privateKey) {
|
|
|
+ res.status(400);
|
|
|
+ throw Error(JSON.stringify({ error: { name: "Error, user must provide \"privateKey\" to sign transaction!" } }));
|
|
|
+ }
|
|
|
|
|
|
- let irohaResponse = await forwardBuyRequest(
|
|
|
- {
|
|
|
- privateKey: req.body.privateKey,
|
|
|
- transfers: [
|
|
|
- {
|
|
|
- asset: IROHA_ASSET + "#" + IROHA_DOMAIN,
|
|
|
- source: req.body.user + "@" + IROHA_DOMAIN,
|
|
|
- destination: DATA_OWNER + "@" + IROHA_DOMAIN,
|
|
|
- description: "test", //TODO: compose uniform description
|
|
|
- amount: price
|
|
|
- }
|
|
|
- ]
|
|
|
+ let price = getPrice(req.body.extent);
|
|
|
+
|
|
|
+ let irohaResponse = await forwardBuyRequest(
|
|
|
+ {
|
|
|
+ privateKey: req.body.privateKey,
|
|
|
+ transfers: [
|
|
|
+ {
|
|
|
+ asset: IROHA_ASSET + "#" + IROHA_DOMAIN,
|
|
|
+ source: req.body.user + "@" + IROHA_DOMAIN,
|
|
|
+ destination: DATA_OWNER + "@" + IROHA_DOMAIN,
|
|
|
+ description: "test", //TODO: compose uniform description
|
|
|
+ amount: price
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ let data = "test - to be implemented"; //TODO implement data extraction
|
|
|
+
|
|
|
+ res.status(irohaResponse.status);
|
|
|
+
|
|
|
+ if (!irohaResponse.ok) {
|
|
|
+ res.send(await irohaResponse.json());
|
|
|
+ return;
|
|
|
}
|
|
|
- );
|
|
|
|
|
|
- let data = "test - to be implemented"; //TODO implement data extraction
|
|
|
+ let irohaResponseJson = await irohaResponse.json();
|
|
|
|
|
|
- res.status(irohaResponse.status);
|
|
|
+ console.log("tx hash: " + irohaResponseJson.transactionHash);
|
|
|
|
|
|
- if (!irohaResponse.ok) {
|
|
|
- res.send(await irohaResponse.json());
|
|
|
- return;
|
|
|
+ res.send({
|
|
|
+ data: data,
|
|
|
+ transactionHash: irohaResponseJson.transactionHash
|
|
|
+ });
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ next(err);
|
|
|
}
|
|
|
-
|
|
|
- let irohaResponseJson = await irohaResponse.json();
|
|
|
-
|
|
|
- console.log("tx hash: " + irohaResponseJson.transactionHash);
|
|
|
-
|
|
|
- res.send({
|
|
|
- data : data,
|
|
|
- transactionHash : irohaResponseJson.transactionHash
|
|
|
- });
|
|
|
});
|
|
|
|
|
|
async function forwardBuyRequest(body) {
|
|
|
@@ -151,7 +165,7 @@ function getPrice(extent) {
|
|
|
|
|
|
function error(err, req, res, next) { //TODO: add custom Exception class
|
|
|
console.log(err);
|
|
|
- res.status(500);
|
|
|
+ res.status(500);
|
|
|
res.send(err.message);
|
|
|
}
|
|
|
|