mirror of
https://github.com/arnaucube/openworktime.git
synced 2026-02-06 19:26:41 +01:00
project architecture added to the backend, post new projects, webapp interval syncronization
This commit is contained in:
95
controllers/projectController.js
Normal file
95
controllers/projectController.js
Normal file
@@ -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);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -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
models/projectModel.js
Normal file
18
models/projectModel.js
Normal file
@@ -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
|
||||||
@@ -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
|
|
||||||
16
server.js
16
server.js
@@ -29,8 +29,8 @@ app.use(morgan('dev'));
|
|||||||
var userMdl = require('./models/userModel')(app, mongoose);
|
var userMdl = require('./models/userModel')(app, mongoose);
|
||||||
var userCtrl = require('./controllers/userController');
|
var userCtrl = require('./controllers/userController');
|
||||||
|
|
||||||
/*var projectMdl = require('./models/projectModel')(app, mongoose);
|
var projectMdl = require('./models/projectModel')(app, mongoose);
|
||||||
var projectCtrl = require('./controllers/projectController');*/
|
var projectCtrl = require('./controllers/projectController');
|
||||||
|
|
||||||
/*// Example Route
|
/*// Example Route
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
@@ -102,12 +102,12 @@ apiRoutes.route('/users/:id')
|
|||||||
.get(userCtrl.findById);
|
.get(userCtrl.findById);
|
||||||
apiRoutes.route('/users/byusername/:username')
|
apiRoutes.route('/users/byusername/:username')
|
||||||
.get(userCtrl.findUserByUsername);
|
.get(userCtrl.findUserByUsername);
|
||||||
/*apiRoutes.route('/projects/user/:username')
|
apiRoutes.route('/projects/user/:username')
|
||||||
.get(projectCtrl.findAllprojectsFromUsername);
|
.get(projectCtrl.findAllProjectsFromUsername);
|
||||||
|
|
||||||
|
|
||||||
apiRoutes.route('/projects')
|
apiRoutes.route('/projects')
|
||||||
.get(projectCtrl.findAllprojects);
|
.get(projectCtrl.findAllProjects);
|
||||||
|
|
||||||
apiRoutes.route('/projects/:id')
|
apiRoutes.route('/projects/:id')
|
||||||
.get(projectCtrl.findById);
|
.get(projectCtrl.findById);
|
||||||
@@ -118,11 +118,11 @@ apiRoutes.route('/users/:id')
|
|||||||
.delete(userCtrl.deleteUser);
|
.delete(userCtrl.deleteUser);
|
||||||
|
|
||||||
apiRoutes.route('/projects')
|
apiRoutes.route('/projects')
|
||||||
.post(projectCtrl.addproject);
|
.post(projectCtrl.addProject);
|
||||||
|
|
||||||
apiRoutes.route('/projects/:id')
|
apiRoutes.route('/projects/:id')
|
||||||
.put(projectCtrl.updateproject)
|
.put(projectCtrl.updateProject)
|
||||||
.delete(projectCtrl.deleteproject);*/
|
.delete(projectCtrl.deleteProject);
|
||||||
|
|
||||||
app.use('/api', apiRoutes);
|
app.use('/api', apiRoutes);
|
||||||
// end of API routes -------------------------------------
|
// end of API routes -------------------------------------
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ angular.module('workApp', ['chart.js'])
|
|||||||
$interval,
|
$interval,
|
||||||
$http
|
$http
|
||||||
) {
|
) {
|
||||||
|
$scope.users={};
|
||||||
|
$scope.projects={};
|
||||||
$scope.currentInclude='login.html';
|
$scope.currentInclude='login.html';
|
||||||
/* DASHBOARD initialization */
|
/* DASHBOARD initialization */
|
||||||
$scope.dashboardInit = function(){
|
$scope.dashboardInit = function(){
|
||||||
@@ -18,11 +19,34 @@ angular.module('workApp', ['chart.js'])
|
|||||||
//getting users
|
//getting users
|
||||||
$http.get(urlapi + 'users')
|
$http.get(urlapi + 'users')
|
||||||
.success(function(data, status, headers,config){
|
.success(function(data, status, headers,config){
|
||||||
console.log(data);
|
if(data.success==false){
|
||||||
$scope.users=data;
|
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){
|
.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){
|
||||||
|
users = result.data;
|
||||||
|
});
|
||||||
|
|
||||||
|
//getting projects
|
||||||
|
$http.get(urlapi + 'projects')
|
||||||
|
.success(function(data, status, headers,config){
|
||||||
|
console.log(data);
|
||||||
|
$scope.projects=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
|
$scope.$broadcast('scroll.refreshComplete');//refresher stop
|
||||||
})
|
})
|
||||||
.then(function(result){
|
.then(function(result){
|
||||||
@@ -38,6 +62,9 @@ angular.module('workApp', ['chart.js'])
|
|||||||
$scope.user=JSON.parse(localStorage.getItem("owt_user"));
|
$scope.user=JSON.parse(localStorage.getItem("owt_user"));
|
||||||
$scope.currentInclude="dashboard.html";
|
$scope.currentInclude="dashboard.html";
|
||||||
$scope.dashboardInit();
|
$scope.dashboardInit();
|
||||||
|
intervalGetData = $interval(function(){
|
||||||
|
$scope.dashboardInit();
|
||||||
|
}, 2000);
|
||||||
}else{
|
}else{
|
||||||
$scope.currentInclude="login.html";
|
$scope.currentInclude="login.html";
|
||||||
}
|
}
|
||||||
@@ -62,7 +89,7 @@ angular.module('workApp', ['chart.js'])
|
|||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
var intervalGetData;//out of internal scope
|
||||||
$scope.onBtnLogin = function(){
|
$scope.onBtnLogin = function(){
|
||||||
$http({
|
$http({
|
||||||
url: urlapi + 'auth',
|
url: urlapi + 'auth',
|
||||||
@@ -77,10 +104,10 @@ angular.module('workApp', ['chart.js'])
|
|||||||
|
|
||||||
$scope.currentInclude="dashboard.html";
|
$scope.currentInclude="dashboard.html";
|
||||||
$scope.dashboardInit();
|
$scope.dashboardInit();
|
||||||
var intervalGetData;
|
|
||||||
intervalGetData = $interval(function(){
|
intervalGetData = $interval(function(){
|
||||||
$scope.dashboardInit();
|
$scope.dashboardInit();
|
||||||
}, 10000);
|
}, 2000);
|
||||||
}else{
|
}else{
|
||||||
toastr.error(response.data.message);
|
toastr.error(response.data.message);
|
||||||
}
|
}
|
||||||
@@ -96,6 +123,7 @@ angular.module('workApp', ['chart.js'])
|
|||||||
}).then(function(response) {
|
}).then(function(response) {
|
||||||
localStorage.removeItem("owt_token");
|
localStorage.removeItem("owt_token");
|
||||||
localStorage.removeItem("owt_user");
|
localStorage.removeItem("owt_user");
|
||||||
|
$interval.cancel(intervalGetData);
|
||||||
$scope.currentInclude="login.html";
|
$scope.currentInclude="login.html";
|
||||||
},
|
},
|
||||||
function(response) {// failed
|
function(response) {// failed
|
||||||
@@ -108,35 +136,12 @@ angular.module('workApp', ['chart.js'])
|
|||||||
|
|
||||||
//localStorage.clear();
|
//localStorage.clear();
|
||||||
$scope.working=false;
|
$scope.working=false;
|
||||||
$scope.projects=[];
|
|
||||||
$scope.currentproject={};
|
$scope.currentproject={};
|
||||||
if(localStorage.getItem("w_l_projects"))
|
|
||||||
{
|
$scope.newproject={};
|
||||||
$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.id=$scope.projects[$scope.projects.length-1].id+1;
|
//$scope.newproject.id=$scope.projects[$scope.projects.length-1].id+1;
|
||||||
$scope.addNewProject = function(){
|
$scope.addNewProject = function(){
|
||||||
$scope.newproject.totaltime="0";
|
/*$scope.newproject.totaltime="0";
|
||||||
$scope.projects.push($scope.newproject);
|
$scope.projects.push($scope.newproject);
|
||||||
localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
|
localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
|
||||||
$scope.newproject={
|
$scope.newproject={
|
||||||
@@ -146,7 +151,19 @@ angular.module('workApp', ['chart.js'])
|
|||||||
series: ['Working time'],
|
series: ['Working time'],
|
||||||
data: []
|
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.editingIndex="";
|
||||||
$scope.editProject = function(index){
|
$scope.editProject = function(index){
|
||||||
|
|||||||
@@ -76,6 +76,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<img ng-src="img/{{project.icon}}.png" class="circle"/>
|
<img ng-src="img/{{project.icon}}.png" class="circle"/>
|
||||||
<span class="title">{{project.title}}</span>
|
<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>
|
<p class="grey-text">{{project.totaltime | secondsToDateTime | date:'HH:mm:ss'}}</p>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
@@ -89,6 +90,10 @@
|
|||||||
<input ng-model="newproject.icon" id="icon" type="text" class="validate">
|
<input ng-model="newproject.icon" id="icon" type="text" class="validate">
|
||||||
<label for="icon">icon</label>
|
<label for="icon">icon</label>
|
||||||
</div>
|
</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>
|
<a ng-click="addNewProject()" class="waves-effect waves-light btn blue-grey lighten-1 right">Add new project</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-action" ng-show="editingProject">
|
<div class="card-action" ng-show="editingProject">
|
||||||
|
|||||||
Reference in New Issue
Block a user