Skip to content

Commit 60e4cb3

Browse files
committed
Modularized card v deck methods and routing; created appropriate sub-directories, and moved associated functionality to assigned locations
1 parent e48e16e commit 60e4cb3

File tree

5 files changed

+63
-77
lines changed

5 files changed

+63
-77
lines changed

server/controllers/cardControllers.js

+1-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
* This controller handles routes to localhost:3000/card
3-
*
43
* "Queries are Not Promises," from mongo docs: https://mongoosejs.com/docs/queries.html
54
*/
65

@@ -9,7 +8,7 @@ const CardModel = require('../models/card');
98
const cardController = {};
109

1110
cardController.addCard = (req, res, next) => {
12-
// receive request access request.body and destructure model
11+
// deconstruct properties required in mongoose/mongo model from request.body
1312
const { term, definition, deckId } = req.body;
1413
// instantiate a new card document via the mongoose model
1514
CardModel.create({ term, definition, deckId })
@@ -20,47 +19,4 @@ cardController.addCard = (req, res, next) => {
2019
.catch(() => next(new Error('Error in addCard create method')));
2120
};
2221

23-
cardController.readDeckOfCards = (req, res, next) => {
24-
// const { deckId } = req.params; // deckId = '1' (string type)
25-
/*
26-
req.params = {
27-
deckId: 1
28-
}
29-
*/
30-
const { deckId } = req.params; // deckId = '1' (string type)
31-
console.log(deckId); // 1
32-
// declare constant using mongo db query
33-
const query = CardModel.find({deckId});
34-
35-
// create functional promise out of mongodb query
36-
const promise = query.exec();
37-
38-
promise
39-
.then((data) => {
40-
res.locals.data = data;
41-
console.log(res.locals.data);
42-
next();
43-
})
44-
.catch(() => next(new Error('Error in readCard read method')));
45-
};
46-
47-
// cardController.readCard = (req, res, next) => {
48-
// // receive request access request.params and deconstruct model
49-
// const { cardId } = req.params;
50-
51-
// // declare constant using mongo db query
52-
// const query = CardModel.findById(cardId);
53-
54-
// // create functional promise out of mongodb query
55-
// const promise = query.exec();
56-
57-
// promise
58-
// .then((data) => {
59-
// res.locals.data = data;
60-
// console.log(res.locals.data);
61-
// next();
62-
// })
63-
// .catch(() => next(new Error('Error in readCard read method')));
64-
// };
65-
6622
module.exports = cardController;

server/controllers/deckControllers.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This controller handles routes to localhost:3000/card
3+
* "Queries are Not Promises," from mongo docs: https://mongoosejs.com/docs/queries.html
4+
*/
5+
6+
const CardModel = require('../models/card');
7+
8+
const deckController = {};
9+
10+
deckController.readDeckOfCards = (req, res, next) => {
11+
// deconstruct property required in mongoose/mongo model's find method from request.params
12+
const { deckId } = req.params;
13+
// declare constant using mongo db query
14+
const query = CardModel.find({ deckId });
15+
16+
// create functional promise out of mongodb query
17+
const promise = query.exec();
18+
19+
promise
20+
.then((data) => {
21+
res.locals.data = data;
22+
console.log(res.locals.data);
23+
next();
24+
})
25+
.catch(() => next(new Error('Error in readDeckOfCards read method')));
26+
};
27+
28+
module.exports = deckController;

server/routes/cardRoutes.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,13 @@
33
*/
44

55
const express = require('express');
6-
const cardRouter = express.Router();
76
const cardController = require('../controllers/cardControllers');
87

9-
// /card/ routes post requests to '/' to cardController
8+
const cardRouter = express.Router();
9+
10+
// directs post requests made to the root endpoint of /card to the cardController
1011
cardRouter.post('/', cardController.addCard, (req, res) => {
1112
res.status(200).send(res.locals.newCard);
1213
});
1314

14-
// [ ] hypothesis ... don't need a colon with params ...
15-
// /deck/[number]
16-
cardRouter.get('/deck/:deckId', cardController.readDeckOfCards, (req, res) => {
17-
/*
18-
from axios: https://oscards/deck/1
19-
processed in express: req.params = {
20-
deckId: 1
21-
}
22-
*/
23-
res.status(200).send(res.locals.data);
24-
});
25-
26-
// cardRouter.get('/', cardController.readCard, (req, res) => {
27-
// res.status(200).send(res.locals.data);
28-
// });
29-
30-
module.exports = cardRouter;
15+
module.exports = cardRouter;

server/routes/deckRoutes.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This router handles routes to localhost:3000/deck
3+
*/
4+
5+
const express = require('express');
6+
const deckController = require('../controllers/deckControllers');
7+
8+
const deckRouter = express.Router();
9+
10+
// directs get requests made to the root endpoint of /deck to the deckController
11+
deckRouter.get('/:deckId', deckController.readDeckOfCards, (req, res) => {
12+
res.status(200).send(res.locals.data);
13+
});
14+
15+
module.exports = deckRouter;

server/server.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const mongoose = require('mongoose');
77
// invocation of dot env to access API keys
88
require('dotenv').config();
99

10-
// creation of express instance
10+
// creation of express instance
1111
const app = express();
1212
const PORT = 3000;
1313
const mongoURI = `${process.env.MONGO_URI}`;
1414

1515
// connect to instance of mongodb atlas
16-
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true})
16+
mongoose
17+
.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
1718
.then(() => {
1819
app.listen(PORT, () => console.log(`Listening on PORT: ${PORT}`));
1920
})
@@ -28,22 +29,23 @@ if (process.env.NODE_ENV === 'production') {
2829
app.use('/dist', express.static(path.join(__dirname, '../dist')));
2930
}
3031

31-
// instantiate router(s) for data calls
32+
// instantiate router(s) for data calls
3233
const cardRouter = require('./routes/cardRoutes.js');
34+
const deckRouter = require('./routes/deckRoutes.js');
35+
3336
app.use('/card', cardRouter);
37+
app.use('/deck', deckRouter);
3438

3539
// serve index.html on the route '/'
36-
app.get('/', (req, res) => {
37-
return res.status(200).sendFile(path.join(__dirname, '../client/src/index.html'));
38-
});
40+
app.get('/', (req, res) =>
41+
res.status(200).sendFile(path.join(__dirname, '../client/src/index.html'))
42+
);
3943

40-
// 404 error
41-
app.use('*', (req, res) => {
42-
res.status(404).send('Not Found');
43-
});
44+
// create 404 error
45+
app.use('*', (req, res) => res.status(404).send('Not Found'));
4446

45-
// global error handler
46-
app.use((err, req, res, next) => {
47+
// create global error handler
48+
app.use((err, req, res) => {
4749
console.log(err);
4850
res.status(500).send('Internal Server Error');
49-
});
51+
});

0 commit comments

Comments
 (0)