{"size": 915, "ext": "js", "max_stars_repo_path": "Develop/routes/apiRoutes.js", "max_stars_repo_name": "msr102087/note_taker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Develop/routes/apiRoutes.js", "max_issues_repo_name": "msr102087/note_taker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Develop/routes/apiRoutes.js", "max_forks_repo_name": "msr102087/note_taker", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 16.9444444444, "max_line_length": 73, "alphanum_fraction": 0.581420765}
const { v4: uuidv4 } = require('uuid')

const fs =  require('fs');

module.exports = (app) => {

let notes = require(__dirname+'/../db/db.json')

//Display Notes
app.get('/api/notes', function(req,res) {

    res.json(notes)
})

// // Create a new note

app.post('/api/notes', (req,res) => {

    let addNote = req.body 
    addNote.id =  uuidv4()
    notes.push(addNote)
    const data = JSON.stringify(notes)

    fs.writeFile(__dirname+'/../db/db.json', data, function(err) {
        if (err) throw err
    })


    res.end()
}
)

// Delete Notes

app.delete('/api/notes/:id', (req, res) => {

    const id = req.params.id
    const filterNotes = notes.filter(note => {

        return note.id != id

    })

    const newNote = JSON.stringify(filterNotes);
    notes = filterNotes
    fs.writeFileSync(__dirname+'/../db/db.json', newNote, function(err) {
        if (err) throw err
    });

    res.end()
})
}

