-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated question, answer schema and made respective routes for implem…
…enting CRUD operations
- Loading branch information
1 parent
180d9a0
commit 32ececd
Showing
8 changed files
with
2,394 additions
and
55 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,24 @@ | ||
const express= require('express') | ||
const express = require("express"); | ||
const app = express(); | ||
const connectDB = require("./server/config/db"); | ||
const cors = require("cors"); | ||
const questionRoutes=require('./server/routes/questionRoutes'); | ||
const answerRoutes=require('./server/routes/answerRoutes'); | ||
|
||
const app=express(); | ||
const PORT = process.env.PORT || 5000; | ||
|
||
const PORT = process.env.PORT || 5000 | ||
connectDB(); | ||
|
||
app.get('/',(req,res)=>{ | ||
res.send("API is running") | ||
}) | ||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
app.listen(PORT,()=>{console.log('Server started on port '+PORT)}) | ||
app.use('/question',questionRoutes); | ||
app.use('/answer',answerRoutes); | ||
|
||
app.get("/", (req, res) => { | ||
res.send("API is running"); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log("Server started on port " + PORT); | ||
}); |
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,16 +1,17 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoose = require("mongoose"); | ||
|
||
const AnswerSchema = new mongoose.Schema({ | ||
|
||
Ques: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Quesion', | ||
}, | ||
|
||
Answer: { | ||
type: String, | ||
required: [true, 'Answer is required'], | ||
}, | ||
Answers: [ | ||
{ | ||
QuestionId: { | ||
type: Number, | ||
required:true | ||
}, | ||
AnswerId: { | ||
type: Number | ||
} | ||
}, | ||
], | ||
}); | ||
|
||
module.exports = Answer = mongoose.model('Answer', AnswerSchem); | ||
module.exports = Answers = mongoose.model("Answer", AnswerSchema); |
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,32 +1,35 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoose = require("mongoose"); | ||
|
||
const QuestionSchema = new mongoose.Schema({ | ||
|
||
Description: { | ||
type: [String], | ||
required: [true, 'Description is required'], | ||
}, | ||
|
||
Option1: { | ||
type: String, | ||
}, | ||
|
||
Option2: { | ||
type: String, | ||
}, | ||
|
||
Option3: { | ||
type: String, | ||
Qid: { | ||
type: Number, | ||
required: true, | ||
default: 0, | ||
}, | ||
|
||
Option4: { | ||
Description: { | ||
type: String, | ||
required: [true, "Description is required"], | ||
}, | ||
|
||
Ans: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Answer' | ||
Options: [ | ||
{ | ||
Text: { | ||
type: String, | ||
required: true, | ||
}, | ||
Oid: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}, | ||
], | ||
|
||
Solution: { | ||
type: Number, | ||
default: 0, | ||
required: true, | ||
}, | ||
}); | ||
|
||
module.exports = Question = mongoose.model('Question', QuestionSchema); | ||
module.exports = Question = mongoose.model("Question", QuestionSchema); |
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,64 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const Answer = require("../Models/Answer"); | ||
|
||
router.get("/", async (req, res) => { | ||
try { | ||
const answers = await Answer.find(); | ||
return res.status(200).json(answers); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.get("/:id", async (req, res) => { | ||
try { | ||
const answers = await Answer.findById(req.params.id); | ||
return res.status(200).json(answers); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.post("/create", async (req, res) => { | ||
try { | ||
const { Answers } = req.body; | ||
const answers = await Answer.create({ | ||
Answers, | ||
}); | ||
return res.status(201).json(answers); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.put("/:id", async (req, res) => { | ||
try { | ||
const { Answers } = req.body; | ||
const answer = await Answer.findById(req.params.id); | ||
|
||
if (!answer) { | ||
answer = await Answer.create({ | ||
Answers, | ||
}); | ||
return res.status(201).json(answer); | ||
} else { | ||
answer.Answers=Answers; | ||
await answer.save(); | ||
return res.status(200).json(answer); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.delete("/:id", async (req, res) => { | ||
try { | ||
const answers = await Answer.findByIdAndDelete(req.params.id); | ||
return res.status(200).json(answers); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const Question = require("../Models/Question"); | ||
|
||
router.get("/", async (req, res) => { | ||
try { | ||
const questions = await Question.find(); | ||
return res.status(200).json(questions); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.get("/:id", async (req, res) => { | ||
try { | ||
const questions = await Question.findById(req.params.id); | ||
return res.status(200).json(questions); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.post("/create", async (req, res) => { | ||
try { | ||
const { Qid, Description, Options, Solution } = req.body; | ||
const question = await Question.create({ | ||
Qid, | ||
Description, | ||
Options, | ||
Solution, | ||
}); | ||
return res.status(201).json(question); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.put("/:id", async (req, res) => { | ||
try { | ||
const { Qid, Description, Options, Solution } = req.body; | ||
const question = await Question.findById(req.params.id); | ||
|
||
if (!question) { | ||
question = await Question.create({ | ||
Qid, | ||
Description, | ||
Options, | ||
Solution, | ||
}); | ||
return res.status(201).json(question); | ||
} else { | ||
question.Description = Description; | ||
question.Options = Options; | ||
question.Solution = Solution; | ||
await question.save(); | ||
return res.status(200).json(question); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
router.delete("/:id", async (req, res) => { | ||
try { | ||
const questions = await Question.findByIdAndDelete(req.params.id); | ||
return res.status(200).json(questions); | ||
} catch (error) { | ||
return res.status(500).json({ msg: error.message }); | ||
} | ||
}); | ||
|
||
module.exports = router; |