Split request into controller and repo.
Also try returning better error message on exceptions and check for errors in payload to return well-defined errors.
This commit is contained in:
64
api/controllers/requestController.js
Normal file
64
api/controllers/requestController.js
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
const path = require("path");
|
||||||
|
const RequestRepository = require(path.join(
|
||||||
|
__dirname, "../request"
|
||||||
|
));
|
||||||
|
|
||||||
|
function addRequest(req, res) {
|
||||||
|
const { wine } = req.body;
|
||||||
|
|
||||||
|
return RequestRepository.addNew(wine)
|
||||||
|
.then(wine => res.json({
|
||||||
|
message: "Successfully added new request",
|
||||||
|
wine: wine,
|
||||||
|
success: true
|
||||||
|
}))
|
||||||
|
.catch(error => {
|
||||||
|
const { message, statusCode } = error;
|
||||||
|
|
||||||
|
return res.status(statusCode || 500).send({
|
||||||
|
success: false,
|
||||||
|
message: message || "Unable to add requested wine."
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAllRequests(req, res) {
|
||||||
|
return RequestRepository.getAll()
|
||||||
|
.then(wines => res.json({
|
||||||
|
wines: wines,
|
||||||
|
success: true
|
||||||
|
}))
|
||||||
|
.catch(error => {
|
||||||
|
console.log("error in getAllRequests:", error);
|
||||||
|
|
||||||
|
const message = "Unable to fetch all requested wines."
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: message
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteRequest(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
return RequestRepository.deleteById(id)
|
||||||
|
.then(_ => res.json({
|
||||||
|
message: `Slettet vin med id: ${ id }`,
|
||||||
|
success: true
|
||||||
|
}))
|
||||||
|
.catch(error => {
|
||||||
|
const { statusCode, message } = error;
|
||||||
|
|
||||||
|
return res.status(statusCode || 500).send({
|
||||||
|
success: false,
|
||||||
|
message: message || "Unable to delete requested wine."
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
addRequest,
|
||||||
|
getAllRequests,
|
||||||
|
deleteRequest
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
const express = require("express");
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const RequestedWine = require(path.join(
|
const RequestedWine = require(path.join(
|
||||||
__dirname, "/schemas/RequestedWine"
|
__dirname, "/schemas/RequestedWine"
|
||||||
@@ -7,31 +6,15 @@ const Wine = require(path.join(
|
|||||||
__dirname, "/schemas/Wine"
|
__dirname, "/schemas/Wine"
|
||||||
));
|
));
|
||||||
|
|
||||||
const deleteRequestedWineById = async (req, res) => {
|
class RequestedWineNotFound extends Error {
|
||||||
const { id } = req.params;
|
constructor(message="Wine with this id was not found.") {
|
||||||
if(id == null){
|
super(message);
|
||||||
return res.json({
|
this.name = "RequestedWineNotFound";
|
||||||
message: "Id er ikke definert",
|
this.statusCode = 404;
|
||||||
success: false
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await RequestedWine.deleteOne({wineId: id})
|
const addNew = async (wine) => {
|
||||||
return res.json({
|
|
||||||
message: `Slettet vin med id: ${id}`,
|
|
||||||
success: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAllRequestedWines = async (req, res) => {
|
|
||||||
const allWines = await RequestedWine.find({}).populate("wine");
|
|
||||||
|
|
||||||
return res.json(allWines);
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestNewWine = async (req, res) => {
|
|
||||||
const {wine} = req.body
|
|
||||||
|
|
||||||
let thisWineIsLOKO = await Wine.findOne({id: wine.id})
|
let thisWineIsLOKO = await Wine.findOne({id: wine.id})
|
||||||
|
|
||||||
if(thisWineIsLOKO == undefined){
|
if(thisWineIsLOKO == undefined){
|
||||||
@@ -59,11 +42,31 @@ const requestNewWine = async (req, res) => {
|
|||||||
}
|
}
|
||||||
await requestedWine.save()
|
await requestedWine.save()
|
||||||
|
|
||||||
return res.send(requestedWine);
|
return requestedWine;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getById = (id) => {
|
||||||
|
return RequestedWine.findOne({ wineId: id }).populate("wine")
|
||||||
|
.then(wine => {
|
||||||
|
if (wine == null) {
|
||||||
|
throw new RequestedWineNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return wine;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteById = (id) => {
|
||||||
|
return getById(id)
|
||||||
|
.then(wine => RequestedWine.deleteOne({ wineId: wine.id }))
|
||||||
|
}
|
||||||
|
|
||||||
|
const getAll = () => {
|
||||||
|
return RequestedWine.find({}).populate("wine");
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
requestNewWine,
|
addNew,
|
||||||
getAllRequestedWines,
|
getAll,
|
||||||
deleteRequestedWineById
|
deleteById
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,13 +17,15 @@ const virtualRegistrationApi = require(path.join(
|
|||||||
const lottery = require(path.join(__dirname, "/lottery"));
|
const lottery = require(path.join(__dirname, "/lottery"));
|
||||||
const chatHistoryApi = require(path.join(__dirname, "/chatHistory"));
|
const chatHistoryApi = require(path.join(__dirname, "/chatHistory"));
|
||||||
|
|
||||||
|
const requestController = require(path.join(__dirname, "/controllers/requestController"));
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get("/wineinfo/search", wineinfo.wineSearch);
|
router.get("/wineinfo/search", wineinfo.wineSearch);
|
||||||
|
|
||||||
router.get("/request/all", setAdminHeaderIfAuthenticated, request.getAllRequestedWines);
|
router.get("/request/all", setAdminHeaderIfAuthenticated, requestController.getAllRequests);
|
||||||
router.post("/request/new-wine", request.requestNewWine);
|
router.post("/request/new-wine", requestController.addRequest);
|
||||||
router.delete("/request/:id", request.deleteRequestedWineById);
|
router.delete("/request/:id", requestController.deleteRequest);
|
||||||
|
|
||||||
router.get("/wineinfo/schema", mustBeAuthenticated, update.schema);
|
router.get("/wineinfo/schema", mustBeAuthenticated, update.schema);
|
||||||
router.get("/wineinfo/:ean", wineinfo.byEAN);
|
router.get("/wineinfo/:ean", wineinfo.byEAN);
|
||||||
|
|||||||
Reference in New Issue
Block a user