Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vikrampatel0408 committed Oct 5, 2023
1 parent c026be0 commit e6981d8
Show file tree
Hide file tree
Showing 20 changed files with 10,638 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Existing_API_Collection/CryptoCurrenciesAPIs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Holdlinfo

## git clone the repository
## Go to file
## configure Database
## write on terminal npm start
## go to browser and search http://localhost:8000/api/view/wazirx/data to view data
if data is not added search http://localhost:8000/api/add/wazirx/data
19 changes: 19 additions & 0 deletions Existing_API_Collection/CryptoCurrenciesAPIs/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const bodyParser = require('body-parser')
const path = require('path')
global.appRootPath = __dirname
const app = express();


app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
/* global appRootPath */
app.use(express.static(path.join(appRootPath, 'public')))
app.set('view engine', 'ejs')
app.use('/', require('./routes'))
require('dotenv').config();


app.listen(process.env.PORT, () => {
console.log('Magic happens on port :' + process.env.PORT)
});
68 changes: 68 additions & 0 deletions Existing_API_Collection/CryptoCurrenciesAPIs/controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const axios = require('axios')
require('dotenv').config()
const CurrencyModel = require('./model')
const fs = require('fs')
const ejs = require('ejs')

async function addWazirxData(req, res) {
try {

// Take Data from WazirX for Data
let response = await axios.get(process.env.WAZIR_URL)
response = Object.values(response.data).slice(0,10)

// Create DB Object

const aCurrencyData = []
for (const data of response) {
aCurrencyData.push({
sBaseUnit: data.base_unit,
sQuoteUnit: data.quote_unit,
sLowPrice: data.low,
sHighPrice: data.high,
sLastPrice: data.last,
sType: data.type,
sOpenPrice: data.open,
sVolume: data.volume,
sSell: data.sell,
sBuy: data.buy,
sName: data.name
})
}

// Insert WazirX Data in Bulk
const result = await CurrencyModel.create(aCurrencyData)

res.status(201).send(result)
} catch (error) {
console.log(error)
throw Error(error)
}
}

async function viewWazirxData(req, res) {
try {
// Fetch Data from DB
const aCurrencyData = await CurrencyModel.find({}).limit(10).lean()
console.log(aCurrencyData[0])
var iAverage;
var iSum= 0 ;
aCurrencyData.forEach(element => {
iSum += parseInt(element.sBuy);
});
iAverage = iSum/10;
const template = fs.readFileSync('views/' + 'home.ejs', {
encoding: 'utf-8' // Unicode Transformation Format (UTF).
})
const renderedData = ejs.render(template, { average:iAverage, response: aCurrencyData, title: 'Vikram Patel' })
return res.status(201).send(renderedData)
} catch (error) {
console.log(error)
throw Error(error)
}
}

async function test(req, res) {
console.log('test')
}
module.exports = { addWazirxData, viewWazirxData, test }
19 changes: 19 additions & 0 deletions Existing_API_Collection/CryptoCurrenciesAPIs/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mongoose = require('mongoose')


const dbConnect = connection(process.env.DB_URL, parseInt(process.env.DB_POOL_SIZE), 'internship')
function connection(DB_URL, maxPoolSize = 10, DB) {
try {
const dbConfig = { useNewUrlParser: true, useUnifiedTopology: true, readPreference: 'secondaryPreferred' }

const conn = mongoose.createConnection(DB_URL, dbConfig)
conn.on('connected', () => console.log(`Connected to ${DB} database.`))
return conn
} catch (error) {
handleCatchError(error)
}
}

module.exports = {
dbConnect
}
24 changes: 24 additions & 0 deletions Existing_API_Collection/CryptoCurrenciesAPIs/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const { dbConnect } = require('./database')

const Currency = new Schema({
// Currency Data
sBaseUnit: { type: String },
sQuoteUnit: { type: String },
sLowPrice: { type: String },
sHighPrice: { type: String },
sLastPrice: { type: String },
sType: { type: String },
sOpenPrice: { type: String || Number },
sVolume: { type: String },
sSell: { type: String },
sBuy: { type: String },
sName: { type: String }
},
{ timestamps: { createdAt: 'dCreatedAt', updatedAt: 'dUpdatedAt' } })


const CurrencyModel = dbConnect.model('currencies', Currency)

module.exports = CurrencyModel;
Loading

0 comments on commit e6981d8

Please sign in to comment.