Selaa lähdekoodia

no conetent handling added

kunickyd 3 vuotta sitten
vanhempi
commit
6d1082a3ac
1 muutettua tiedostoa jossa 28 lisäystä ja 12 poistoa
  1. 28 12
      app.js

+ 28 - 12
app.js

@@ -6,9 +6,9 @@ const port = 3000;
 
 //TODO load these values from setting or env
 
-const irohaApiHost = "http://localhost";
-const irohaApiPort = 5000;
-const irohaDomain = "test";
+const IROHA_API_HOST = "http://localhost";
+const IROHA_API_PORT = 5000;
+const IROHA_DOMAIN = "test";
 
 app.post("/price", (req, res) => {
     //TODO implement price calculation depending on extent area
@@ -19,8 +19,19 @@ app.post("/price", (req, res) => {
 
 app.get("/users/:userId/assets", async (req, res) => {
     
-    try{        
-        res.send(await getUsersAssets(req.params.userId));
+    try{
+        let irohaResponse = await fetchUsersAssets(req.params.userId);
+
+        res.status(irohaResponse.status);
+
+        if(irohaResponse.headers.get("content-length") < 1){            
+            res.send();
+        }
+        else{                        
+            res.send(await irohaResponse.json());
+        }
+
+        
     }
     catch(error){
         console.error(error);
@@ -32,15 +43,21 @@ app.get("/users/:userId/assets", async (req, res) => {
 app.get("/users/:userId/assets/:assetId", async (req, res) => {
 
     try{
-        let response = await getUsersAssets(req.params.userId);
+        let assets = (await fetchUsersAssets(req.params.userId)).assets;
         
+        let parsedAssetName = "";
         let i = 0;
-        for(i; i < response.assets.length; i++){            
-            if(response.assets[i].assetId.startsWith(req.params.assetId)){
-                res.send(response.assets[i]);
+        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();
     }
     catch(error){
         console.error(error);
@@ -58,7 +75,6 @@ app.listen(port, () => {
     console.log(`Listening at http://localhost:${port}`)
 });
 
-async function getUsersAssets(userId){    
-    let response = await fetch(irohaApiHost + ':' + irohaApiPort + '/accounts/' + userId + '@' + irohaDomain + '/assets/');
-    return await response.json();
+async function fetchUsersAssets(userId){    
+    return await fetch(IROHA_API_HOST + ':' + IROHA_API_PORT + '/accounts/' + userId + '@' + IROHA_DOMAIN + '/assets/');    
 }