-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
197 lines (161 loc) · 5.75 KB
/
server.js
File metadata and controls
197 lines (161 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
const express = require('express');
const cors = require('cors');
require('dotenv').config();
// const Data=require('./Data');
// const seedBookCollection=require('./Data');
// const seedOwnerUserCollection=require('./Data');
// const getBookHandler=require('./Data');
// const addBookHandler=require('./Data');
const server = express();
server.use(express.json());
server.use(cors());
const PORT = process.env.PORT;
// seedOwnerUserCollection();
// http://localhost:3001/book?email=leana_baba@yahoo.com
server.get('/book',getBookHandler);
server.post('/addBook',addBookHandler);
server.delete('/deleteBook/:index',deleteBookHandler);
server.put('/updateBook/:index',updateBookHandler)
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI,
{ useNewUrlParser: true, useUnifiedTopology: true });
const bookSchema = new mongoose.Schema({
name: String,
description: String,
img: String,
status: String
});
const userSchema = new mongoose.Schema({
email: String,
books: [bookSchema]
});
const bookUser = mongoose.model('book', bookSchema);
const ownerUser = mongoose.model('user', userSchema);
function seedBookCollection(){
const book1 = new bookUser ({
name: 'Living in the Light: A guide to personal transformation',
description: 'Learn to light a candle in the darkest moments of someone’s life. Be the light that helps others see; it is what gives life its deepest significance.',
img:`https://m.media-amazon.com/images/I/411a3QxUrPL.jpg`,
status: 'Still reading.'
})
const book2 = new bookUser ({
name: 'Give and Take: WHY HELPING OTHERS DRIVES OUR SUCCESS',
description: 'Everything in the universe is within you. Ask all from yourself.',
img:`https://m.media-amazon.com/images/I/41PDasOQTxL.jpg`,
status: 'Still reading.'
})
const book3 = new bookUser ({
name: `13 Things Mentally Strong People Don't Do`,
description: `Make yourself a priority once in a while. It's not selfish. It's necessary.`,
img: `https://images-na.ssl-images-amazon.com/images/I/41JQFNXxfdL._SX326_BO1,204,203,200_.jpg`,
status: 'Still reading.'
})
book1.save();
book2.save();
book3.save();
}
// seedBookCollection();
function seedOwnerUserCollection(){
const account1 = new ownerUser ({
email: `lolo.baba2014@gmail.com`,
books:[
{
name: 'Living in the Light: A guide to personal transformation',
description: 'Learn to light a candle in the darkest moments of someone’s life. Be the light that helps others see; it is what gives life its deepest significance.',
img:`https://m.media-amazon.com/images/I/411a3QxUrPL.jpg`,
status: 'Still reading.'
}
]
})
const account2 = new ownerUser ({
email: `leana_baba@yahoo.com`,
books:[
{
name: 'Give and Take: WHY HELPING OTHERS DRIVES OUR SUCCESS',
description: 'Everything in the universe is within you. Ask all from yourself.',
img:`https://m.media-amazon.com/images/I/41PDasOQTxL.jpg`,
status: 'Still reading.'
},
{
name: `13 Things Mentally Strong People Don't Do`,
description: `Make yourself a priority once in a while. It's not selfish. It's necessary.`,
img: `https://images-na.ssl-images-amazon.com/images/I/41JQFNXxfdL._SX326_BO1,204,203,200_.jpg`,
status: 'Still reading.'
}
]
})
account1.save();
account2.save();
}
// seedOwnerUserCollection();
function getBookHandler(req,res){
let userName=req.query.email;
ownerUser.find({email:userName},function(err,ownerData){
if(err) {
console.log('did not work')
} else {
// console.log(ownerData)
// console.log(ownerData[0])
// console.log(ownerData[0].books)
res.send(ownerData[0].books)
}
})
}
function addBookHandler(req,res){
console.log(req.body);
let userName=req.body.email;
console.log(userName);
const {name,description,img,status} = req.body;
ownerUser.find({email:userName},(error,userData)=>{
if(error){res.send('not working')}
else {
console.log('before pushing',userName)
userData[0].books.push({
name: name,
description: description,
img:img,
status:status
})
console.log('after pushing',userData[0].books)
userData[0].save();
res.send(userData[0].books);
}
})
}
function deleteBookHandler (req,res){
const email = req.query.email;
const index = Number(req.params.index);
ownerUser.find({email:email},(error,ownerData)=>{
// filter the cats for the owner and remove the one that matches the index
const newBookArr = ownerData[0].books.filter((item,idx)=>{
if( idx !== index) return item;
// return idx !==index
})
ownerData[0].books = newBookArr;
ownerData[0].save();
res.send(ownerData[0].books)
})
}
function updateBookHandler (req,res){
console.log(req.body);
const {email,name,description,img,status}=req.body;
const index = Number(req.params.index);
ownerUser.findOne({email:email},(error,ownerData)=>{
// filter the cats for the owner and remove the one that matches the index
ownerData.books.splice(index,1,{
name: name,
description: description,
img:img,
status:status
})
ownerData.save();
res.send(ownerData.books)
})
}
server.listen(PORT, () => {
console.log(`Listenging on PORT ${PORT}`);
})
server.get('/', (req, res) => {
res.send('The home route')
})