{"size": 1530, "ext": "js", "max_stars_repo_path": "node-todo-api/server/server.js", "max_stars_repo_name": "Shubham-koli/nodejs-stuff", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node-todo-api/server/server.js", "max_issues_repo_name": "Shubham-koli/nodejs-stuff", "max_issues_repo_issues_event_min_datetime": "2021-05-07T13:29:26.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-07T13:29:39.000Z", "max_forks_repo_path": "node-todo-api/server/server.js", "max_forks_repo_name": "Shubham-koli/nodejs-stuff", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.4, "max_line_length": 64, "alphanum_fraction": 0.468627451}
// library imports
const express = require("express");
const bodyParser = require("body-parser");
const _ = require('lodash');
const {
    ObjectID
} = require('mongodb');

// local imports
const {
    mongoose
} = require("./db/mongoose");
const {
    Todo
} = require("./models/todo");
const {
    User
} = require("./models/users");

var app = express();
app.use(bodyParser.json());

app.post("/todos", (req, res) => {
    let todo = new Todo({
        text: req.body.text
    });

    todo.save().then(
        doc => {
            res.send(doc);
        },
        err => {
            res.status(400).send(err);
        }
    );
});

app.get("/todos", (req, res) => {
    Todo.find().then(
        doc => {
            let count = _.size(doc);
            res.send({
                doc,
                count
            });
        },
        err => {
            res.status(500).send(err);
        }
    );
});

//variables can be separated by the & or /

app.get('/todos/:id', (req, res) => {
    let recordID = req.params.id;
    if (!ObjectID.isValid(recordID)) {
        res.status(502).send('INVALID REQUEST! CHECK ID AGAIN');
    } else {
        Todo.findById(recordID).then((todo) => {
            console.log(todo);
            if (!todo) {
                res.status(404).send('ID NOT FOUND');
            } else {
                res.send(todo);
            }
        }).catch((err) => {
            console.log(err);
        });
    }
});

app.listen(3000, () => {
    console.log("Started on port 3000");
});