-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
179 lines (154 loc) · 4.8 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
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
const express = require('express');
const server = express();
const db = require('./data/dbConfig');
const cors = require('cors');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const PORT = process.env.PORT || 8000; //This line is critical, must have process.env.PORT for Heroku
server.use(express.json());
server.use(cors());
let token = jwt.sign({
something: 'anything'
}, 'You have to know the password');
server.get('/', (req, res) => {
res.status(200).send('App is running');
})
server.get('/notes/:id', async (req, res) => {
if (req.params.id === 'all') {
try {
const notes = await db('notes');
res.status(200).json(notes);
} catch(err) {
return res.status(500).send(`Ya done goofed with error: ${err}`);
}
} else {
try {
const note = await db('notes').where('id', req.params.id).first();
const tags = await db('tags').where('note_id', req.params.id);
note.tags = tags;
res.status(200).json(note);
} catch (err) {
return res.status(500).send(`Ya done goofed with error: ${err}`)
}
}
});
server.get('/tags', async (req, res) => {
try {
const tags = await db('tags');
res.status(200).json(tags);
} catch(err) {
return res.status(500).send(`Error${err}...no tags for you`)
}
})
server.post('/notes', async(req, res) => {
const {title, textBody, user_id} = req.body;
try {
const ids = await db.insert({title, textBody, user_id}).into('notes');
const id = ids[0];
res.status(201).json(await db('notes').where('id', id).first());
} catch (err) {
res.status(404).send(`${err}...notes could not be created`);
}
});
server.post('/create-tag', async (req, res) => {
const {text, note_id} = req.body;
try {
const ids = await db.insert({text, note_id}).into('tags');
const id = ids[0];
res.status(201).json(await db('tags').where('note_id', note_id));
} catch(err) {
res.status(500).send(`${err}...tag could not be created`)
}
});
server.delete('/delete-tag/:id', async (req, res) => {
try {
const result = await db('tags').where('id', req.params.id).del();
if (result > 0) {
return res.status(200).json({status:'DELETED :)'});
}
} catch(err) {
return res.status(500).send(`Server error... ->${err}`)
}
})
server.put('/notes/:id', async(req, res) => {
const {title, textBody} = req.body;
try {
const result = await db('notes').where('id', req.params.id).first().update({
title,
textBody
});
if(result > 0) {
return res.status(200).json(await db('notes').where('id', req.params.id).first());
};
} catch(err) {
res.status(500).send(`${err}...did not put`)
}
});
server.delete('/notes/:id', async(req, res) => {
try {
const result = await db('notes').where('id', req.params.id).del();
if(result > 0) {
return res.status(200).json({result: "DELETED"})
}
} catch (err) {
res.status(500).send(`Server error...${err}`)
}
});
server.post('/register', async (req, res) => {
const user = req.body;
const hash = bcrypt.hashSync(user.password, 14);
user.password = hash;
try {
if (user.username && user.password) {
const ids = await db.insert(user).into('users');
const createdUser = await db('users').where('id', ids[0]).first();
createdUser.token = token;
res.status(200).json(createdUser);
}
} catch (err) {
res.status(500).send(`Server error...${err}`)
}
});
server.get('/users', async (req, res) => {
try {
res.status(200).json(await db('users'));
} catch (err) {
res.status(500).send(`${err}`)
}
})
server.get('/users/:id', async (req, res) => {
if (req.params.id) {
try {
res.status(200).json(await db('users').where('id', req.params.id));
} catch (err) {
res.status(404).send(`${err} user not found`)
}
}
})
server.get('/users/:id/notes', async (req, res) => {
try {
res.status(200).json(await db('notes').where('user_id', req.params.id));
} catch (err) {
res.status(500).send(`${err} Server error`);
}
})
server.post('/login', async (req, res) => {
try {
const credentials = req.body;
const foundUser = await db('users').where('username', credentials.username).first();
if (foundUser) {
const userHash = foundUser.password;
let isValid = bcrypt.compareSync(credentials.password, userHash);
if (isValid) {
return res.status(200).json({message: "Logged In", user: credentials.username, token:token, id:foundUser.id})
} else {
return res.status(401).send('Incorrect password')
}
} else {
return res.status(404).send('Error, no user exists with that name')
}
} catch (err) {
return res.status(401).json({message: `Server Error ${err}`});
}
})
server.listen(PORT, () => console.log(`App is listening with Heroku :)...on port ${PORT}`));