Browse Source

project architecture added to the backend, post new projects, webapp interval syncronization

master
nau 8 years ago
parent
commit
3df8ff8c70
7 changed files with 176 additions and 168 deletions
  1. +95
    -0
      controllers/projectController.js
  2. +0
    -102
      controllers/travelController.js
  3. +18
    -0
      models/projectModel.js
  4. +0
    -25
      models/travelModel.js
  5. +8
    -8
      server.js
  6. +50
    -33
      webapp/controllers.js
  7. +5
    -0
      webapp/dashboard.html

+ 95
- 0
controllers/projectController.js

@ -0,0 +1,95 @@
//File: controllers/projectController.js
var mongoose = require('mongoose');
var projectModel = mongoose.model('projectModel');
var userModel = mongoose.model('userModel');
//GET
exports.findAllProjects = function(req, res) {
projectModel.find(function(err, projects) {
if(err) res.send(500, err.message);
res.status(200).jsonp(projects);
});
};
//GET
exports.findById = function(req, res) {
projectModel.findById(req.params.id, function(err, project) {
if(err) return res.send(500, err.message);
console.log('GET /project/' + req.params.id);
res.status(200).jsonp(project);
});
};
exports.findAllProjectsFromUsername = function(req, res) {
projectModel.find({
owner: req.params.username
}, function(err, projects) {
if (err) throw err;
if (!projects) {
res.json({ success: false, message: 'no projects for user' });
} else if (projects) {
console.log(projects);
// return the information including token as JSON
res.jsonp(projects);
}
});
};
exports.addProject = function(req, res) {
console.log('POST new project, title: ' + req.body.title);
var project = new projectModel({
title: req.body.title,
description: req.body.description,
icon: req.body.icon,
users: req.body.users,
chart: req.body.chart,
dateCreation: req.body.dateCreation,
github: req.body.github,
refnum: req.body.refnum
});
project.save(function(err, project) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(project);
});
};
//PUT
exports.updateProject = function(req, res) {
ActivityModel.findById(req.params.id, function(err, tvshow) {
tvshow.title = req.body.petId;
tvshow.year = req.body.year;
tvshow.country = req.body.country;
tvshow.poster = req.body.poster;
tvshow.seasons = req.body.seasons;
tvshow.genre = req.body.genre;
tvshow.summary = req.body.summary;
tvshow.save(function(err) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(tvshow);
});
});
};
//DELETE
exports.deleteProject = function(req, res) {
ActivityModel.findById(req.params.id, function(err, activity) {
activity.remove(function(err) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(req.params.id);
console.log('DELETE /activities/' + req.params.id);
})
});
};

+ 0
- 102
controllers/travelController.js

@ -1,102 +0,0 @@
//File: controllers/travelController.js
var mongoose = require('mongoose');
var travelModel = mongoose.model('travelModel');
var userModel = mongoose.model('userModel');
//GET
exports.findAllTravels = function(req, res) {
travelModel.find(function(err, travels) {
if(err) res.send(500, err.message);
res.status(200).jsonp(travels);
});
};
//GET
exports.findById = function(req, res) {
travelModel.findById(req.params.id, function(err, travel) {
if(err) return res.send(500, err.message);
console.log('GET /travel/' + req.params.id);
res.status(200).jsonp(travel);
});
};
exports.findAllTravelsFromUsername = function(req, res) {
travelModel.find({
owner: req.params.username
}, function(err, travels) {
if (err) throw err;
if (!travels) {
res.json({ success: false, message: 'no travels for user' });
} else if (travels) {
console.log(travels);
// return the information including token as JSON
res.jsonp(travels);
}
});
};
exports.addTravel = function(req, res) {
console.log('POST new travel, title: ' + req.body.title);
var travel = new travelModel({
title: req.body.title,
description: req.body.description,
owner: req.body.owner,
from: req.body.from,
to: req.body.to,
date: req.body.date,
periodic: req.body.periodic,
generateddate: req.body.generateddate,
seats: req.body.seats,
package: req.body.package,
icon: req.body.icon,
phone: req.body.phone,
telegram: req.body.telegram,
collectivized: req.body.collectivized,
modality: req.body.modality
});
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(travel);
});
};
//PUT
exports.updateTravel = function(req, res) {
ActivityModel.findById(req.params.id, function(err, tvshow) {
tvshow.title = req.body.petId;
tvshow.year = req.body.year;
tvshow.country = req.body.country;
tvshow.poster = req.body.poster;
tvshow.seasons = req.body.seasons;
tvshow.genre = req.body.genre;
tvshow.summary = req.body.summary;
tvshow.save(function(err) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(tvshow);
});
});
};
//DELETE
exports.deleteTravel = function(req, res) {
ActivityModel.findById(req.params.id, function(err, activity) {
activity.remove(function(err) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(req.params.id);
console.log('DELETE /activities/' + req.params.id);
})
});
};

+ 18
- 0
models/projectModel.js

@ -0,0 +1,18 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var travelSchema = new Schema({
title: { type: String },
description: { type: String },
icon: { type: String },
users: { type: String },
chart: { type: String },
dateCreation: { type: Date },
github: { type: String },
refnum: { type: String }
})
module.exports = mongoose.model('projectModel', travelSchema);
//modality can be: offering, asking, package

+ 0
- 25
models/travelModel.js

@ -1,25 +0,0 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var travelSchema = new Schema({
title: { type: String },
description: { type: String },
owner: { type: String },
from: { type: String },
to: { type: String },
date: { type: Date },
periodic: { type: Boolean },
generateddate: { type: Date },
seats: { type: Number },
package: { type: Boolean },
icon: { type: String },
phone: { type: Number },
telegram: { type: String },
collectivized: { type: Boolean },
modality: { type: String } //if is an offering travel or asking for travel
})
module.exports = mongoose.model('travelModel', travelSchema);
//modality can be: offering, asking, package

+ 8
- 8
server.js

@ -29,8 +29,8 @@ app.use(morgan('dev'));
var userMdl = require('./models/userModel')(app, mongoose);
var userCtrl = require('./controllers/userController');
/*var projectMdl = require('./models/projectModel')(app, mongoose);
var projectCtrl = require('./controllers/projectController');*/
var projectMdl = require('./models/projectModel')(app, mongoose);
var projectCtrl = require('./controllers/projectController');
/*// Example Route
var router = express.Router();
@ -102,12 +102,12 @@ apiRoutes.route('/users/:id')
.get(userCtrl.findById);
apiRoutes.route('/users/byusername/:username')
.get(userCtrl.findUserByUsername);
/*apiRoutes.route('/projects/user/:username')
.get(projectCtrl.findAllprojectsFromUsername);
apiRoutes.route('/projects/user/:username')
.get(projectCtrl.findAllProjectsFromUsername);
apiRoutes.route('/projects')
.get(projectCtrl.findAllprojects);
.get(projectCtrl.findAllProjects);
apiRoutes.route('/projects/:id')
.get(projectCtrl.findById);
@ -118,11 +118,11 @@ apiRoutes.route('/users/:id')
.delete(userCtrl.deleteUser);
apiRoutes.route('/projects')
.post(projectCtrl.addproject);
.post(projectCtrl.addProject);
apiRoutes.route('/projects/:id')
.put(projectCtrl.updateproject)
.delete(projectCtrl.deleteproject);*/
.put(projectCtrl.updateProject)
.delete(projectCtrl.deleteProject);
app.use('/api', apiRoutes);
// end of API routes -------------------------------------

+ 50
- 33
webapp/controllers.js

@ -6,7 +6,8 @@ angular.module('workApp', ['chart.js'])
$interval,
$http
) {
$scope.users={};
$scope.projects={};
$scope.currentInclude='login.html';
/* DASHBOARD initialization */
$scope.dashboardInit = function(){
@ -17,12 +18,35 @@ angular.module('workApp', ['chart.js'])
//getting users
$http.get(urlapi + 'users')
.success(function(data, status, headers,config){
if(data.success==false){
localStorage.removeItem("owt_token");
localStorage.removeItem("owt_user");
$scope.currentInclude="login.html";
console.log("token ended");
}else{
console.log(data);
$scope.users=data;
}
})
.error(function(data, status, headers,config){
console.log("server not responding, data error");
toastr.error("server not responding");
$scope.$broadcast('scroll.refreshComplete');//refresher stop
})
.then(function(result){
users = result.data;
});
//getting projects
$http.get(urlapi + 'projects')
.success(function(data, status, headers,config){
console.log(data);
$scope.users=data;
$scope.projects=data;
})
.error(function(data, status, headers,config){
console.log('data error');
console.log("server not responding, data error");
toastr.error("server not responding");
$scope.$broadcast('scroll.refreshComplete');//refresher stop
})
.then(function(result){
@ -38,6 +62,9 @@ angular.module('workApp', ['chart.js'])
$scope.user=JSON.parse(localStorage.getItem("owt_user"));
$scope.currentInclude="dashboard.html";
$scope.dashboardInit();
intervalGetData = $interval(function(){
$scope.dashboardInit();
}, 2000);
}else{
$scope.currentInclude="login.html";
}
@ -62,7 +89,7 @@ angular.module('workApp', ['chart.js'])
});
};
var intervalGetData;//out of internal scope
$scope.onBtnLogin = function(){
$http({
url: urlapi + 'auth',
@ -77,10 +104,10 @@ angular.module('workApp', ['chart.js'])
$scope.currentInclude="dashboard.html";
$scope.dashboardInit();
var intervalGetData;
intervalGetData = $interval(function(){
$scope.dashboardInit();
}, 10000);
}, 2000);
}else{
toastr.error(response.data.message);
}
@ -96,6 +123,7 @@ angular.module('workApp', ['chart.js'])
}).then(function(response) {
localStorage.removeItem("owt_token");
localStorage.removeItem("owt_user");
$interval.cancel(intervalGetData);
$scope.currentInclude="login.html";
},
function(response) {// failed
@ -108,35 +136,12 @@ angular.module('workApp', ['chart.js'])
//localStorage.clear();
$scope.working=false;
$scope.projects=[];
$scope.currentproject={};
if(localStorage.getItem("w_l_projects"))
{
$scope.projects=JSON.parse(localStorage.getItem("w_l_projects")); //w_local_
}
if($scope.projects.length>0)
{
$scope.newproject={
id: $scope.projects[$scope.projects.length-1].id+1,
chart: {
labels: [],
series: ['Working time'],
data: []
}
};
}else{
$scope.newproject={
id: 0,
chart: {
labels: [],
series: ['Working time'],
data: []
}
};
}
$scope.newproject={};
//$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
$scope.addNewProject = function(){
$scope.newproject.totaltime="0";
/*$scope.newproject.totaltime="0";
$scope.projects.push($scope.newproject);
localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
$scope.newproject={
@ -146,7 +151,19 @@ angular.module('workApp', ['chart.js'])
series: ['Working time'],
data: []
}
};
};*/
$http({
url: urlapi + 'projects',
method: "POST",
data: $scope.newproject
}).then(function(response) {
console.log("project posted");
console.log(response);
toastr.success("project created at server");
$scope.newproject={};
},
function(response) {// failed
});
};
$scope.editingIndex="";
$scope.editProject = function(index){

+ 5
- 0
webapp/dashboard.html

@ -76,6 +76,7 @@
</div>
<img ng-src="img/{{project.icon}}.png" class="circle"/>
<span class="title">{{project.title}}</span>
<p class="grey-text">{{project.description}}</p>
<p class="grey-text">{{project.totaltime | secondsToDateTime | date:'HH:mm:ss'}}</p>
</a>
</div>
@ -89,6 +90,10 @@
<input ng-model="newproject.icon" id="icon" type="text" class="validate">
<label for="icon">icon</label>
</div>
<div class="input-field col s12">
<input ng-model="newproject.description" id="description" type="text" class="validate">
<label for="description">description</label>
</div>
<a ng-click="addNewProject()" class="waves-effect waves-light btn blue-grey lighten-1 right">Add new project</a>
</div>
<div class="card-action" ng-show="editingProject">

Loading…
Cancel
Save