{"size": 1340, "ext": "js", "max_stars_repo_path": "routes/apiRoutes.js", "max_stars_repo_name": "NLe97/Note-Taker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/apiRoutes.js", "max_issues_repo_name": "NLe97/Note-Taker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/apiRoutes.js", "max_forks_repo_name": "NLe97/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": 31.9047619048, "max_line_length": 71, "alphanum_fraction": 0.421641791}
const note = require("../db/db");
const fs = require("fs");


module.exports = function (app) {
    app.get("/api/notes", function(req,res) {
            console.log("Notes Received");
            return res.json(note);
    });

    app.post("/api/notes", function(req,res) {
            let newNote = req.body;
            if (note === "") {
                newNote.id = 0;

            } else {
                newNote.id = note.length;    
            } 
            note.push(newNote);
            var noteString = JSON.stringify(note);
            fs.writeFile("db/db.json", noteString, function (err) {
                if (err) throw err;
                console.log("Note has been sent!");
                res.send();
            });          
    });

    app.delete("/api/notes/:id", function(req,res) {
            let deleteNote = (req.params.id);
            note.splice(deleteNote, 1);
            if (!note === undefined || !note.length == 0) {
                    for (let i = 0; i < note.length; i++) {
                            note[i].id = i;
                    };
            };
            fs.writeFile("db/db.json", JSON.stringify(note), (err) => {
                    if (err) throw err;
                    console.log("Note #" + deleteNote + " deleted");
                    res.send();
            });
    });
};