-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (118 loc) · 4.29 KB
/
server.js
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
'use strict';
const express = require('express');
const app = express();
require('dotenv').config();
const cors = require('cors');
app.use(cors());
app.use(express.json());
// require mongoose
const mongoose = require('mongoose');
// hey mongoose, connect to the database at localhost:27017
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true });
const User = require('./models/User');
// const userProfile = new User({
// // userEmail: '[email protected]',
// // favoriteImages: [
// // {
// // title: 'howling wolf',
// // date: '2021-05-15',
// // explaination: 'stufffffffffffffffffffffff and things',
// // url: 'https://zen-varahamihira-8a9600.netlify.app/static/media/wolf-space.1cd72378.jpg',
// // }
// // ],
// // userEmail: '[email protected]',
// // favoriteImages: [
// // {
// // title: 'NGC 602 and Beyond',
// // date: '2021-05-16',
// // explanation: 'The clouds may look like an oyster, and the stars like pearls, but look beyond. Near the outskirts of the Small Magellanic Cloud, a satellite galaxy some 200 thousand light-years distant, lies 5 million year young star cluster NGC 602. Surrounded by natal gas and dust, NGC 602 is featured in this stunning Hubble image of the region.',
// // url: 'https://apod.nasa.gov/apod/image/2105/Ngc602_Hubble_960.jpg',
// // }
// // ]
// });
// userProfile.save().then(() => console.log('successfully saved', userProfile));
const PORT = process.env.PORT || 3001;
app.get('/', (req, res) => {
res.send('🚀 Hello Fellow Space Cadet');
});
// app.get('/userdata', (req, res) => {
// User.find((arr, userData) => {
// res.send(userData);
// });
// });
app.get('/user', (req, res) => {
console.log('request :', req.query);
User.find({ userEmail: req.query.userEmail }, (err, databaseResults) => {
console.log('data base results', databaseResults);
res.send(databaseResults);
});
});
app.post('/user', (req, res) => {
console.log('====', req.body);
//Check if user exists
User.find({ userEmail: req.body.email}, (err, databaseResults) => {
console.log(databaseResults);
console.log(req.body.email);
if (databaseResults.length < 1) {
// res.status(400).send('Error: user not found');
//If user is not there create a new user
let newUser = new User({
userEmail: req.body.email,
favoriteImages:
[{
title: req.body.title,
date: req.body.date,
url: req.body.url,
}],
});
newUser.save().then(newUserData => {
res.send(newUserData.favoriteImages);
});
} else {
let foundUser = databaseResults[0];
// let user = userData[0];
let flag = false;
for (let i=0; i < foundUser.favoriteImages.length; i++) {
if (foundUser.favoriteImages[i].url === req.body.url) flag = true;
}
if (flag === true) {
console.log('image already saved to database');
res.send(foundUser.favoriteImages);
} else {
foundUser.favoriteImages.push({
title: req.body.title,
date: req.body.date,
url: req.body.url,
});
foundUser.save().then((databaseResults) => {
console.log('image not in database', databaseResults);
res.send(databaseResults.favoriteImages);
});
}
// console.log('inside else');
//If user is found, add favorite images info to that user
// foundUser.favoriteImages.push({
// title: req.body.title,
// date: req.body.date,
// url: req.body.url,
// });
// foundUser.save().then((databaseResults) => {
// console.log(databaseResults);
// res.send(databaseResults.favoriteImages);
// });
}
});
});
app.delete('/user/:id', (req, res) => {
console.log('delete called');
let userEmail = req.query.userEmail;
User.find({ userEmail: userEmail }, (err, userData) => {
let user = userData[0];
user.favoriteImages = user.favoriteImages.filter(image => `${image._id}` !== req.params.id);
user.save().then(userData => {
res.send(userData.favoriteImages);
});
});
});
console.log('❤️ Hello sPaCe CaDeTs welcome to the back-end! ❤️');
app.listen(PORT, () => console.log(`Listening on ${PORT}`));