-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Routes -> Controllers -> Model fo r User Entity and Mongo DB set up #37
Merged
L0Lmaker
merged 2 commits into
main
from
Andy/2_Route_Controller_Model_For_User_And_DB_Setup
May 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"mongodb": "^5.5.0", | ||
"mongoose": "^7.2.1" | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
# misc | ||
.DS_Store | ||
.env | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require('dotenv').config() | ||
|
||
const MONGO_USERNAME = process.env.MONGO_USERNAME || '' | ||
const MONGO_PASSWORD = process.env.MONGO_PASSWORD || '' | ||
const MONGO_URI = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@cluster0.4bflh41.mongodb.net/BugStormDB?retryWrites=true&w=majority` | ||
const SERVER_PORT = process.env.PORT ? Number(process.env.PORT) : 5001 | ||
Comment on lines
+1
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i changed this to use port 5001, some mac has problems with port 5000 https://developer.apple.com/forums/thread/682332 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem :) |
||
const ENV = process.env.ENV || '' | ||
const JWT_SECRET = process.env.JWT_SECRET || '' | ||
|
||
const config = { | ||
mongo: { | ||
uri: MONGO_URI, | ||
}, | ||
server: { | ||
port: SERVER_PORT, | ||
env: ENV, | ||
JWT_SECRET, | ||
}, | ||
} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const UserController = require('./UserController') | ||
|
||
const userController = new UserController() | ||
|
||
const controllers = { | ||
userController: userController, | ||
} | ||
|
||
module.exports = controllers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const UserModel = require('../models/UserModel') | ||
|
||
class UserController { | ||
constructor() {} | ||
|
||
async getAll() { | ||
try { | ||
const users = await UserModel.find() | ||
return users | ||
} catch (error) { | ||
console.error('Error while retrieving users:', error) | ||
throw error | ||
} | ||
} | ||
|
||
async createUser(userData) { | ||
try { | ||
const newUser = await UserModel.create(userData) | ||
return newUser | ||
} catch (error) { | ||
console.error('Error while creating user:', error) | ||
throw error | ||
} | ||
} | ||
|
||
async updateUser(userId, updateData) { | ||
try { | ||
const updatedUser = await UserModel.findByIdAndUpdate( | ||
userId, | ||
updateData, | ||
{ | ||
new: true, // Return the updated user | ||
}, | ||
) | ||
return updatedUser | ||
} catch (error) { | ||
console.error('Error while updating user:', error) | ||
throw error | ||
} | ||
} | ||
|
||
async deleteUser(userId) { | ||
try { | ||
const deletedUser = await UserModel.findByIdAndRemove(userId) | ||
return deletedUser | ||
} catch (error) { | ||
console.error('Error while deleting user:', error) | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
module.exports = UserController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
const express = require('express') | ||
const mongoose = require('mongoose') | ||
|
||
const UserRoute = require('./routes/UserRoute') | ||
const config = require('./config/config') | ||
const app = express() | ||
const port = 5000 | ||
app.use(express.json()) | ||
|
||
const apiRouter = express.Router() | ||
app.use('/api', apiRouter) | ||
|
||
const userRoute = new UserRoute() | ||
userRoute.initRoutes(apiRouter) | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello, world!') | ||
}) | ||
|
||
app.listen(port, () => { | ||
console.log(`Server is running on port ${port}`) | ||
}) | ||
const connectDB = async () => { | ||
try { | ||
await mongoose.connect(config.mongo.uri) | ||
startServer() | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
const startServer = () => { | ||
app.listen(config.server.port, () => { | ||
console.log(`Server is running on port ${config.server.port}`) | ||
}) | ||
} | ||
|
||
connectDB() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const userSchema = new mongoose.Schema({ | ||
username: { | ||
type: String, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
}) | ||
|
||
module.exports = mongoose.model('User', userSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const express = require('express') | ||
const controllers = require('../controllers/Controllers') | ||
|
||
class UserRoute { | ||
constructor() { | ||
this.router = express.Router() | ||
// the this.getAll.bind(this) is basically saying | ||
// when I hit /api/example in a get request, I am going | ||
// to call the getAll function | ||
this.router.get('', this.getAll.bind(this)) | ||
} | ||
|
||
initRoutes(apiRouter) { | ||
apiRouter.use('/user', this.router) | ||
} | ||
|
||
async getAll(req, res) { | ||
try { | ||
const response = await controllers.userController.getAll() | ||
res.json(response) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
} | ||
|
||
module.exports = UserRoute |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might have accidentally install the dependencies in root. should be fine though. will be resolved in my pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yea oops I only removed the node module