var mongoose = require('mongoose');
var Notice = require('../data/notice');
var _ = require('underscore');

var router = require('express').Router();
router.route('/:id').post(editNotice).delete(deleteNotice);
router.route('/').get(getNotices).post(addNotice);

function getNotices(req, res) {
    Notice.find(function (err, notices) {
        if (err) res.send(err);
        else res.json(notices);
    });
}

function addNotice(req, res) {
    var notice = new Notice(_.extend({}, req.body));
    notice.save(function (err) {
        if (err) res.send(err);
        else res.json(notice);
    });
}

function editNotice(req, res) {
    var id = req.params.id;
    var info = req.body;
    var query = { _id: id },
        update = { $set: {
            name: info.name,
            location: info.location,
            description: info.description,
            link: info.link,
            date: info.date,
            tags: info.tags,
            edited: info.edited,
            editDate: info.editDate
        }};
    Notice.update(query, update, function (err, updated) {
        if (err) res.send(err);
        else res.json(updated);
    });
}

function deleteNotice(req, res) {
    var id = req.params.id;
    Notice.remove({ _id: id }, function (err, removed) {
        if (err) res.send(err);
        else res.json(removed);
    });
}

module.exports = router;

@@ -259,432 +259,1574 @@
 );%0A%0A
-function getNotices(req, res) %7B%0A    Notice.find(function (err, notices) %7B%0A        if (err) res.send(err);%0A        else res.json(notices);%0A    %7D);%0A%7D%0A%0Afunction addNotice(req, res) %7B%0A    var notice = new Notice(_.extend(%7B%7D, req.body));%0A    notice.save(function (err) %7B%0A        if (err) res.send(err);%0A        else res.json(notice);%0A    %7D);%0A%7D%0A%0Afunction editNotice(req, res) %7B%0A    var id = req.params.id;%0A    var info = req.body;
+// nodemailer%0Avar nodemailer = require('nodemailer');%0Avar smtpConfig = %7B%0A%09host: 'smtp.gmail.com',%0A%09port: 465,%0A%09secure: true,%0A%09auth: %7B%0A%09%09user: 'amr.notify@gmail.com',%0A%09%09pass: process.env.EMAIL_PASSWORD%0A%09%7D%0A%7D;%0Avar transporter = nodemailer.createTransport(smtpConfig);%0Atransporter.verify(function(err, success) %7B%0A%09if (err) %7B%0A%09%09console.log(err);%0A%09%7D else %7B%0A%09%09console.log('Email server ready...');%0A%09%7D%0A%7D)%0A%0Avar mailData = %7B%0A%09from: 'amr.notify@gmail.com',%0A%09to: 'chris.witulski@gmail.com'%0A%7D%0A%0A// App functions%0Afunction getNotices(req, res) %7B%0A    Notice.find(function (err, notices) %7B%0A        if (err) res.send(err);%0A        else res.json(notices);%0A    %7D);%0A%7D%0A%0Afunction addNotice(req, res) %7B%0A    var notice = new Notice(_.extend(%7B%7D, req.body));%0A%0A%09mailData.subject = '%5BAMR%5D Notice created';%0A%09mailData.text = 'New notice called ' + notice.name + '%5Cr%5Cn' +%0A%09%09'Description: ' + notice.description + '%5Cr%5Cn' +%0A%09%09'Owner ID: ' + notice.owner + '%5Cr%5Cn' +%0A%09%09'Other information may be available on the site.'%0A%09transporter.sendMail(mailData);%0A%0A    notice.save(function (err) %7B%0A        if (err) res.send(err);%0A        else res.json(notice);%0A    %7D);%0A%7D%0A%0Afunction editNotice(req, res) %7B%0A    var id = req.params.id;%0A    var info = req.body;%0A%0A%09mailData.subject = '%5BAMR%5D Notice edited';%0A%09mailData.text = 'Notice edited.' + '%5Cr%5Cn' +%0A%09%09'Name: ' + info.name + '%5Cr%5Cn' +%0A%09%09'Description: ' + info.description + '%5Cr%5Cn' +%0A%09%09'Owner ID: ' + info.owner + '%5Cr%5Cn' +%0A%09%09'Other information may be available on the site.' + '%5Cr%5Cn' +%0A%09%09'http://www.arabmusicresearch.org/notices/' + id;%0A%09transporter.sendMail(mailData);%0A
 %0A   
@@ -2347,24 +2347,155 @@
 .params.id;%0A
+%0A%09mailData.subject = '%5BAMR%5D Notice deleted';%0A%09mailData.text = 'A notice was deleted. ID: ' + id;%0A%09transporter.sendMail(mailData);%0A%0A
     Notice.r

add email to notices