2020-05-12 13:16:03 +00:00
|
|
|
const express = require("express");
|
|
|
|
const serverResponses = require("../utils/helpers/responses");
|
|
|
|
const messages = require("../config/messages");
|
|
|
|
const { Todo } = require("../models/todos/todo");
|
2020-05-11 19:40:39 +00:00
|
|
|
|
|
|
|
const routes = (app) => {
|
2020-05-12 13:16:03 +00:00
|
|
|
const router = express.Router();
|
2020-05-11 19:40:39 +00:00
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
router.post("/todos", (req, res) => {
|
|
|
|
const todo = new Todo({
|
|
|
|
text: req.body.text,
|
2020-05-11 19:40:39 +00:00
|
|
|
});
|
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
todo
|
|
|
|
.save()
|
|
|
|
.then((result) => {
|
|
|
|
serverResponses.sendSuccess(res, messages.SUCCESSFUL, result);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
serverResponses.sendError(res, messages.BAD_REQUEST, e);
|
|
|
|
});
|
|
|
|
});
|
2020-05-11 19:40:39 +00:00
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
router.get("/", (req, res) => {
|
|
|
|
Todo.find({}, { __v: 0 })
|
|
|
|
.then((todos) => {
|
|
|
|
serverResponses.sendSuccess(res, messages.SUCCESSFUL, todos);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
serverResponses.sendError(res, messages.BAD_REQUEST, e);
|
|
|
|
});
|
|
|
|
});
|
2020-05-11 19:40:39 +00:00
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
//it's a prefix before api it is useful when you have many modules and you want to
|
|
|
|
//differentiate b/w each module you can use this technique
|
|
|
|
app.use("/api", router);
|
2020-05-11 19:40:39 +00:00
|
|
|
};
|
2020-05-12 13:16:03 +00:00
|
|
|
module.exports = routes;
|