This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added controllers folder in order to have a much cleaner api file
- Loading branch information
1 parent
509ac57
commit d6c88cc
Showing
2 changed files
with
57 additions
and
49 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 |
---|---|---|
@@ -1,55 +1,10 @@ | ||
const express = require('express'); | ||
const Todo = require('../models/Todo'); | ||
|
||
const todoController = require('../controllers/todos'); | ||
const router = express.Router(); | ||
|
||
// Error handling | ||
const sendError = (err, res) => { | ||
response.status = 501; | ||
response.message = typeof err == 'object' ? err.message : err; | ||
res.status(501).json(response); | ||
}; | ||
|
||
// Response handling | ||
let response = { | ||
status: 200, | ||
data: [], | ||
message: null | ||
}; | ||
|
||
router.post('/todo', function (req, res) { | ||
let todo = new Todo({ | ||
'title': req.body.title, | ||
'text': req.body.text | ||
}); | ||
|
||
todo.save((err) => { | ||
if (err) | ||
sendError(err, res); | ||
response.data = 'Todo created'; | ||
res.json(response); | ||
}); | ||
|
||
}); | ||
|
||
router.get('/todos', function (req, res) { | ||
Todo.find({}, function (err, todos) { | ||
if (err) | ||
sendError(err, res); | ||
response.data = todos; | ||
res.json(response); | ||
}); | ||
}); | ||
|
||
router.delete('/todo/:todoid', function (req, res) { | ||
let todoId = req.params.todoid; | ||
|
||
Todo.findByIdAndRemove(todoId, function (err, todo) { | ||
if(err) | ||
sendError(err, res); | ||
response.data = 'Todo deleted' | ||
res.json(response); | ||
}); | ||
}); | ||
router.post('/todo', todoController.createTodo); | ||
router.get('/todos', todoController.getTodos); | ||
router.delete('/todo/:todoid', todoController.deleteTodo); | ||
|
||
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,53 @@ | ||
const passport = require('passport'); | ||
const jwt = require('express-jwt'); | ||
const { validationResult } = require('express-validator/check'); | ||
|
||
const Todo = require('../models/Todo'); | ||
|
||
// Error handling | ||
const sendError = (err, res) => { | ||
response.status = 501; | ||
response.message = typeof err == 'object' ? err.message : err; | ||
res.status(501).json(response); | ||
}; | ||
|
||
// Response handling | ||
let response = { | ||
status: 200, | ||
data: [], | ||
message: null | ||
}; | ||
|
||
exports.createTodo = function (req, res) { | ||
let todo = new Todo({ | ||
'title': req.body.title, | ||
'text': req.body.text | ||
}); | ||
|
||
todo.save((err) => { | ||
if (err) | ||
sendError(err, res); | ||
response.data = 'Todo created'; | ||
res.json(response); | ||
}); | ||
}; | ||
|
||
exports.getTodos = function (req, res) { | ||
Todo.find({}, function (err, todos) { | ||
if (err) | ||
sendError(err, res); | ||
response.data = todos; | ||
res.json(response); | ||
}); | ||
}; | ||
|
||
exports.deleteTodo = function (req, res) { | ||
let todoId = req.params.todoid; | ||
|
||
Todo.findByIdAndRemove(todoId, function (err, todo) { | ||
if(err) | ||
sendError(err, res); | ||
response.data = 'Todo deleted' | ||
res.json(response); | ||
}); | ||
}; |