Last active
September 23, 2020 16:02
-
-
Save jinwook-k/cf2944a7e656b66afd2397a810ea35f5 to your computer and use it in GitHub Desktop.
server/api/weather.js
This file contains hidden or 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
const WEATHER = require("../models/Weather"); | |
... | |
class Weather { | |
... | |
/** | |
* Saves the weather data using the zipcode as the unique identifier | |
* If it already exists, replace, if not, then add. | |
* | |
* @param {number} zipCode The zipcode used to identify the document to upsert | |
* @param {string} data Weather data to save/update | |
* @return {JSON} The data response from the weather api data. | |
*/ | |
saveWeatherDataToMongo = async (zipCode, data) => { | |
const filter = { | |
zip: zipCode | |
} | |
const replace = { | |
...filter, | |
...data, | |
data: Date.now() | |
} | |
await this.findOneReplace(filter, replace); | |
} | |
/** | |
* Saves Weather data to MongoDb | |
* | |
* @param {number} zipCode The zipcode used as unique identifier to find the document from mongo | |
* @return {JSON} The data response from the mongodb. | |
*/ | |
getWeatherDataFromMongo = async (zipCode) => { | |
return WEATHER.findOne({zip: zipCode}); | |
} | |
/** | |
* If a document already exists with the filter, then replace, if not, add. | |
* | |
* @param {{zip: number}} filter The filter is the zipcode used as unique identifier to find the document from mongo | |
* @return {JSON} The data response from the mongodb. | |
*/ | |
async findOneReplace(filter, replace) { | |
await WEATHER.findOneAndReplace(filter, replace, {new: true, upsert: true}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment