From 48eb01dd5e3abca70b3810c6477e43064f8c404c Mon Sep 17 00:00:00 2001
From: nau
Date: Thu, 8 Sep 2016 23:56:46 +0200
Subject: [PATCH] delete project and join project added (frontend and backend)
---
controllers/projectController.js | 40 ++++++++++++++++++---
models/projectModel.js | 6 ++--
server.js | 3 ++
webapp/controllers.js | 60 ++++++++++++++++++++++++++++----
webapp/dashboard.html | 12 +++++--
5 files changed, 104 insertions(+), 17 deletions(-)
diff --git a/controllers/projectController.js b/controllers/projectController.js
index 26f7d31..8b064b0 100644
--- a/controllers/projectController.js
+++ b/controllers/projectController.js
@@ -61,7 +61,31 @@ exports.addProject = function(req, res) {
project.save(function(err, project) {
if(err) return res.send(500, err.message);
- res.status(200).jsonp(project);
+ /*res.status(200).jsonp(project);*/
+ projectModel.find(function(err, projects) {
+ if(err) res.send(500, err.message);
+
+ res.status(200).jsonp(projects);
+ });
+ });
+};
+
+exports.addUserToProject = function(req, res) {
+ console.log("addUserToProject");
+ console.log(req.body);
+ projectModel.findById(req.params.id, function(err, project) {
+ console.log(project);
+ project.users.push(req.body.username);
+ console.log(project.users);
+ project.save(function(err) {
+ if(err) return res.send(500, err.message);
+
+ projectModel.find(function(err, projects) {
+ if(err) res.send(500, err.message);
+
+ res.status(200).jsonp(projects);
+ });
+ });
});
};
@@ -85,11 +109,17 @@ exports.updateProject = function(req, res) {
//DELETE
exports.deleteProject = function(req, res) {
- ActivityModel.findById(req.params.id, function(err, activity) {
- activity.remove(function(err) {
+ projectModel.findById(req.params.id, function(err, project) {
+ project.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);
+
+ /*res.status(200).jsonp(req.params.id);*/
+ console.log('DELETE /projects/' + req.params.id);
+ projectModel.find(function(err, projects) {
+ if(err) res.send(500, err.message);
+
+ res.status(200).jsonp(projects);
+ });
})
});
};
diff --git a/models/projectModel.js b/models/projectModel.js
index 5cae477..e1a7cdb 100644
--- a/models/projectModel.js
+++ b/models/projectModel.js
@@ -2,17 +2,17 @@ var mongoose = require('mongoose'),
Schema = mongoose.Schema;
-var travelSchema = new Schema({
+var projectSchema = new Schema({
title: { type: String },
description: { type: String },
icon: { type: String },
- users: { type: String },
+ users: [{ type: String }],
chart: { type: String },
dateCreation: { type: Date },
github: { type: String },
refnum: { type: String }
})
-module.exports = mongoose.model('projectModel', travelSchema);
+module.exports = mongoose.model('projectModel', projectSchema);
//modality can be: offering, asking, package
diff --git a/server.js b/server.js
index f1df13a..e0f0700 100755
--- a/server.js
+++ b/server.js
@@ -124,6 +124,9 @@ apiRoutes.route('/projects/:id')
.put(projectCtrl.updateProject)
.delete(projectCtrl.deleteProject);
+apiRoutes.route('/projects/:id/adduser')
+ .put(projectCtrl.addUserToProject);
+
app.use('/api', apiRoutes);
// end of API routes -------------------------------------
diff --git a/webapp/controllers.js b/webapp/controllers.js
index 857de6f..c26d24c 100644
--- a/webapp/controllers.js
+++ b/webapp/controllers.js
@@ -9,21 +9,28 @@ angular.module('workApp', ['chart.js'])
$scope.users={};
$scope.projects={};
$scope.currentInclude='login.html';
+ var refreshTime=20000;
/* DASHBOARD initialization */
$scope.dashboardInit = function(){
if(localStorage.getItem('owt_token')){// adding token to the headers
$http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('owt_token');
+ $http.defaults.headers.post['Content-Type'] = 'application/json';
$http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('owt_token');
}
+ if(localStorage.getItem("owt_user")){
+ $scope.user=JSON.parse(localStorage.getItem("owt_user"));
+ }else{
+ $scope.currentInclude="login.html";
+ }
//getting users
$http.get(urlapi + 'users')
.success(function(data, status, headers,config){
if(data.success==false){
- localStorage.removeItem("owt_token");
+ /*localStorage.removeItem("owt_token");
localStorage.removeItem("owt_user");
$scope.currentInclude="login.html";
- console.log("token ended");
+ console.log("token ended");*/
}else{
console.log(data);
$scope.users=data;
@@ -64,7 +71,7 @@ angular.module('workApp', ['chart.js'])
$scope.dashboardInit();
intervalGetData = $interval(function(){
$scope.dashboardInit();
- }, 2000);
+ }, refreshTime);
}else{
$scope.currentInclude="login.html";
}
@@ -107,7 +114,7 @@ angular.module('workApp', ['chart.js'])
intervalGetData = $interval(function(){
$scope.dashboardInit();
- }, 2000);
+ }, refreshTime);
}else{
toastr.error(response.data.message);
}
@@ -159,6 +166,7 @@ angular.module('workApp', ['chart.js'])
}).then(function(response) {
console.log("project posted");
console.log(response);
+ $scope.projects=response.data;
toastr.success("project created at server");
$scope.newproject={};
},
@@ -179,14 +187,43 @@ angular.module('workApp', ['chart.js'])
$scope.editingProject=false;
};
$scope.removeProject = function(index){
- $scope.projects.splice(index, 1);
- localStorage.setItem("w_l_projects", angular.toJson($scope.projects));
- $scope.projectSelect(0);
+ /*$scope.projects.splice(index, 1);
+ localStorage.setItem("w_l_projects", angular.toJson($scope.projects));*/
+ $http({
+ url: urlapi + 'projects/' + $scope.projects[index]._id,
+ method: "DELETE",
+ data: $scope.user._id
+ }).then(function(response) {
+ console.log(response);
+ $scope.projects=response.data;
+ $scope.projectSelect(null);
+ },
+ function(response) {// failed
+ });
};
+ $scope.currentprojectIndex;
$scope.projectSelect = function(index){
$scope.btnStop();
+ $scope.currentprojectIndex=index;
$scope.currentproject=$scope.projects[index];
};
+
+ $scope.joinProject = function(){
+ console.log($scope.user);
+ $http({
+ url: urlapi + 'projects/'+$scope.currentproject._id+'/adduser',
+ method: "PUT",
+ data: $scope.user
+ }).then(function(response) {
+ console.log("project joined");
+ $scope.projects=response.data;
+ //re select currentproject
+ $scope.currentproject=$scope.projects[$scope.currentprojectIndex];
+ toastr.success("project joined");
+ },
+ function(response) {// failed
+ });
+ };
var interval;
$scope.currentStrike=0;
$scope.btnWork = function(){
@@ -197,6 +234,15 @@ angular.module('workApp', ['chart.js'])
$scope.currentStrike++;
$scope.currentproject.totaltime++;
}, 1000);
+ $http({
+ url: urlapi + 'users',
+ method: "POST",
+ data: $scope.user
+ }).then(function(response) {
+ $scope.currentInclude="login.html";
+ },
+ function(response) {// failed
+ });
};
$scope.btnStop = function(){
$interval.cancel(interval);
diff --git a/webapp/dashboard.html b/webapp/dashboard.html
index 555557e..dea00a4 100644
--- a/webapp/dashboard.html
+++ b/webapp/dashboard.html
@@ -78,6 +78,9 @@
{{project.title}}
{{project.description}}
{{project.totaltime | secondsToDateTime | date:'HH:mm:ss'}}
+
+
{{user}}
+
@@ -125,8 +128,13 @@