Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from hacettepeoyt/id-query
Browse files Browse the repository at this point in the history
Using _id while querying document for CRUD operations
  • Loading branch information
furkansimsekli authored Oct 10, 2022
2 parents 1dc3b62 + b0a25ad commit 97a2975
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
53 changes: 47 additions & 6 deletions controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,23 @@ const newProject = async (req, res) => {
}
}

const newFaq = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
const faq = {
question: req.body.question,
answer: req.body.answer
}

await Faq.insertMany(faq);
res.sendStatus(200);
} else {
res.sendStatus(401);
}
}

const deleteEvent = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
await Event.findOneAndDelete({ name: req.body.name });
await Event.findByIdAndDelete(req.body.id);
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -177,7 +191,7 @@ const deleteEvent = async (req, res) => {

const deleteCourse = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
await Course.findOneAndDelete({ name: req.body.name });
await Course.findByIdAndDelete(req.body.id);
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -186,7 +200,16 @@ const deleteCourse = async (req, res) => {

const deleteProject = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
await Project.findOneAndDelete({ name: req.body.name });
await Project.findByIdAndDelete(req.body.id);
res.sendStatus(200);
} else {
res.sendStatus(401);
}
}

const deleteFaq = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
await Faq.findByIdAndDelete(req.body.id);
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -205,7 +228,7 @@ const updateEvent = async (req, res) => {
duration: req.body.duration
}

await Event.findOneAndUpdate({ name: req.body.name }, event, { runValidators: true, new: true });
await Event.findByIdAndUpdate(req.body.id, event, { runValidators: true, new: true });
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -225,7 +248,7 @@ const updateCourse = async (req, res) => {
duration: req.body.duration
}

await Course.findOneAndUpdate({ name: req.body.name }, course, { runValidators: true, new: true });
await Course.findByIdAndUpdate(req.body.id, course, { runValidators: true, new: true });
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -242,7 +265,21 @@ const updateProject = async (req, res) => {
repository: req.body.repository
}

await Project.findOneAndUpdate({ name: req.body.name }, project, { runValidators: true, new: true });
await Project.findByIdAndUpdate(req.body.id, project, { runValidators: true, new: true });
res.sendStatus(200);
} else {
res.sendStatus(401);
}
}

const updateFaq = async (req, res) => {
if (req.body.auth === process.env.AUTH_KEY) {
const faq = {
question: req.body.question,
answer: req.body.answer
}

await Faq.findByIdAndUpdate(req.body.id, faq, { runValidators: true, new: true });
res.sendStatus(200);
} else {
res.sendStatus(401);
Expand All @@ -258,6 +295,7 @@ const getMembers = async (req, res) => {
csv += `${member.firstName},${member.lastName},${member.studentID},${member.degree},${member.email},${member.department},${member.mobileNumber},${member.groupChat}`;
csv += "\r\n";
}

res.send(csv);
} else {
res.sendStatus(401);
Expand All @@ -282,11 +320,14 @@ module.exports = {
newEvent,
newCourse,
newProject,
newFaq,
deleteEvent,
deleteCourse,
deleteProject,
deleteFaq,
updateEvent,
updateCourse,
updateProject,
updateFaq,
getMembers
}
3 changes: 3 additions & 0 deletions routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ router.post('/idea', controller.shareIdea);
router.post('/event', controller.newEvent);
router.post('/course', controller.newCourse);
router.post('/arge', controller.newProject);
router.post('/faq', controller.newFaq);

router.delete('/event', controller.deleteEvent);
router.delete('/course', controller.deleteCourse);
router.delete('/arge', controller.deleteProject);
router.delete('/faq', controller.deleteFaq);

router.patch('/event', controller.updateEvent);
router.patch('/course', controller.updateCourse);
router.patch('/arge', controller.updateProject);
router.patch('/faq', controller.updateFaq);


module.exports = router;
1 change: 1 addition & 0 deletions views/sections/arge.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
</div>
</div>
</div>
<span id="project-id" style="display: none;"><%= project._id.toString() %></span>
</div>
<% } %>
</div>
Expand Down
2 changes: 2 additions & 0 deletions views/sections/events.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
</div>
</div>
</div>
<span id="event-id" style="display: none;"><%= event._id.toString() %></span>
</div>
<% } %>
</div>
Expand Down Expand Up @@ -145,6 +146,7 @@
</div>
</div>
</div>
<span id="course-id" style="display: none;"><%= course._id.toString() %></span>
</div>
<% } %>
</div>
Expand Down
1 change: 1 addition & 0 deletions views/sections/faq.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target=<%=`#collapse${i}`%> aria-expanded="false" aria-controls=<%=`collapse${i}`%>>
<%= faq.question %>
</button>
<span id="faq-id" style="display: none;"><%= faq._id.toString() %></span>
</h2>
<!-- ANSWER -->
Expand Down

0 comments on commit 97a2975

Please sign in to comment.