Skip to content

Commit

Permalink
replaced deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipeDominguesGit committed Aug 23, 2017
1 parent 15b1ecb commit b482ae4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/routes/api/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ const express = require('express');
const router = express.Router();

const inMemoryTodoDB = [
{id:0,name:'todo 0',description:'description here', done:false},
{id:1,name:'todo 1',description:'description here', done:true}
{id:0,name:'Part I',description:'Write Part I', done:true},
{id:1,name:'Part II',description:'Write Part II', done:false}
];

router.get('/',(req,res)=>{
res.status(200).json(inMemoryTodoDB);
res.status(200)
.json(inMemoryTodoDB);
});

router.get('/:id',(req,res)=>{

const { id } = req.params;

const todoItem = inMemoryTodoDB.filter((todo)=> todo.id==id)[0];

if(!todoItem){
res.status(404).json({error:'Todo task not found.'});
res.sendStatus(404);
}
else{
res.status(200).json(todoItem);
Expand All @@ -31,36 +32,36 @@ router.post('/',(req,res)=>{
// getting last used id from our Mock DB
const lastId = inMemoryTodoDB[inMemoryTodoDB.length-1].id;
const id = lastId + 1;

const newTodo = { id,name,description,done };

inMemoryTodoDB.push(newTodo);

res.status(201)
.location(`/api/todos/${id}`)
.json(newTodo);

});

router.put('/:id/done',(req,res)=>{

let { done } = req.body;
const {id} = req.params;

// check if its a boolean
if(typeof(done) != 'boolean' )
{
res.sendStatus(400);
return;
}

const exists = inMemoryTodoDB.filter((todo)=> todo.id==id).length > 0;

if(!exists){
res.sendStatus(404);
return;
}

inMemoryTodoDB.map((todo)=>{
if(todo.id == id) {
todo.done = done;
Expand All @@ -78,12 +79,12 @@ router.delete('/:id',(req,res)=>{

if(!todoItem)
{
res.send(404);
res.sendStatus(404);
return;
}
inMemoryTodoDB.splice(inMemoryTodoDB.indexOf((todo)=>todo.id==id),1);

res.send(200);
res.sendStatus(200);

});

Expand Down

0 comments on commit b482ae4

Please sign in to comment.