<file_sep>const _ = require('lodash');
const uuid = require('uuid')

const fs = require('fs');
const path = require('path');

const filename = path.join(__dirname, '../data/todos.json');

exports.getAll = function (req,cb) {
  if(req.query.complete){
    if (req.query.complete === 'true'){
      fs.readFile(filename, (err,buffer) => {
        if(err) return cb(err);
        try {
          var data = JSON.parse(buffer);
          data = data.filter(cur => {
            if(cur.isComplete){
              return true;
            }
          })
          cb(null,data)
        } catch(e){
          var data = [];
        }
      })
    } else {
      console.log('false')
      fs.readFile(filename, (err,buffer) => {
        if(err) return cb(err);
        try {
          var data = JSON.parse(buffer);
          data = data.filter(cur => {
            if(cur.isComplete === false){
              return true;
            }
          })
          cb(null,data)
        } catch(e){
          var data = [];
        }
      })
    }
  }
  else{
    fs.readFile(filename, (err, buffer) => {
      if(err) return cb(err);
      try {
        var data = JSON.parse(buffer);
        console.log(data)
        cb(null, data);
      } catch (e) {
        var data = [];
      }
    });
  }



}

exports.write = function(requested,newData, cb) {
  let json = JSON.stringify(newData);
  fs.writeFile(filename, json, (err) => {
    if(err)return cb(err);
  });
  return cb(null, requested);
}

exports.edit = function(req, cb){
  let idToEdit = req.params.id;
  let didIt = {};
  fs.readFile(filename, (err, buffer) => {
    try {
      var data = JSON.parse(buffer);
      data = data.map((cur) => {
        if(cur.id === idToEdit && cur.isComplete){
          cur.isComplete = false;
          didIt = {
            isComplete:false,
            name: cur.name,
            duedate: cur.duedate
          }
          return cur;
        }
        else if(cur.id === idToEdit && cur.isComplete == false){
          cur.isComplete = true;
          didIt = {
            isComplete:true,
            name: cur.name,
            duedate: cur.duedate
          }
          return cur;
        }
        else {}
        return cur;
      })
      exports.write(didIt,data, cb);
    } catch (e) {
      var data = [];
    }
  })
}

exports.create = function(newItem, cb){
  console.log(newItem)
  newItem.isComplete = false;
  newItem.id = uuid();
  let requested = {name: newItem.name, duedate: newItem.duedate}
  fs.readFile(filename, (err, buffer) => {
    if(err) return cb(err);
    try {
      var data = JSON.parse(buffer);
      console.log(data)
      data.push(newItem)
      console.log(data)
      exports.write(requested,data, cb)
    } catch (e) {
      var data = [];
    }
  })
}

exports.delete = function(req, cb){
  let idToDelete = req.params.id
  if(idToDelete === "complete"){
    fs.readFile(filename, (err, buffer) => {
      try {
        var data = JSON.parse(buffer);
        data = data.filter((cur) => {
          if(cur.isComplete){
            return false;
          }else {
            return true;
          }

        })
        exports.write(data,data, cb);
      } catch (err) {
        var data = [];
        return cb(err);
      }
    })
  }
  else {
    fs.readFile(filename, (err, buffer) => {
      try {
        var data = JSON.parse(buffer);
        data = data.filter((cur) => {
          if(cur.id !== idToDelete){
            return true;
          }
        })
        exports.write(data,data, cb);
      } catch (err) {
        var data = [];
        return cb(err);
      }
    })
  }
}
<file_sep>const PORT = 8000;

const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');

const flashcards = require('./models/todos.js');

const app = express();

// App Middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use((req, res, next) => {
  res.handle = (err, data) => res.status(err ? 400 : 200).send(err || data);
  next();
})


// ROUTEs

app.get('/todos', (req, res) => flashcards.getAll(req,res.handle));

app.post('/todos', (req,res) => flashcards.create(req.body,res.handle));

app.put('/todos/:id', (req,res) => flashcards.edit(req, res.handle));

app.delete('/todos/:id', (req,res) => flashcards.delete(req, res.handle));



app.listen(PORT, (err) => {
  console.log(err || `Express listening on port ${PORT}`);
});
