This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
ACTECH-91 #10
Open
Genyus
wants to merge
3
commits into
main
Choose a base branch
from
ACTECH-91
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ACTECH-91 #10
Changes from all commits
Commits
Show all changes
3 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 hidden or 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,2 +1,3 @@ | ||
PORT=8080 | ||
DATABASE_CONNECTION_STRING="PUT YOUR DATABASE CONNECTION STRING HERE" | ||
DATABASE_CONNECTION_STRING="<COPY CONNECTION STRING FROM MONGODB ATLAS>" | ||
MONGO_DB_NAME="example_db" # REPLACE THIS WITH A RELEVANT NAME FOR YOUR PROJECT |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,29 +1,29 @@ | ||
require("dotenv").config(); | ||
|
||
const express = require("express"); | ||
const mongoose = require("mongoose"); | ||
const MongoClient = require("mongodb").MongoClient; | ||
const bodyParser = require("body-parser"); | ||
const cors = require("cors"); | ||
|
||
// IMPORT YOUR SCHEMAS HERE | ||
require("./models/Profiles"); //This is just an example. Don't forget to delete this | ||
|
||
const PORT = process.env.PORT; | ||
const app = express(); | ||
|
||
// This is where your API is making its initial connection to the database | ||
mongoose.Promise = global.Promise; | ||
mongoose.set("strictQuery", false); | ||
mongoose.connect(process.env.DATABASE_CONNECTION_STRING, { | ||
useNewUrlParser: true, | ||
}); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(cors()); | ||
|
||
// IMPORT YOUR API ROUTES HERE | ||
// Below is just an example. Don't forget to delete it. | ||
// It's importing and using everything from the profilesRoutes.js file and also passing app as a parameter for profileRoutes to use | ||
require("./routes/profilesRoutes")(app); | ||
// Connect to the database | ||
MongoClient.connect(process.env.DATABASE_CONNECTION_STRING) | ||
.then((client) => { | ||
const db = client.db(process.env.MONGO_DB_NAME); | ||
// IMPORT YOUR API ROUTES HERE | ||
// Below is just an example. Don't forget to delete it. | ||
// It's importing and using everything from the profilesRoutes.js file and also passing app as a parameter for profileRoutes to use | ||
require("./routes/profilesRoutes")(app, db); | ||
|
||
const PORT = process.env.PORT; | ||
app.listen(PORT, () => { | ||
console.log(`API running on port ${PORT}`); | ||
}); | ||
app.listen(PORT, () => { | ||
console.log(`API running on port ${PORT}`); | ||
}); | ||
}) | ||
.catch((err) => { | ||
console.error("Error: ", err); | ||
}); | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,43 +1,91 @@ | ||
const mongoose = require("mongoose"); | ||
const Profile = mongoose.model("profiles"); | ||
const { ObjectId } = require("mongodb"); | ||
|
||
const profileRoutes = (app) => { | ||
app.get(`/api/profile`, async (req, res) => { | ||
const profiles = await Profile.find(); | ||
/** | ||
* @param {import('express').Express} app - The Express instance | ||
* @param {import('mongodb').Db} db - The Db instance. | ||
*/ | ||
const profilesRoutes = (app, db) => { | ||
/** | ||
* Retrieves the profiles collection from Mongo db | ||
* @returns Collection<Document> | ||
*/ | ||
const profilesCollection = () => db.collection("profiles"); | ||
|
||
return res.status(200).send(profiles); | ||
/** | ||
* Middleware handler for GET requests to /api/profiles path | ||
*/ | ||
app.get(`/api/profiles`, async (req, res) => { | ||
try { | ||
// Waits for asynchronous `find()` operation to complete and converts results to array | ||
const profiles = await profilesCollection().find({}).toArray(); | ||
|
||
return res.status(200).send(profiles); | ||
} catch (e) { | ||
return res | ||
.status(500) | ||
.send(`Error occurred while retrieving profiles: ${e}`); | ||
} | ||
}); | ||
|
||
app.post(`/api/profile`, async (req, res) => { | ||
const profile = await Profile.create(req.body); | ||
/** | ||
* Middleware handler for POST requests to /api/profiles path | ||
*/ | ||
app.post(`/api/profiles`, async (req, res) => { | ||
try { | ||
const profile = await profilesCollection().insertOne(req.body); | ||
|
||
return res.status(201).send({ | ||
error: false, | ||
profile, | ||
}); | ||
return res.status(201).send({ | ||
error: false, | ||
profile, | ||
}); | ||
} catch (e) { | ||
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. Could this be updated to be error instead of e? Will help reinforce that we mean error and not something else such as event |
||
return res | ||
.status(500) | ||
.send(`Error occurred while creating profile: ${e}`); | ||
} | ||
}); | ||
|
||
app.put(`/api/profile/:id`, async (req, res) => { | ||
const { id } = req.params; | ||
|
||
const profile = await Profile.findByIdAndUpdate(id, req.body); | ||
/** | ||
* Middleware handler for PUT requests to /api/profiles/:id path | ||
*/ | ||
app.put(`/api/profiles/:id`, async (req, res) => { | ||
try { | ||
// Captures target id from URL | ||
const { id } = req.params; | ||
// Builds query matching `_id` field value matching captured id. `ObjectId()` is needed to convert string value to correct type | ||
const query = { _id: new ObjectId(id) }; | ||
const profile = await profilesCollection().replaceOne(query, req.body); | ||
|
||
return res.status(202).send({ | ||
error: false, | ||
profile, | ||
}); | ||
return res.status(202).send({ | ||
error: false, | ||
profile, | ||
}); | ||
} catch (e) { | ||
return res | ||
.status(500) | ||
.send(`Error occurred while updating profilee: ${e}`); | ||
} | ||
}); | ||
|
||
app.delete(`/api/profile/:id`, async (req, res) => { | ||
const { id } = req.params; | ||
|
||
const profile = await Profile.findByIdAndDelete(id); | ||
/** | ||
* Middleware handler for DELETE requests to /api/profiles/:id path | ||
*/ | ||
app.delete(`/api/profiles/:id`, async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const query = { _id: new ObjectId(id) }; | ||
const profile = await profilesCollection().deleteOne(query); | ||
|
||
return res.status(202).send({ | ||
error: false, | ||
profile, | ||
}); | ||
return res.status(202).send({ | ||
error: false, | ||
profile, | ||
}); | ||
} catch (e) { | ||
return res | ||
.status(500) | ||
.send(`Error occurred while deleting profiles: ${e}`); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = profileRoutes; | ||
module.exports = profilesRoutes; |
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.
Missing empty line at the end of this file