Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var News = require('mongoose').model('News'),
- fs = require('fs'),
- Bulletin = require('mongoose').model('Bulletin'),
- Lang = require('mongoose').model('Languages'),
- nodemailer = require('nodemailer');
- module.exports = {
- createNews: function (req, res, next) {
- News.create(req.body, function (err, news) {
- if (err) {
- console.log('Failed to register new news: ' + err);
- res.status(500).end();
- return;
- }
- Lang.find({}).exec(function (err, collection) {
- if (err) {
- res.status(500).end();
- return;
- }
- collection.forEach(function (lang) {
- Lang.update({lang: lang.lang}, {$push: {'words': {phraseBG: news.title, phrase: ""}}}, function (err) {
- if (err) {
- res.status(500).end();
- return;
- }
- });
- Lang.update({lang: lang.lang}, {$push: {'words': {phraseBG: news.description, phrase: "", html: 1}}}, function (err) {
- if (err) {
- res.status(500).end();
- return;
- }
- });
- })
- })
- Bulletin.find({}).exec(function (err, collection) {
- if (err) {
- console.log('Subscriptions could not be loaded: ' + err);
- res.status(500).end();
- return;
- }
- var smtpConfig = {
- host: 'smtp.gmail.com',
- port: 465,
- secure: true,
- auth: {
- user: 'tuka emaila pishesh',
- pass: 'tuka parolata pishesh'
- }
- };
- var transporter = nodemailer.createTransport(smtpConfig);
- var description = '<div style="background-color:#eeeeee;margin:0 auto;background: url(http://blagoevgrad.herokuapp.com/img/bckg.jpg) no-repeat center center fixed;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;padding-top: 50px;padding-bottom: 50px;background-size: cover;">' +
- '<div style="background-color: #eee;width: 70%;margin: 0 auto;padding: 15px;">' +
- '<img alt="" src="https://blagoevgrad.herokuapp.com/img/logo.png" style="height:100px;width:100px;float:left;margin-right:10px;margin-bottom:10px;"><h2>' + req.body.title + '</h2><p>' + req.body.description + '</p><p><a href="https://blagoevgrad.herokuapp.com/#/news" target="_blank">https://blagoevgrad.herokuapp.com/#/news</a></p>' +
- '</div>' +
- '<div style="height: 25px;background: #323232;width: 70%;padding: 0 15px;text-align: center;margin: 0 auto;color: #fff;line-height: 2;">© Някои права запазени | Благоевград</div>' +
- '</div>';
- var plainDescription = description.replace(/<[^>]*>/g, '');
- collection.forEach( function (subscription) {
- var mailOptions = {
- from: '"Благоевград - Новини" <emil12543@gmail.com>',
- to: subscription.email,
- subject: req.body.title,
- text: plainDescription,
- html: description
- };
- transporter.sendMail(mailOptions, function(error, info){
- if(error){
- return console.log(error);
- }
- console.log('Message sent: ' + info.response);
- });
- });
- })
- res.send(news);
- });
- },
- getAllNews: function (req, res, next) {
- News.find({}).exec(function (err, collection) {
- if (err) {
- console.log('News could not be loaded: ' + err);
- res.status(500).end();
- return;
- }
- res.send(collection);
- })
- },
- updateNews: function (req, res, next) {
- var updatedNewsData = req.body;
- News.update({_id: req.body._id}, updatedNewsData, function () {
- Lang.find({}).exec(function (err, collection) {
- if (err)
- console.log(err);
- collection.forEach(function (lang) {
- Lang.update({lang: lang.lang}, {$push: {'words': {phraseBG: updatedNewsData.title, phrase: ""}}}, function (err) {
- if (err)
- console.log(err);
- });
- Lang.update({lang: lang.lang}, {$push: {'words': {phraseBG: updatedNewsData.description, phrase: "", html: 1}}}, function (err) {
- if (err)
- console.log(err);
- });
- })
- })
- res.end();
- });
- },
- deleteNews: function (req, res, next) {
- News.findOneAndRemove({_id: req.params.id}, function () {
- fs.unlinkSync('./public/img/uploads/' + req.params.id + '.jpg');
- res.end();
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement