Browse Source

implemented add image to event, and sent it to the goImgServer

master
arnaucode 7 years ago
parent
commit
21757a4712
4 changed files with 252 additions and 185 deletions
  1. +1
    -1
      config.js
  2. +244
    -181
      controllers/eventController.js
  3. +2
    -1
      package.json
  4. +5
    -2
      server.js

+ 1
- 1
config.js

@ -3,7 +3,7 @@ module.exports = {
'database': process.env.MONGO_DSN,*/
'secret': 'secretfortoken',// local version
'database': 'mongodb://localhost/openeventsplatform',
"port" : process.env.PORT || 3000,
"port" : process.env.PORT || 3001,
"pageSize": 20
};

+ 244
- 181
controllers/eventController.js

@ -7,161 +7,217 @@ var mongoose = require('mongoose');
var userModel = mongoose.model('userModel');
var eventModel = mongoose.model('eventModel');
var pageSize=config.pageSize;
var pageSize = config.pageSize;
var request = require('request');
exports.getCategoriesList = function(req, res) {
var categoriesList=[
{
name: "esport"
},
{
name: "xerrada"
},
{
name: "taller"
},
{
name: "festa"
},
{
name: "concert"
},
{
name: "musica"
}
]
var categoriesList = [{
name: "esport"
},
{
name: "xerrada"
},
{
name: "taller"
},
{
name: "festa"
},
{
name: "concert"
},
{
name: "musica"
}
]
res.status(200).jsonp(categoriesList);
};
exports.getAllEvents = function(req, res) {
eventModel.find({
date: {$gte: new Date()},
type: {$nin: ["alert"]}//cal filtrar per type d'event, aquí només agafem els type: alert
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
eventModel.find({
date: {
$gte: new Date()
},
type: {
$nin: ["alert"]
} //cal filtrar per type d'event, aquí només agafem els type: alert
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function(err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
};
exports.getAllAlerts = function(req, res) {
eventModel.find({
date: {$gte: new Date()},
type: "alert"
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
eventModel.find({
date: {
$gte: new Date()
},
type: "alert"
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function(err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
};
exports.getEventById = function (req, res) {
eventModel.findOne({_id: req.params.eventid})
.lean()
.populate('user', 'username img shortDescription')
.exec(function (err, event) {
if (err) return res.send(500, err.message);
if (!event) {
res.json({success: false, message: 'event not found.'});
} else if (event) {
res.status(200).jsonp(event);
}
});
exports.getEventById = function(req, res) {
eventModel.findOne({
_id: req.params.eventid
})
.lean()
.populate('user', 'username img shortDescription')
.exec(function(err, event) {
if (err) return res.send(500, err.message);
if (!event) {
res.json({
success: false,
message: 'event not found.'
});
} else if (event) {
res.status(200).jsonp(event);
}
});
};
exports.addEvent = function(req, res) {
userModel.findOne({'tokens.token': req.headers['x-access-token']})
.exec(function(err, user){
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
function postImage(req, res, user, filename, fileImg) {
url = "http://127.0.0.1:3050/image";
var importFile = function(fileImg) {
var decodedFile = new Buffer(fileImg, 'base64');
var r = request.post(url, function(err, httpResponse, body) {
if (err) {
console.log(err);
}
//console.log(body);
addNewEvent(req, res, user, body);
});
var form = r.form();
form.append('file', decodedFile, {
filename: filename + '.png'
});
}
importFile(fileImg);
}
function addNewEvent(req, res, user, imgUrl){
//adding random number to the url, to force ionic reload the image
req.body.img = imgUrl+ "?" + getRandomInt(1, 9999);
var event = new eventModel({
title: req.body.title,
description: req.body.description,
img: req.body.img,
date: req.body.date,
categories: req.body.categories,
generateddate: Date(),
location: req.body.location,
user: user._id
});
event.save(function(err, event) {
if (err) return res.send(500, err.message);
if (!user) {
console.log("user not found");
res.json({success: false, message: 'User not found.'});
} else if (user) {
var event = new eventModel({
title: req.body.title,
description: req.body.description,
img: req.body.img,
date: req.body.date,
categories: req.body.categories,
generateddate: Date(),
location: req.body.location,
user: user._id
});
event.save(function(err, event) {
if(err) return res.send(500, err.message);
user.events.push(event._id);
user.save(function (err, user) {
if (err) return res.send(500, err.message);
exports.getAllEvents(req, res);
user.events.push(event._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
req.params.eventid=event._id;
exports.getEventById(req, res);
});
}); //end of event.save
}
exports.addEvent = function(req, res) {
userModel.findOne({
'tokens.token': req.headers['x-access-token']
})
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
console.log("user not found");
res.json({
success: false,
message: 'User not found.'
});
});//end of event.save
}
});//end of usermodel.find
} else if (user) {
if (req.body.img) {
imgname = getRandomInt(1, 9999) + "_" + getRandomInt(1, 9999);
postImage(req, res, user, "event_" + imgname, req.body.img);
}else{
addNewEvent(req, res, user, "");
}
}
}); //end of usermodel.find
};
exports.addAlert = function(req, res) {
userModel.findOne({'tokens.token': req.headers['x-access-token']})
.exec(function(err, user){
if (err) return res.send(500, err.message);
if (!user) {
console.log("user not found");
res.json({success: false, message: 'User not found.'});
} else if (user) {
var event = new eventModel({
title: req.body.title,
description: req.body.description,
date: req.body.date,
generateddate: Date(),
user: user._id,
type: "alert"
});
event.save(function(err, event) {
if(err) return res.send(500, err.message);
user.events.push(event._id);
user.save(function (err, user) {
if (err) return res.send(500, err.message);
exports.getAllEvents(req, res);
userModel.findOne({
'tokens.token': req.headers['x-access-token']
})
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
console.log("user not found");
res.json({
success: false,
message: 'User not found.'
});
});//end of event.save
}
});//end of usermodel.find
} else if (user) {
var event = new eventModel({
title: req.body.title,
description: req.body.description,
date: req.body.date,
generateddate: Date(),
user: user._id,
type: "alert"
});
event.save(function(err, event) {
if (err) return res.send(500, err.message);
user.events.push(event._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getAllEvents(req, res);
});
}); //end of event.save
}
}); //end of usermodel.find
};
exports.deleteEvent = function(req, res) {
userModel.findOne({'tokens.token': req.headers['x-access-token']})
.exec(function(err, user){
if (err) return res.send(500, err.message);
eventModel.findOne({
_id: req.params.eventid,
user: user._id
})
.exec(function(err, event) {
if (err) return res.send(500, err.message);
if(event.user.equals(user._id))
{
event.remove(function(err) {
if(err) return res.send(500, err.message);
console.log("deleted");
exports.getAllEvents(req, res);
});
}
});
});
};//funciona, pero no esborra les referències dels users als events que s'esborren. Més endavant caldria fer-ho.
userModel.findOne({
'tokens.token': req.headers['x-access-token']
})
.exec(function(err, user) {
if (err) return res.send(500, err.message);
eventModel.findOne({
_id: req.params.eventid,
user: user._id
})
.exec(function(err, event) {
if (err) return res.send(500, err.message);
if (event.user.equals(user._id)) {
event.remove(function(err) {
if (err) return res.send(500, err.message);
console.log("deleted");
exports.getAllEvents(req, res);
});
}
});
});
}; //funciona, pero no esborra les referències dels users als events que s'esborren. Més endavant caldria fer-ho.
/*
un get events by following, que seria:
@ -175,24 +231,29 @@ això retorna els events d'aquests users que segueixes
*/
exports.getEventsByFollowingArray = function(req, res) {
if(req.body.users==null)
{
res.status(200).jsonp(null);
}
eventModel.find({
date: {$gte: new Date()},
'user': req.body.users,
type: {$nin: ["alert"]}//cal filtrar per type d'event, aquí només agafem els type: alert
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
/*if (req.body.users == null) {
res.status(200).jsonp([]);
}*/
eventModel.find({
date: {
$gte: new Date()
},
'user': req.body.users,
type: {
$nin: ["alert"]
} //cal filtrar per type d'event, aquí només agafem els type: alert
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function(err, events) {
if (err) return res.send(500, err.message);
console.log(events);
res.status(200).jsonp(events);
});
};
/*
@ -201,35 +262,37 @@ s'envia un get /events/category/:category
això retorna els events d'aquests users que segueixes
*/
exports.getEventsByCategory = function(req, res) {
eventModel.find({
date: {$gte: new Date()},
'categories.name': req.params.category
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
eventModel.find({
date: {
$gte: new Date()
},
'categories.name': req.params.category
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function(err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
};
exports.getEventsByDay = function(req, res) {
var dayRequested= new Date(req.params.day);
eventModel.find({
date: {
$gte: dayRequested
}
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function (err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
var dayRequested = new Date(req.params.day);
eventModel.find({
date: {
$gte: dayRequested
}
})
.lean()
.populate('user', 'username img shortDescription')
.sort('date')
.limit(pageSize)
.skip(pageSize * Number(req.query.page))
.exec(function(err, events) {
if (err) return res.send(500, err.message);
res.status(200).jsonp(events);
});
};

+ 2
- 1
package.json

@ -15,6 +15,7 @@
"method-override": "^2.1.2",
"mongoose": "latest",
"morgan": "latest",
"mongoose-unique-validator": "^1.0.2"
"mongoose-unique-validator": "^1.0.2",
"request": "^2.81.0"
}
}

+ 5
- 2
server.js

@ -17,10 +17,13 @@ mongoose.connect(config.database, function(err, res) {
app.set('superSecret', config.secret); // secret variable
// Middlewares
app.use(bodyParser.urlencoded({
/*app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(bodyParser.json());*/
app.use(bodyParser.json({limit: '2mb'}));
app.use(bodyParser.urlencoded({limit: '2mb', extended: true}));
app.use(methodOverride());
// use morgan to log requests to the console

Loading…
Cancel
Save