diff --git a/api/chatHistory.js b/api/chatHistory.js index dc1862c..ae3e0ba 100644 --- a/api/chatHistory.js +++ b/api/chatHistory.js @@ -1,33 +1,29 @@ -const express = require("express"); const path = require("path"); -const router = express.Router(); - const { history, clearHistory } = require(path.join(__dirname + "/../api/redis")); -router.use((req, res, next) => { - next(); -}); - -router.route("/chat/history").get(async (req, res) => { +const getAllHistory = (req, res) => { let { skip, take } = req.query; skip = !isNaN(skip) ? Number(skip) : undefined; take = !isNaN(take) ? Number(take) : undefined; - try { - const messages = await history(skip, take); - res.json(messages) - } catch(error) { - res.status(500).send(error); - } -}); + return history(skip, take) + .then(messages => res.json(messages)) + .catch(error => res.status(500).json({ + message: error.message, + success: false + })); +}; -router.route("/chat/history").delete(async (req, res) => { - try { - const messages = await clearHistory(); - res.json(messages) - } catch(error) { - res.status(500).send(error); - } -}); +const deleteHistory = (req, res) => { + return clearHistory() + .then(message => res.json(message)) + .catch(error => res.status(500).json({ + message: error.message, + success: false + })); +}; -module.exports = router; +module.exports = { + getAllHistory, + deleteHistory +}; diff --git a/api/router.js b/api/router.js index 80ddd76..a4bd555 100644 --- a/api/router.js +++ b/api/router.js @@ -16,6 +16,7 @@ const virtualRegistrationApi = require(path.join( __dirname + "/virtualRegistration" )); const lottery = require(path.join(__dirname + "/lottery")); +const chatHistoryApi = require(path.join(__dirname + "/chatHistory")); const router = express.Router(); @@ -60,6 +61,9 @@ router.post('/winner/notify/:id', virtualRegistrationApi.sendNotificationToWinne router.get('/winner/:id', virtualRegistrationApi.getWinesToWinnerById); router.post('/winner/:id', virtualRegistrationApi.registerWinnerSelection); +router.get('/chat/history', chatHistoryApi.getAllHistory) +router.delete('/chat/history', mustBeAuthenticated, chatHistoryApi.deleteHistory) + router.post('/login', userApi.login); router.post('/register', mustBeAuthenticated, userApi.register); router.get('/logout', userApi.logout);