diff --git a/api/lottery.js b/api/lottery.js index 9d192f3..17d8e29 100644 --- a/api/lottery.js +++ b/api/lottery.js @@ -1,21 +1,13 @@ -const express = require('express'); const path = require('path'); -const router = express.Router(); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/vinlottis', { useNewUrlParser: true }) -const mustBeAuthenticated = require(path.join( - __dirname + '/../middleware/mustBeAuthenticated' -)); -const config = require(path.join(__dirname + '/../config/defaults/lottery')); - const Highscore = require(path.join(__dirname + '/../schemas/Highscore')); const Wine = require(path.join(__dirname + '/../schemas/Wine')); - // Utils const epochToDateString = date => new Date(parseInt(date)).toDateString(); @@ -62,7 +54,7 @@ const resolveWineReferences = listWithWines => { } // Routes -router.route('/all').get((req, res) => { +const all = (req, res) => { return Highscore.find() .then(highscore => getHighscoreByDates(highscore)) .then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries)) @@ -70,9 +62,19 @@ router.route('/all').get((req, res) => { message: "Lotteries by date!", lotteries })) -}) +} -router.route('/by-date/:date').get((req, res) => { +const latest = (req, res) => { + return Highscore.find() + .then(highscore => getHighscoreByDates(highscore)) + .then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries)) + .then(lotteries => res.send({ + message: "Latest lottery!", + lottery: lotteries.slice(-1).pop() + })) +} + +const byEpochDate = (req, res) => { const { date } = req.params; const dateString = epochToDateString(date); @@ -92,10 +94,10 @@ router.route('/by-date/:date').get((req, res) => { }) } }) -}) +} -router.route("/by-name").get((req, res) => { - const { name } = req.query; +const byName = (req, res) => { + const { name } = req.params; return Highscore.find({ name }) .then(async (highscore) => { @@ -113,6 +115,11 @@ router.route("/by-name").get((req, res) => { }) } }) -}) +} -module.exports = router; +module.exports = { + all, + latest, + byEpochDate, + byName +}; diff --git a/api/retrieve.js b/api/retrieve.js index c7577b8..b6b62f4 100644 --- a/api/retrieve.js +++ b/api/retrieve.js @@ -13,23 +13,19 @@ const PreLotteryWine = require(path.join( __dirname + "/../schemas/PreLotteryWine" )); -router.use((req, res, next) => { - next(); -}); - -router.route("/wines/prelottery").get(async (req, res) => { +const prelotteryWines = async (req, res) => { let wines = await PreLotteryWine.find(); - res.json(wines); -}); + return res.json(wines); +}; -router.route("/purchase/statistics").get(async (req, res) => { +const allPurchase = async (req, res) => { let purchases = await Purchase.find() .populate("wines") .sort({ date: 1 }); - res.json(purchases); -}); + return res.json(purchases); +}; -router.route("/purchase/statistics/color").get(async (req, res) => { +const purchaseByColor = async (req, res) => { const countColor = await Purchase.find(); let red = 0; let blue = 0; @@ -75,7 +71,7 @@ router.route("/purchase/statistics/color").get(async (req, res) => { const total = red + yellow + blue + green; - res.json({ + return res.json({ red: { total: red, win: redWin @@ -95,21 +91,21 @@ router.route("/purchase/statistics/color").get(async (req, res) => { stolen: stolen, total: total }); -}); +}; -router.route("/highscore/statistics").get(async (req, res) => { +const highscore = async (req, res) => { const highscore = await Highscore.find().populate("wins.wine"); - res.json(highscore); -}); + return res.json(highscore); +}; -router.route("/wines/statistics").get(async (req, res) => { +const allWines = async (req, res) => { const wines = await Wine.find(); - res.json(wines); -}); + return res.json(wines); +}; -router.route("/wines/statistics/overall").get(async (req, res) => { +const allWinesSummary = async (req, res) => { const highscore = await Highscore.find().populate("wins.wine"); let wines = {}; @@ -149,7 +145,14 @@ router.route("/wines/statistics/overall").get(async (req, res) => { } } - res.json(Object.values(wines)); -}); + return res.json(Object.values(wines)); +}; -module.exports = router; +module.exports = { + prelotteryWines, + allPurchase, + purchaseByColor, + highscore, + allWines, + allWinesSummary +}; diff --git a/api/update.js b/api/update.js index 5fd2c12..fe4ec17 100644 --- a/api/update.js +++ b/api/update.js @@ -1,15 +1,11 @@ const express = require("express"); const path = require("path"); -const router = express.Router(); const mongoose = require("mongoose"); mongoose.connect("mongodb://localhost:27017/vinlottis", { useNewUrlParser: true }); const sub = require(path.join(__dirname + "/../api/subscriptions")); -const mustBeAuthenticated = require(path.join( - __dirname + "/../middleware/mustBeAuthenticated" -)); const _wineFunctions = require(path.join(__dirname + "/../api/wine")); const _personFunctions = require(path.join(__dirname + "/../api/person")); @@ -19,11 +15,7 @@ const PreLotteryWine = require(path.join( __dirname + "/../schemas/PreLotteryWine" )); -router.use((req, res, next) => { - next(); -}); - -router.route("/log/wines").post(mustBeAuthenticated, async (req, res) => { +const submitWines = async (req, res) => { const wines = req.body; for (let i = 0; i < wines.length; i++) { let wine = wines[i]; @@ -50,20 +42,20 @@ router.route("/log/wines").post(mustBeAuthenticated, async (req, res) => { sub.sendNotification(subscription, message); } - res.send(true); -}); + return res.send(true); +}; -router.route("/log/schema").get(mustBeAuthenticated, async (req, res) => { +const schema = async (req, res) => { let schema = { ...PreLotteryWine.schema.obj }; let nulledSchema = Object.keys(schema).reduce((accumulator, current) => { accumulator[current] = ""; - return accumulator; + return accumulator }, {}); - res.send(nulledSchema); -}); + return res.send(nulledSchema); +} -router.route("/log").post(mustBeAuthenticated, async (req, res) => { +const submitLottery = async (req, res) => { await PreLotteryWine.deleteMany(); const purchaseBody = req.body.purchase; @@ -102,7 +94,11 @@ router.route("/log").post(mustBeAuthenticated, async (req, res) => { await purchase.save(); - res.send(true); -}); + return res.send(true); +}; -module.exports = router; +module.exports = { + submitWines, + schema, + submitLottery +}; diff --git a/api/wineinfo.js b/api/wineinfo.js index 237de19..5a8de10 100644 --- a/api/wineinfo.js +++ b/api/wineinfo.js @@ -1,15 +1,7 @@ -const express = require("express"); -const path = require("path"); -const router = express.Router(); const fetch = require('node-fetch') +const path = require('path') -const mustBeAuthenticated = require(path.join(__dirname + "/../middleware/mustBeAuthenticated")) - -router.use((req, res, next) => { - next(); -}); - -router.route("/wineinfo/:ean").get(async (req, res) => { +const byEAN = async (req, res) => { const vinmonopoletResponse = await fetch("https://app.vinmonopolet.no/vmpws/v2/vmp/products/barCodeSearch/" + req.params.ean) .then(resp => resp.json()) @@ -25,7 +17,9 @@ router.route("/wineinfo/:ean").get(async (req, res) => { }) } - res.send(vinmonopoletResponse); -}); + return res.send(vinmonopoletResponse); +}; -module.exports = router; +module.exports = { + byEAN +};