Skip to content

Commit 13f20e1

Browse files
Merge pull request #87 from oslabs-beta/Swell8-dev
Merge refactor http server to not use remote db
2 parents c1649cc + 30f401b commit 13f20e1

File tree

1 file changed

+28
-51
lines changed

1 file changed

+28
-51
lines changed

test/mockController.js

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,57 @@
1-
const mongoose = require('mongoose');
2-
const db = require('./dbModel');
1+
// const mongoose = require('mongoose');
2+
let db = [];
33

4-
mongoose.set('useFindAndModify', false);
4+
// mongoose.set('useFindAndModify', false);
55

66
const bookController = {};
77

88
bookController.clearDB = (req, res, next) => {
9-
db.BookStore.deleteMany({}, (err) => {
10-
if (err) next(err);
11-
next()
12-
});
9+
db = [];
10+
return next()
1311
}
1412

1513
bookController.getAll = (req, res, next) => {
16-
db.BookStore.find({}, (err, books) => {
17-
if (err) {
18-
next(err);
19-
}
20-
res.locals.books = books;
21-
// console.log(books);
22-
next();
23-
})
14+
res.locals.books = db;
15+
return next();
2416
}
2517

2618
bookController.addBook = (req, res, next) => {
2719
const { title, author, pages } = req.body;
28-
db.BookStore.create({ title, author, pages }, (err, books) => {
29-
if (err) {
30-
next(err);
31-
}
32-
res.locals.books = books;
33-
next();
34-
})
20+
const book = {title, author, pages};
21+
db.push(book);
22+
res.locals.books = db;
23+
return next();
3524
}
3625

3726
bookController.updateEntireBook = (req, res, next) => {
3827
const { title } = req.params;
3928
const { author, pages } = req.body;
40-
db.BookStore.replaceOne({ title }, { title, author, pages }, (err, books) => {
41-
if (err) {
42-
next(err);
43-
}
44-
if (books.n === 1) {
45-
db.BookStore.findOne({title}, (err, books) => {
46-
if (err) {
47-
next(err);
48-
}
49-
50-
res.locals.books = books;
51-
next();
52-
})
53-
}
54-
})
29+
const book = db.filter(book => book.title === title);
30+
book[0].title = title;
31+
book[0].author = author;
32+
book[0].pages = pages;
33+
db = [...db, ...book];
34+
res.locals.books = db;
35+
return next();
5536
}
5637

5738
bookController.patchBook = (req, res, next) => {
5839
const { title } = req.params;
5940
const { author } = req.body;
60-
db.BookStore.findOneAndUpdate({ title }, { title, author }, {new: true}, (err, books) => {
61-
if (err) {
62-
next(err);
63-
}
64-
res.locals.books = books;
65-
next();
66-
})
41+
const book = db.filter(book => book.title === title);
42+
book[0].title = title;
43+
book[0].author = author;
44+
db = [...db, ...book];
45+
res.locals.books = db;
46+
return next();
6747
}
6848

6949
bookController.deleteBook = (req, res, next) => {
7050
const { title } = req.params;
71-
db.BookStore.findOneAndDelete({ title }, (err, books) => {
72-
if (err) {
73-
next(err);
74-
}
75-
res.locals.books = books;
76-
next();
77-
})
51+
const book = db.filter(book => book.title === title)[0];
52+
db = db.filter(book => book.title !== title);
53+
res.locals.books = book;
54+
return next();
7855
}
7956

8057
module.exports = bookController;

0 commit comments

Comments
 (0)