-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Verma314/aditya-verma
added business logic for asking questions, added relevant controllers…
- Loading branch information
Showing
6 changed files
with
162 additions
and
10 deletions.
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,2 @@ | ||
node_modules | ||
users.txt |
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,72 @@ | ||
# Building a sample Q&A website such as StackOverflow or Reddit | ||
|
||
## to do | ||
|
||
* [X] login, registeration | ||
* [ ] adding HTTP error codes for faliure etc | ||
* [X] mongodb connection | ||
* [ ] refine login, registeration | ||
* [ ] adding JWT tokens for login | ||
* [X] adding session for _logged in_ user | ||
* [X] creating endpoint for asking a question | ||
* [ ] GET endpoint for enumerating all questions | ||
* [ ] POST endpoint for answering a question | ||
* [ ] GET endpoint for enumerating answers to a question | ||
* [ ] Endpoint to upvote a question or an answer | ||
* [ ] Endpoint to get stats of a user | ||
* [ ] Deploying this to cloud | ||
|
||
|
||
|
||
## Objects | ||
|
||
### 1. User | ||
- username | ||
- password | ||
|
||
### 2. Post | ||
- ```question``` | ||
- List of ```Answer```s | ||
- ```User``` | ||
|
||
### 3. Question | ||
- ```Statement``` | ||
- List of ```Answer``` strings | ||
- ```Upvotes``` | ||
- ```User``` | ||
|
||
### 4. Answer | ||
- ```Statement``` | ||
- ```Upvotes``` | ||
- ```User``` | ||
|
||
-- | ||
|
||
## Public Endpoints | ||
1. POST http://localhost:8080/login | ||
|
||
Header: | ||
Content-Type: application/x-www-form-urlencoded | ||
|
||
Body: | ||
username - <username> | ||
password - <password> | ||
|
||
2. GET http://localhost:8080/session | ||
|
||
to check who is logged in | ||
|
||
3. GET http://localhost:8080/logout | ||
|
||
to log the user out. | ||
|
||
4. POST http://localhost:8080/register | ||
|
||
to register a new user | ||
|
||
Header: | ||
Content-Type: application/x-www-form-urlencoded | ||
|
||
Body: | ||
username - <username> | ||
password - <password> |
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,41 @@ | ||
const dbUtilsQnA = require('../dao/dbUtilsQnA') | ||
|
||
module.exports = function (app) { | ||
|
||
app.get('/', function (req, res) { | ||
res.send('Hello world !'); | ||
}); | ||
|
||
app.post('/ask', function (req, res) { | ||
|
||
//perform validations here on the req | ||
console.log(req.body); | ||
console.log( "Logged in user: " + req.session.loggedInUser); | ||
|
||
if ( req.session.loggedInUser == null ) { | ||
res.status(400).send("User not logged in!"); | ||
return; | ||
} | ||
|
||
req.body["username"] = req.session.loggedInUser; | ||
|
||
|
||
dbUtilsQnA.askQuestion(req.body, function (result,err) { | ||
if ( err == null ) { | ||
res.send("Question Submitted!"); | ||
} else { | ||
res.status(500).send("Failed to ask question!"); | ||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
app.get('/2', function (req, res) { | ||
res.send('Hello world !'); | ||
}); | ||
|
||
|
||
|
||
}; |
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,22 @@ | ||
const MongoClient = require('mongodb').MongoClient | ||
fs = require('fs'); | ||
|
||
|
||
const connectionString = "mongodb+srv://admin:[email protected]/Stackover?retryWrites=true&w=majority"; | ||
|
||
module.exports.askQuestion = function (questionObj, callback) { | ||
MongoClient.connect(connectionString, (err, client) => { | ||
if (err) return console.error(err) | ||
console.log('Connected to Database') | ||
|
||
const db = client.db('Stackover') | ||
const question = db.collection('Questions') | ||
|
||
question.insertOne(questionObj) | ||
.then(result => { | ||
console.log(result) | ||
callback(result,null); | ||
}) | ||
.catch(error => callback(null,error)) | ||
}) | ||
}; |
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