-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented appending record to database
only base functionality solves #66
- Loading branch information
Showing
5 changed files
with
97 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mongoose from "mongoose"; | ||
const Schema = mongoose.Schema; | ||
|
||
const RecordSchema = new Schema( | ||
{ | ||
date: Number, | ||
quotes: [ | ||
{ | ||
baseCurrency: String, | ||
rates: [{ currency: String, rate: Number }], | ||
}, | ||
], | ||
institutions: [ | ||
{ | ||
name: String, | ||
country: String, | ||
assets: [ | ||
{ | ||
currency: String, | ||
amount: Number, | ||
isEarning: Boolean, | ||
description: String, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
module.exports = | ||
mongoose.models.Record || mongoose.model("Record", RecordSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use server"; | ||
|
||
export async function getQuotes({ baseCurrencies, recordCurrencies }) { | ||
const fetchUrls = recordCurrencies.map( | ||
(currency) => | ||
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${currency}.json` | ||
); | ||
|
||
async function fetchQuotes(fetchUrls) { | ||
try { | ||
const promises = fetchUrls.map((url) => fetch(url)); | ||
const responses = await Promise.all(promises); | ||
const data = await Promise.all( | ||
responses.map((response) => response.json()) | ||
); | ||
return data; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
const quotes = (await fetchQuotes(fetchUrls)) | ||
.map((quote) => { | ||
const { date, ...rest } = quote; | ||
const [baseCurrency] = Object.keys(rest); | ||
const quotes = Object.entries(rest[baseCurrency]); | ||
const targetQuotes = quotes | ||
.filter(([key]) => baseCurrencies.includes(key)) | ||
.map(([currency, value]) => ({ | ||
currency: currency, | ||
rate: value, | ||
})); | ||
return { baseCurrency: baseCurrency, rates: [...targetQuotes] }; | ||
}) | ||
.flat(); | ||
|
||
return quotes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const connect = async () => { | ||
try { | ||
await mongoose.connect(process.env.MONGO_URI); | ||
console.log("Database connected!"); | ||
} catch (error) { | ||
throw new Error("Connection failed!"); | ||
} | ||
}; | ||
|
||
export default connect; |