Instructions to Use -
- Clone the Repository.
- Navigate into the Directory of Repository via terminal
- npm install
- node server.js
Problem statement:
Create an API to fetch stock price for a security from an external API. In this API, the main server will receive user request to fetch the stock prices.
/stock (GET): This query will return the stock prices of a stock. If the stock doesn't exist in the database, then first the stock is created, data is populated in the database and then sent back to the user.
Query fields
- ticker or array of ticker (tickers separated by "-") (required: true) [*Ticker: Code of stock. For ex: WIPRO, TATAMOTORS, INFY etc..]
- dataType (required: false, default: "Close") [*learn more about the types from external API output]
- startDate (required: false)
- endDate (required: false)
Stock has following schema.
- ticker (Code of the stock)
- priceHistory: (Object of stock prices {dateType: Array of prices}). For ex: "Close":[Array],"Open":[Array]) and so on). This is DIFFERENT from how data is stored in external API.
- createdDate: Datetime the data was first added to the database
- updatedDate: Datetime at which priceHistory was updated
How is the data populated (via inter-server communication)?
-
The API server process launches (when starts) a separate web-socket server as well (server at a different port)
-
This web-socket server is responsible for doing any computation and fetching the prices from an external third-party API.
-
On getting, user request the API checks whether the data for the stock is present in the database. If it's not present, a request is sent to the web-socket server to fetch the data. On successful data retrieval, the data is returned back to the main server (via web-socket) and updated in the database. After update, the data is sent back to the user for the relevant query.