Skip to content

Commit

Permalink
reorganize the backend files
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-mitra committed Oct 6, 2020
1 parent b60176c commit 9676179
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 92 deletions.
6 changes: 4 additions & 2 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ var express = require("express");
var app = express();
var cors = require("cors");
var bodyParser = require("body-parser");
var morgan = require('morgan');

app.use(morgan('combined'));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var QuestionRoute = require("./routes/Questions");
var CodeRoutes = require("./routes/CodeRoutes");
var Leaderboard = require("./routes/leaderboard");
var LeaderboardRoutes = require("./routes/Leaderboard");
var AdminRoutes = require("./routes/AdminRoutes");
var CommentRoute = require("./routes/Comment");
var CategoryRoute = require("./routes/category");
Expand All @@ -29,7 +31,7 @@ app.use("/api/category", CategoryRoute);
app.use("/category", findQues);

app.use("/api/code", CodeRoutes);
app.use("/api/leaderboard", Leaderboard);
app.use("/api/leaderboard", LeaderboardRoutes);
app.use("/admin", AdminRoutes);

app.use(function (err, req, res, next) {
Expand Down
88 changes: 0 additions & 88 deletions backend/routes/Comment.js

This file was deleted.

85 changes: 85 additions & 0 deletions backend/routes/Comment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var express = require("express");
var router = express.Router();
var db = require("../../models");


router.get("/", function (req, res, next) {
db.Comments.find()
.then((comments) => res.json(comments))
.catch((err) => next(err));
});

//POST ROUTE
router.post("/:questionId/comments", function (req, res, next) {
db.Question.findById(req.params.questionId, function (err, questions) {
if (err) {
next(err);
} else {
db.Comments.create(req.body, function (err1, newComment) {
if (err1) {
next(err1);
} else {
res.json(newComment);
questions.comments.push(newComment._id);
questions.save();
}
});
}
});
});

//FIND ANY SPECIFIC COMMENT
router.get("/:commentId", function (req, res, next) {
db.Comments.findById(req.params.commentId)
.then((foundComments) => res.json(foundComments))
.catch((err) => next(err));
});

//UPDATE ROUTE

router.put("/:questionId/comments/:commentId", function (req, res, next) {
db.Question.findById(req.params.questionId, function (err, questions) {
if (err) {
next(err);
} else {
db.Comments.findOneAndUpdate({ _id: req.params.commentId }, req.body, {
new: true,
})
.then(function (updatedComment) {
res.json(updatedComment);
})
.catch(function () {
next(err);
});
}
});
});

//DELETE ROUTE
router.delete("/comments/:commentId", function (req, res, next) {
db.Comments.remove({ _id: req.params.commentId })
.then(function () {
res.json({ message: "we deleted it!" });
})
.catch(function (err) {
next(err);
});
});

// router.delete("/:questionId/comments/:commentId", function (req, res) {
// db.Question.findById(req.params.questionId, function (err, questions) {
// if (err) {
// console.log(err);
// } else {
// db.Comments.findByIdAndRemove({ _id: req.params.commentId })
// .then(function () {
// res.json({ message: "we deleted it!" });
// })
// .catch(function (err) {
// res.send(err);
// });
// }
// });
// });

module.exports = router;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Router } = require("express");
var db = require("../models");
var db = require("../../models");
const router = Router();

router.get("/", function (req, res, next){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var express = require("express");
var router = express.Router();
var db = require("../models");
var db = require("../../models");

//GET ROUTE(TO GET ALL QUESTION api's)
router.get("/", function (req, res, next) {
Expand Down

0 comments on commit 9676179

Please sign in to comment.