{"size": 2682, "ext": "js", "max_stars_repo_path": "routes/apiRoutes.js", "max_stars_repo_name": "Epanastatis2020/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": "Epanastatis2020/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": "Epanastatis2020/Note-Taker", "max_forks_repo_forks_event_min_datetime": "2020-11-01T09:31:52.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-11T08:19:35.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 30.8275862069, "max_line_length": 91, "alphanum_fraction": 0.4217002237}
//------------------------------------------------
// Dependencies
//------------------------------------------------

const fs = require("fs");

//------------------------------------------------
// Functions
//------------------------------------------------

    // Generate a unique ID

    const generateID = () => {
        // create array of IDs already in use
        const existingIDArray = [];
        notesArray.forEach((note) => existingIDArray.push(note.id));

        // generate new ID
        let newID = () => {
            let IDvar = Math.floor(Math.random() * 10000 + 1).toString();
            // check to see if the ID already exists
            if (existingIDArray.includes(IDvar)) {
                newID()
            };
            return IDvar;
        };
        
        return newID();
    };

    // Write to the database

    const writetoDB = () => {
        fs.writeFileSync(__dirname + "/../db/db.json", JSON.stringify(notesArray), "UTF8");
    };

//------------------------------------------------
// Setting routes
//------------------------------------------------

// global variable
let notesArray = [];

module.exports = function (app) {

    //------------------------------------------------
    // Getting all stored notes
    //------------------------------------------------
    app.get("/api/notes", function (req, res) {
        // setting db contents to a variable
        let storedData = fs.readFileSync(__dirname + "/../db/db.json");
        // storing db content variable as JSON array
        notesArray = JSON.parse(storedData);
        // sending the array
        res.send(notesArray);
    });

    //------------------------------------------------
    // Saving new note
    //------------------------------------------------
    app.post("/api/notes", function (req, res) {
        // set new note to received client request
        let newNote = req.body;
        // give new note randomly generated ID
        newNote.id = generateID();
        // push new note to notes array
        notesArray.push(newNote);
        // write notes array back to db
        writetoDB();
        // return new note to client
        res.send(newNote)
    })

    //------------------------------------------------
    // Delete a note
    //------------------------------------------------
    app.delete("/api/notes/:id", function (req, res) {
        // filters the array of notes to exclude the requested id
        notesArray = notesArray.filter((item) => {
            return item.id !== req.params.id;
        });
        // write the updated array back to the db
        writetoDB();
        res.json({ ok: true });
    });

};