Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/api crypto get product [ 2 ] #209

50 changes: 50 additions & 0 deletions controllers/cryptoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { fetchProducts, fetchProduct } = require('../models/crypto')

const ERROR_MESSAGE = 'Something went wrong. Please try again or contact admin'

/**
* Get the products details
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/

const getProducts = async (req, res) => {
try {
const productData = await fetchProducts()
return res.json({
message: Object.keys(productData).length !== 0 ? 'Products returned successfully!' : 'No products found',
products: productData
})
} catch (err) {
logger.error(`Error while retriving products ${err}`)
return res.boom.badImplementation(ERROR_MESSAGE)
}
}

/**
* Get the product details
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/

const getProduct = async (req, res) => {
try {
const productId = req.params.productId
const productData = await fetchProduct(productId)
if (productData) {
return res.json({
message: 'Product returned successfully.',
product: productData
})
}
return res.boom.notFound('Product doesn\'t exist')
} catch (err) {
logger.error(`Error while retriving products ${err}`)
return res.boom.badImplementation(ERROR_MESSAGE)
}
}

module.exports = {
getProducts,
getProduct
}
49 changes: 49 additions & 0 deletions models/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const firestore = require('../utils/firestore')
const cryptoProductsCollection = firestore.collection('crypto-products')

/**
* Fetches the data of crypto product
* @return {Promise<userModel|Array>}
*/

const fetchProducts = async () => {
try {
const snapshot = await cryptoProductsCollection.get()

const productsData = {}

snapshot.forEach((doc) => {
const data = doc.data()
productsData[data.id] = { ...data }
})
return productsData
} catch (err) {
logger.error('Error retrieving product data', err)
throw err
}
}

/**
* Fetches the data of crypto product
* @param productId { string }: product id
* @return {Promise<userModel|Array>}
*/

const fetchProduct = async (productId) => {
try {
const snapshot = await cryptoProductsCollection.doc(productId).get()
if (snapshot.exists) {
const productData = snapshot.data()
return productData
}
return undefined
} catch (err) {
logger.error('Error retrieving product data', err)
throw err
}
}

module.exports = {
fetchProducts,
fetchProduct
}
63 changes: 63 additions & 0 deletions routes/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const express = require('express')
const router = express.Router()
const cryptoController = require('../controllers/cryptoController')

/**
* @swagger
* /crypto/products:
* get:
* summary: Used to get all the crypto products data
* tags:
* - crypto
* responses:
* 200:
* description: Return product data
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/crypto'
* 404:
* description: not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/errors/notFound'
* 500:
* description: badImplementation
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/errors/badImplementation'
*/
router.get('/products', cryptoController.getProducts)

/**
* @swagger
* /crypto/products/{productid}:
* get:
* summary: Used to get the crypto product data
* tags:
* - crypto
* responses:
* 200:
* description: Return product data
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/crypto'
* 404:
* description: not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/errors/notFound'
* 500:
* description: badImplementation
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/errors/badImplementation'
*/
router.get('/products/:productId', cryptoController.getProduct)

module.exports = router
1 change: 1 addition & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ app.use('/tasks', require('./tasks'))
app.use('/challenges', require('./challenges.js'))
app.use('/pullrequests', require('./pullrequests.js'))
app.use('/contributions', require('./contributions'))
app.use('/crypto', require('./crypto'))
app.use('/badges', require('./badges.js'))

module.exports = app