· 4 years ago · Jul 12, 2021, 03:30 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");
23
24let top25 = [];
25let myInterval;
26
27/*
28 Challenge 1
29*/
30// YOUR CODE GOES HERE
31//API-routes
32app.get("/assets", (req, res) => {
33 clearInterval(myInterval);
34 top25 = [];
35 fetch(" http://api.coincap.io/v2/assets")
36 .then((response) => response.json())
37 .then(async (item) => {
38 //console.log(item.data)
39 item.data.slice(0, 25).forEach(async (data) => {
40 // console.log(data);
41 if (top25.length < 25) {
42 await top25.push(data.id);
43 }
44 });
45 await topref.set([top25]);
46 res.status(200).send(item.data.slice(0, 25));
47
48 myInterval = setInterval(async () => {
49 await priceTicker2();
50 }, 15000);
51 });
52});
53
54/*
55 Challenge 1 Comments:
56 COMMENT HERE
57 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.
58
59
60*/
61
62/*
63 Challenge 2
64*/
65// YOUR CODE GOES HERE
66
67// const priceTicker = () => {
68// topref.on("child_added", (snapshot) => {
69// let temp = snapshot.val();
70
71// temp.map((id, index) => {
72// fetch(`http://api.coincap.io/v2/assets/${id}`)
73// .then((response) => response.json())
74// .then((data) => {
75// const idref = ref.child(`${id}`);
76// idref.push(data.data);
77// });
78// });
79// });
80// };
81
82//setInterval(priceTicker, 10000);
83
84/*
85 Challenge 2 Comments:
86 COMMENT HERE
87 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
88 where push helps to create a unique id for each Child, which removes the conflicts while addind childrens in same location
89
90 Transaction is other method of indexing where the key set ourown using some auto increment integer index for each child.
91 But Push() is more efficient than transaction. Transactions are slower and complex as we need to set the index and ordering explicitly,
92 where in push timestamps are used which ensures they are ordered chronologically,and the random bits ensure that each ID is unique,
93 even if thousands of people are creating push IDs at the same time.
94
95*/
96
97/*
98 Challenge 3
99*/
100// YOUR CODE GOES HERE
101
102async function getsetData(id) {
103 const data = await fetch(`http://api.coincap.io/v2/assets/${id}`).then(
104 (response) => response.json()
105 );
106
107 // await console.log(data.data);
108
109 const idref = ref.child(`${id}`);
110 await idref.push(data.data);
111 return;
112}
113
114async function priceTicker2() {
115 topref.on("child_added", async (snapshot) => {
116 let toplist = snapshot.val();
117 //console.log("length is", toplist.length);
118
119 for (i = 0; i < toplist.length; i += 5) {
120 if (top25.length != 0) {
121 for (j = i; j < i + 5; j++) {
122 if (toplist[j] && top25.length != 0) {
123 // await console.log(toplist[j]);
124 await getsetData(toplist[j]);
125 } else {
126 break;
127 }
128 }
129 } else {
130 break;
131 }
132 }
133 });
134 return;
135}
136
137// Expose Express API as a single Cloud Function:
138exports.api = functions.https.onRequest(app);
139