Hey there everyone! I’m trying really hard on solving this challenge, I don’t want to peak at the solution. It seems like my major issue when running tests is that I’m getting an internal server error 500, instead of the statuses I’m sending back. Also, my objects are being sent empty, I don’t understand why. Despite that, I know I’m trying quite a DRYed code, but I’d like to know if it’s possible and if I’m on a good path.
Have you got any tips on how to solve it? Here’s my code for the api.js!!
const express = require(‘express’);
const api = express.Router();
const apiRouter = express.Router();
const checkMillionDollarIdea = require(’./checkMillionDollarIdea’);
const {
createMeeting,
getAllFromDatabase,
getFromDatabaseById,
addToDatabase,
updateInstanceInDatabase,
deleteFromDatabasebyId,
deleteAllFromDatabase,
} = require(’./db’);
apiRouter.use([’/minions’, ‘/minions/minionId’], (req, res, next) => {
req.body.type = 'minions';
next();
})
apiRouter.use([’/ideas’, ‘/ideas/ideasId’], (req, res, next) => {
req.body.type = 'ideas';
next();
})
apiRouter.param([‘minionId’, ‘ideaId’, ‘meetingId’], (req, res, next, id) => {
const api = getFromDatabaseById(type, id);
if (apiThrough) {
req.api = api;
next();
} else {
res.status(404).send();
}
});
apiRouter.use(’/ideas/ideaId’, checkMillionDollarIdea);
apiRouter.use(’/meetings’, (req, res, next) => {
req.body.type = 'meetings';
next();
})
apiRouter.get ([’/minions’, ‘/ideas’, ‘/meetings’], (req, res, next) => {
const type = req.body.type;
const api = getAllFromDatabase(type);
res.status(200).send(api);
})
apiRouter.post ([’/minions’, ‘/ideas’], (req, res, next) => {
const newApi = req.body;
const type = req.body.type;
addToDatabase(type, newApi);
res.status(201).send(newApi);
})
apiRouter.get ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {
const apiId = Number(req.body.id);
const type = req.body.type;
const apiFound = database.getFromDatabaseById(type, apiId)
if (apiFound.id !== -1 && Number.isInteger(apiId)){
res.status(200).send(apiFound);
} else {
res.status(404).send();
}
})
apiRouter.put ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {
const apiId = Number(req.body.id);
const type = req.body.type;
const newApi = req.body;
const api = getFromDatabaseById(type, apiId);
if (api.id !== -1 && Number.isInteger(apiId)){
updateInstanceInDatabase(type, newApi);
res.status(200).send(newApi);
} else {
res.status(404).send("Not Found");
}
})
apiRouter.delete ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {
const apiId = Number(req.body.id);
const type = req.body.type;
const api = getFromDatabaseById(type, apiId);
if (api.id !== -1 && Number.isInteger(apiId)){
deleteFromDatabasebyId(type, apiId);
res.status(204).send();
} else {
res.status(404).send("Not Found");
}
})
apiRouter.post(’/meetings’, (req, res, next) => {
const newMeeting = createMeeting();
res.status(201).send(newMeeting);
})
apiRouter.delete(’/meetings’, (req, res, next) => {
deleteAllFromDatabase('meetings');
res.status(204).send();
})
module.exports = apiRouter;