{"size": 1049, "ext": "js", "max_stars_repo_path": "lectures_node/lecture2_express/app/routes/router.js", "max_stars_repo_name": "MastersAcademy/js-course-2017", "max_stars_repo_stars_event_min_datetime": "2017-10-14T17:36:19.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-17T23:25:22.000Z", "max_issues_repo_path": "lectures_node/lecture2_express/app/routes/router.js", "max_issues_repo_name": "MastersAcademy/js-course-2017", "max_issues_repo_issues_event_min_datetime": "2017-10-13T09:33:55.000Z", "max_issues_repo_issues_event_max_datetime": "2018-02-14T00:19:43.000Z", "max_forks_repo_path": "lectures_node/lecture2_express/app/routes/router.js", "max_forks_repo_name": "MastersAcademy/js-course-2017", "max_forks_repo_forks_event_min_datetime": "2017-10-13T13:26:00.000Z", "max_forks_repo_forks_event_max_datetime": "2019-08-17T19:14:10.000Z"}
{"max_stars_count": 36, "max_issues_count": 78, "max_forks_count": 234, "avg_line_length": 18.4035087719, "max_line_length": 46, "alphanum_fraction": 0.590085796}
let express = require('express');
let router = express.Router();
let data = require('../models/static-data');

let fakeId = 12345677;

router.get('/', function (req, res) {

    res.json(data);

});

router.post('/', function (req, res) {

    let item = {
        firstName : req.body.firstName,
        lastName : req.body.lastName,
        bornCountry : req.body.bornCountry,
        id : ++fakeId
    };

    data.push(item);

    res.send(item);

});

router.put('/:id', function (req, res) {

    let item = data.find(function (el) {
        return el.id == req.params.id;
    });

    let index = data.indexOf(item);

    item.firstName = req.body.firstName;
    item.lastName = req.body.lastName;
    item.bornCountry = req.body.bornCountry;
    data[index] = item;
    res.send(item);

});

router.delete('/:id', function (req, res) {

    let item = data.find(function (laureate) {
        return laureate.id == req.params.id;
    });

    data.splice(data.indexOf(item), 1);

    res.send({status : 'ok'});

});

module.exports = router;
