· 4 years ago · Jul 12, 2021, 12:58 AM
1const functions = require("firebase-functions");
2const admin = require("firebase-admin");
3const express = require("express");
4const cors = require("cors");
5const fetch = require("node-fetch");
6// initialize the app
7if (!admin.apps.length)
8 admin.initializeApp({
9 databaseURL: "http://localhost:9000?ns=demo-take-home-assignment",
10 });
11// // Create and Deploy Your First Cloud Functions
12// // https://firebase.google.com/docs/functions/write-firebase-functions
13const app = express();
14//middlewares
15// Automatically allow cross-origin requests
16app.use(cors({ origin: true }));
17app.use(express.json());
18
19//db config
20const db = admin.database();
21const ref = db.ref("assets");
22const topref = ref.child("top25");
23let top = [];
24
25/*
26 Challenge 1
27*/
28// YOUR CODE GOES HERE
29//API-routes
30app.get("/assets", (req, res) => {
31 top = [];
32 fetch(" http://api.coincap.io/v2/assets")
33 .then((response) => response.json())
34 .then(async (item) => {
35 //console.log(item.data)
36 item.data.slice(0, 25).forEach(async (data) => {
37 // console.log(data);
38 if (top.length < 25) {
39 await top.push(data.id);
40 }
41 });
42 await topref.set([top]);
43 res.status(200).send(item.data.slice(0, 25));
44
45 setInterval(async () => {
46 if (top != null) {
47 await priceTicker2();
48 }
49 }, 15000);
50 });
51});
52
53/*
54 Challenge 1 Comments:
55 COMMENT HERE
56 we can access the coinCaps api in front end using fetch module , axios,or node fetch etc using the react hooks like useEffect to load the api when the page loads.
57
58
59*/
60
61/*
62 Challenge 2
63*/
64// YOUR CODE GOES HERE
65
66// const priceTicker = () => {
67// topref.on("child_added", (snapshot) => {
68// let temp = snapshot.val();
69
70// temp.map((id, index) => {
71// fetch(`http://api.coincap.io/v2/assets/${id}`)
72// .then((response) => response.json())
73// .then((data) => {
74// const idref = ref.child(`${id}`);
75// idref.push(data.data);
76// });
77// });
78// });
79// };
80
81//setInterval(priceTicker, 10000);
82
83/*
84 Challenge 2 Comments:
85 COMMENT HERE
86 The push() method will create a uniuque id when data is pushed,this is used in situation where multiple items are added to same location parellely
87 where push helps to create a unique id for each Child, which removes the conflicts while addind childrens in same location
88
89 Transaction is other method of indexing where the key set ourown using some auto increment integer index for each child.
90 But Push() is more efficient than transaction. Transactions are slower and complex as we need to set the index and ordering explicitly,
91 where in push timestamps are used which ensures they are ordered chronologically,and the random bits ensure that each ID is unique,
92 even if thousands of people are creating push IDs at the same time.
93
94*/
95
96/*
97 Challenge 3
98*/
99// YOUR CODE GOES HERE
100
101async function getsetData(id) {
102 const data = await fetch(`http://api.coincap.io/v2/assets/${id}`).then(
103 (response) => response.json()
104 );
105
106 //await console.log(data.data);
107
108 const idref = ref.child(`${id}`);
109 await idref.push(data.data);
110 return;
111}
112
113async function priceTicker2() {
114 topref.on("child_added", async (snapshot) => {
115 let toplist = snapshot.val();
116 //console.log("length is", toplist.length);
117 for (i = 0; i < toplist.length; i += 5) {
118 for (j = i; j < i + 5; j++) {
119 if (toplist[j]) {
120 // await console.log(toplist[j]);
121 await getsetData(toplist[j]);
122 }
123 }
124 // console.log("Baaaatch");
125 }
126 });
127 return;
128}
129
130// Expose Express API as a single Cloud Function:
131exports.api = functions.https.onRequest(app);
132