Skip to content

Commit 973c40f

Browse files
authored
Merge pull request #3 from junerockwell/mongodb-atlas
Add MongoDB Atlas
2 parents 946e85e + 314dc50 commit 973c40f

File tree

11 files changed

+2446
-1
lines changed

11 files changed

+2446
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ typings/
6969
.yarn-integrity
7070

7171
# dotenv environment variables file
72-
.env
72+
# .env # committing `.env` files in this case for demonstration
7373
.env.test
7474

7575
# parcel-bundler cache (https://parceljs.org/)

API_MongoDB_Atlas/.env

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ENV = 'development'
2+
PORT = 3000
3+
BASE_URL = 'http://localhost:3000',
4+
MONGODB_URI = 'mongodb+srv://<dbUser>:<dbUserPassword>@cluster0.2exue.mongodb.net/<dbname>?retryWrites=true&w=majority'
5+
JWT_SECRET = 'api_jwt_secret'
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const Note = require('./NoteModel');
2+
const errors = require('restify-errors');
3+
4+
async function GetOneNoteById(id) {
5+
try {
6+
const note = await Note.findById(id);
7+
8+
return {
9+
code: 200,
10+
note,
11+
};
12+
}
13+
catch (error) {
14+
global.logger.error(error);
15+
return { code: new errors.InternalServerError() };
16+
}
17+
}
18+
19+
async function GetNotesByAuthorId(id) {
20+
try {
21+
const note = await Note.find({ authorId: id });
22+
23+
return {
24+
code: 200,
25+
note,
26+
};
27+
}
28+
catch (error) {
29+
global.logger.error(error);
30+
return { code: new errors.InternalServerError() };
31+
}
32+
}
33+
34+
async function CreateOneNote(data) {
35+
try {
36+
const note = await new Note({
37+
note: data.note,
38+
authorId: data.authorId
39+
}).save();
40+
41+
return {
42+
code: 201,
43+
message: '',
44+
note
45+
}
46+
}
47+
catch(error) {
48+
global.logger.error(error);
49+
return { code: new errors.InternalServerError() };
50+
}
51+
}
52+
53+
async function GetAllNotes() {
54+
try {
55+
const notes = await Note.find({});
56+
return {
57+
code: 200,
58+
notes
59+
};
60+
}
61+
catch (error) {
62+
global.logger.error(error);
63+
return { code: new errors.InternalServerError() };
64+
}
65+
}
66+
67+
async function UpdateOneNote(id, newNote) {
68+
try {
69+
const note = await Note.findOneAndUpdate({ _id: id }, { note: newNote }, { new: true });
70+
return {
71+
code: 200,
72+
note,
73+
}
74+
}
75+
catch(error) {
76+
global.logger.error(error);
77+
return { code: new errors.InternalServerError() };
78+
}
79+
}
80+
81+
async function DeleteOneNoteById(id) {
82+
try {
83+
const note = await Note.findOneAndDelete({ _id: id });
84+
85+
return {
86+
code: 200,
87+
note,
88+
}
89+
}
90+
catch(error) {
91+
global.logger.error(error);
92+
return { code: new errors.InternalServerError() };
93+
}
94+
}
95+
96+
module.exports = {
97+
GetOneNoteById,
98+
GetNotesByAuthorId,
99+
CreateOneNote,
100+
GetAllNotes,
101+
UpdateOneNote,
102+
DeleteOneNoteById
103+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require('mongoose');
2+
const timestamp = require('mongoose-timestamp');
3+
4+
const NoteSchema = new mongoose.Schema({
5+
note: {
6+
type: String,
7+
required: true,
8+
},
9+
authorId: {
10+
type: String,
11+
// required: true
12+
}
13+
});
14+
15+
/* Plugins
16+
============================================= */
17+
NoteSchema.plugin(timestamp);
18+
19+
/* Methods
20+
============================================= */
21+
22+
module.exports = mongoose.model('Note', NoteSchema);
23+
24+
25+
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const User = require('./UserModel');
2+
const errors = require('restify-errors');
3+
const jwt = require('jsonwebtoken');
4+
5+
async function UserExists(data) {
6+
try {
7+
const user = await User.findOne({ email: data.email }).exec();
8+
return user ? true : false;
9+
} catch (error) {
10+
global.logger.error(error);
11+
return false;
12+
}
13+
}
14+
15+
async function CreateNewUser(data) {
16+
const exists = await UserExists(data);
17+
if (exists) {
18+
return new errors.InvalidArgumentError();
19+
}
20+
else {
21+
const { firstname, lastname, email, password } = data;
22+
23+
const user = new User({
24+
firstname,
25+
lastname,
26+
email,
27+
password
28+
});
29+
30+
const hash = user.generateHash(password);
31+
user.password = hash;
32+
33+
try {
34+
const newUser = await user.save();
35+
const token = jwt.sign(user.toJSON(), process.env.JWT_SECRET, { expiresIn: '10m' });
36+
const { iat, exp } = jwt.decode(token);
37+
return {
38+
code: 201,
39+
message: '',
40+
token,
41+
iat,
42+
exp,
43+
user: newUser
44+
};
45+
}
46+
catch (error) {
47+
global.logger.error(error);
48+
return { code: new errors.InternalError(error.message) };
49+
}
50+
}
51+
}
52+
53+
async function LoginAUser(data) {
54+
try {
55+
const user = await User.findOne({ email: data.email }).exec();
56+
57+
if (!user || !user.validPassword(data.password, user.password)) {
58+
return { code: new errors.UnauthorizedError() };
59+
}
60+
61+
const token = jwt.sign(user.toJSON(), process.env.JWT_SECRET, { expiresIn: '10m' });
62+
const { iat, exp } = jwt.decode(token);
63+
64+
return {
65+
code: 200,
66+
message: '',
67+
token,
68+
iat,
69+
exp,
70+
user,
71+
}
72+
73+
} catch (error) {
74+
global.logger.error(error);
75+
return { code: new errors.InternalServerError() };
76+
}
77+
}
78+
79+
module.exports = {
80+
LoginAUser,
81+
UserExists,
82+
CreateNewUser,
83+
};
84+
85+
86+
87+
88+
89+
90+
91+
92+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const mongoose = require('mongoose');
2+
const bcrypt = require('bcryptjs');
3+
const timestamp = require('mongoose-timestamp');
4+
5+
const UserSchema = new mongoose.Schema({
6+
firstname: {
7+
type: String,
8+
required: true,
9+
trim: true,
10+
},
11+
lastname: {
12+
type: String,
13+
required: true,
14+
trim: true,
15+
},
16+
email: {
17+
type: String,
18+
required: true,
19+
trim: true,
20+
},
21+
password: {
22+
type: String,
23+
required: true
24+
},
25+
});
26+
27+
/* Plugins
28+
============================================= */
29+
UserSchema.plugin(timestamp);
30+
31+
/* Methods
32+
============================================= */
33+
UserSchema.methods.generateHash = password => {
34+
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
35+
};
36+
37+
UserSchema.methods.validPassword = (givenPassword, userPassword) => {
38+
return bcrypt.compareSync(givenPassword, userPassword);
39+
};
40+
41+
module.exports = mongoose.model('User', UserSchema);
42+
43+
44+
45+
46+

0 commit comments

Comments
 (0)