Api files no longer return router, exports usable functions.
This commit is contained in:
@@ -1,21 +1,13 @@
|
|||||||
const express = require('express');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const router = express.Router();
|
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
mongoose.connect('mongodb://localhost:27017/vinlottis', {
|
mongoose.connect('mongodb://localhost:27017/vinlottis', {
|
||||||
useNewUrlParser: true
|
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 Highscore = require(path.join(__dirname + '/../schemas/Highscore'));
|
||||||
const Wine = require(path.join(__dirname + '/../schemas/Wine'));
|
const Wine = require(path.join(__dirname + '/../schemas/Wine'));
|
||||||
|
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
||||||
|
|
||||||
@@ -62,7 +54,7 @@ const resolveWineReferences = listWithWines => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
router.route('/all').get((req, res) => {
|
const all = (req, res) => {
|
||||||
return Highscore.find()
|
return Highscore.find()
|
||||||
.then(highscore => getHighscoreByDates(highscore))
|
.then(highscore => getHighscoreByDates(highscore))
|
||||||
.then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries))
|
.then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries))
|
||||||
@@ -70,9 +62,19 @@ router.route('/all').get((req, res) => {
|
|||||||
message: "Lotteries by date!",
|
message: "Lotteries by date!",
|
||||||
lotteries
|
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 { date } = req.params;
|
||||||
const dateString = epochToDateString(date);
|
const dateString = epochToDateString(date);
|
||||||
|
|
||||||
@@ -92,10 +94,10 @@ router.route('/by-date/:date').get((req, res) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
router.route("/by-name").get((req, res) => {
|
const byName = (req, res) => {
|
||||||
const { name } = req.query;
|
const { name } = req.params;
|
||||||
|
|
||||||
return Highscore.find({ name })
|
return Highscore.find({ name })
|
||||||
.then(async (highscore) => {
|
.then(async (highscore) => {
|
||||||
@@ -113,6 +115,11 @@ router.route("/by-name").get((req, res) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = {
|
||||||
|
all,
|
||||||
|
latest,
|
||||||
|
byEpochDate,
|
||||||
|
byName
|
||||||
|
};
|
||||||
|
|||||||
@@ -13,23 +13,19 @@ const PreLotteryWine = require(path.join(
|
|||||||
__dirname + "/../schemas/PreLotteryWine"
|
__dirname + "/../schemas/PreLotteryWine"
|
||||||
));
|
));
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
const prelotteryWines = async (req, res) => {
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
router.route("/wines/prelottery").get(async (req, res) => {
|
|
||||||
let wines = await PreLotteryWine.find();
|
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()
|
let purchases = await Purchase.find()
|
||||||
.populate("wines")
|
.populate("wines")
|
||||||
.sort({ date: 1 });
|
.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();
|
const countColor = await Purchase.find();
|
||||||
let red = 0;
|
let red = 0;
|
||||||
let blue = 0;
|
let blue = 0;
|
||||||
@@ -75,7 +71,7 @@ router.route("/purchase/statistics/color").get(async (req, res) => {
|
|||||||
|
|
||||||
const total = red + yellow + blue + green;
|
const total = red + yellow + blue + green;
|
||||||
|
|
||||||
res.json({
|
return res.json({
|
||||||
red: {
|
red: {
|
||||||
total: red,
|
total: red,
|
||||||
win: redWin
|
win: redWin
|
||||||
@@ -95,21 +91,21 @@ router.route("/purchase/statistics/color").get(async (req, res) => {
|
|||||||
stolen: stolen,
|
stolen: stolen,
|
||||||
total: total
|
total: total
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
router.route("/highscore/statistics").get(async (req, res) => {
|
const highscore = async (req, res) => {
|
||||||
const highscore = await Highscore.find().populate("wins.wine");
|
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();
|
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");
|
const highscore = await Highscore.find().populate("wins.wine");
|
||||||
let wines = {};
|
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
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const router = express.Router();
|
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
mongoose.connect("mongodb://localhost:27017/vinlottis", {
|
mongoose.connect("mongodb://localhost:27017/vinlottis", {
|
||||||
useNewUrlParser: true
|
useNewUrlParser: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const sub = require(path.join(__dirname + "/../api/subscriptions"));
|
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 _wineFunctions = require(path.join(__dirname + "/../api/wine"));
|
||||||
const _personFunctions = require(path.join(__dirname + "/../api/person"));
|
const _personFunctions = require(path.join(__dirname + "/../api/person"));
|
||||||
@@ -19,11 +15,7 @@ const PreLotteryWine = require(path.join(
|
|||||||
__dirname + "/../schemas/PreLotteryWine"
|
__dirname + "/../schemas/PreLotteryWine"
|
||||||
));
|
));
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
const submitWines = async (req, res) => {
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
router.route("/log/wines").post(mustBeAuthenticated, async (req, res) => {
|
|
||||||
const wines = req.body;
|
const wines = req.body;
|
||||||
for (let i = 0; i < wines.length; i++) {
|
for (let i = 0; i < wines.length; i++) {
|
||||||
let wine = wines[i];
|
let wine = wines[i];
|
||||||
@@ -50,20 +42,20 @@ router.route("/log/wines").post(mustBeAuthenticated, async (req, res) => {
|
|||||||
sub.sendNotification(subscription, message);
|
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 schema = { ...PreLotteryWine.schema.obj };
|
||||||
let nulledSchema = Object.keys(schema).reduce((accumulator, current) => {
|
let nulledSchema = Object.keys(schema).reduce((accumulator, current) => {
|
||||||
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();
|
await PreLotteryWine.deleteMany();
|
||||||
|
|
||||||
const purchaseBody = req.body.purchase;
|
const purchaseBody = req.body.purchase;
|
||||||
@@ -102,7 +94,11 @@ router.route("/log").post(mustBeAuthenticated, async (req, res) => {
|
|||||||
|
|
||||||
await purchase.save();
|
await purchase.save();
|
||||||
|
|
||||||
res.send(true);
|
return res.send(true);
|
||||||
});
|
};
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = {
|
||||||
|
submitWines,
|
||||||
|
schema,
|
||||||
|
submitLottery
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
const express = require("express");
|
|
||||||
const path = require("path");
|
|
||||||
const router = express.Router();
|
|
||||||
const fetch = require('node-fetch')
|
const fetch = require('node-fetch')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
const mustBeAuthenticated = require(path.join(__dirname + "/../middleware/mustBeAuthenticated"))
|
const byEAN = async (req, res) => {
|
||||||
|
|
||||||
router.use((req, res, next) => {
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
router.route("/wineinfo/:ean").get(async (req, res) => {
|
|
||||||
const vinmonopoletResponse = await fetch("https://app.vinmonopolet.no/vmpws/v2/vmp/products/barCodeSearch/" + req.params.ean)
|
const vinmonopoletResponse = await fetch("https://app.vinmonopolet.no/vmpws/v2/vmp/products/barCodeSearch/" + req.params.ean)
|
||||||
.then(resp => resp.json())
|
.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
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user