{"size": 1520, "ext": "js", "max_stars_repo_path": "routes/api/items.js", "max_stars_repo_name": "Pipirok/MaterialNoteKeep", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/api/items.js", "max_issues_repo_name": "Pipirok/MaterialNoteKeep", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/api/items.js", "max_forks_repo_name": "Pipirok/MaterialNoteKeep", "max_forks_repo_forks_event_min_datetime": "2022-03-07T12:28:47.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-07T12:28:47.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 25.3333333333, "max_line_length": 103, "alphanum_fraction": 0.5171052632}
const express = require('express');
const router = express.Router();
const Item = require('../../models/Item');
const auth = require('../../middlewares/auth');


// fetch items from database
router.get('/', async (req, res) => {
    try {
        
        /* const items = await */ Item.find().sort({ date: -1 }).exec((err, items) => res.json(items));

       // res.status(200).json(items);
    } catch (e) {
        res.status(400).json({ msg: e.message });
    }
});

// add a new item
router.post('/', auth, async (req, res) => {
    const newItem = new Item({
        name: req.body.name
    });
    try {
        const item = await newItem.save();
        if (!item) {
            throw Error('Something went wrong whilst saving new item');
        }

        res.status(200).json(item);
    } catch (e) {
        res.status(400).json({ msg: e.message });
    }
});

// delete an item by its id

//////////////////////////////////////////
// Add auth before working on front end //
//////////////////////////////////////////

router.delete('/:id', auth, async (req, res) => {
    try {
        //console.log(req.params.id)
        const item = await Item.findById(req.params.id);
        if (!item) throw Error('No item found!');
        
        const removed = await item.remove();
        if (!removed) throw Error('Error deleting item');

        res.status(200).json({ success: true });
    } catch (e) {
        res.status(400).json({ msg: e.message, success: false });
    }
});




module.exports = router;