diff --git a/app/models/ConvModl.js b/app/models/ConvModl.js new file mode 100644 index 0000000..65a2351 --- /dev/null +++ b/app/models/ConvModl.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Conv', { + titre: { + type: String, + default: "Conversation" + }, + started: { + type: Date, + default: Date.now + } +}); diff --git a/app/routes/ApiRtes.js b/app/routes/ApiRtes.js index e1f2ad6..4fc1367 100644 --- a/app/routes/ApiRtes.js +++ b/app/routes/ApiRtes.js @@ -1,6 +1,7 @@ var MembresServ = require('../services/MembresServ'); var SessionsServ = require('../services/SessionsServ'); var DecryptServ = require('../services/DecryptServ'); +var ConvsServ = require('../services/ConvsServ'); var express = require('express'); var api = express(); @@ -92,4 +93,37 @@ api.delete('/membres/:membre_id', function (req, res) { // Supression d'un membr }); }); + +// Conversations +api.get('/convs', function (req, res) { // Liste des convs + ConvsServ.list(function (err, convs) { + if (err) + res.send(err); + else + res.json(convs); + }); +}); + +api.post('/convs', function (req, res) { // Ajout d'un conv + ifPermission(req, res, 'canAddConv', function () { + ConvsServ.add(req.body, function (err, conv) { + if (err) + res.send(err); + else + res.json(conv); + }); + }); +}); + +api.delete('/convs/:conv_id', function (req, res) { // Supression d'un conv + ifPermission(req, res, 'canDelConv', function () { + ConvsServ.remove(req.params.conv_id, function (err, conv) { + if (err) + res.send(err); + else + res.json(null); + }); + }); +}); + module.exports = api; diff --git a/app/services/ConvsServ.js b/app/services/ConvsServ.js new file mode 100644 index 0000000..1412774 --- /dev/null +++ b/app/services/ConvsServ.js @@ -0,0 +1,51 @@ +var ConvModl = require('../models/ConvModl'); +// var NomsServ = require('../services/NomsServ'); +var async = require('async'); + +var ConvsServ = {}; + +ConvsServ.addData = function (Conv, cb) { + // NomsServ.get(Conv.login, function (nom) { + // if (nom) { + // Conv.nom = nom; + // } else { + // Conv.nom = Conv.login; + // } + // cb(null, Conv); + // }); + // TODO Démarré par + // TODO Dernier message + cb(null, Conv); +}; + +ConvsServ.get = function(id, cb) { + ConvModl.findById(id).lean().exec(function (err, Conv) { + if (err) + cb(err); + else + ConvsServ.addData(Conv, cb); + }); +}; + +ConvsServ.list = function (cb) { + ConvModl.find({}).lean().exec(function (err, Convs) { + async.mapSeries(Convs, ConvsServ.addData, cb); + }); +}; + +ConvsServ.add = function (data, cb) { + ConvModl.create({ + titre: data.titre + }, function(err, Conv) { + ConvsServ.get(Conv._id, cb); + }); +}; + +ConvsServ.remove = function (id, cb) { + // TODO Trash + ConvModl.remove({ + _id: id + }, cb); +}; + +module.exports = ConvsServ; diff --git a/app/services/SessionsServ.js b/app/services/SessionsServ.js index e883fff..61ac047 100644 --- a/app/services/SessionsServ.js +++ b/app/services/SessionsServ.js @@ -15,6 +15,8 @@ sessions.addData = function (session, cb) { } session.canAddMembre = session.login == 'gbontoux'; session.canDelMembre = session.login == 'gbontoux'; + session.canAddConv = session.login == 'gbontoux'; + session.canDelConv = session.login == 'gbontoux'; cb(session); }); }; diff --git a/public/js/app.js b/public/js/app.js index bdf6f77..9863e42 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,4 +1,4 @@ -angular.module('ciApp', ['ngAnimate', 'ngRoute', 'SessionsCtrl', 'ConnectCtrl', 'MembreCtrl']).config(['$routeProvider', '$locationProvider', +angular.module('ciApp', ['ngAnimate', 'ngRoute', 'ConnectCtrl', 'MembreCtrl', 'ForumDirCtrl', 'SessionsCtrl']).config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $routeProvider .when('/', { @@ -8,6 +8,10 @@ angular.module('ciApp', ['ngAnimate', 'ngRoute', 'SessionsCtrl', 'ConnectCtrl', templateUrl: 'views/membres.html', controller: 'MembreCtrl' }) + .when('/forum', { + templateUrl: 'views/forumDir.html', + controller: 'ForumDirCtrl' + }) .when('/connect', { templateUrl: 'views/connect.html', controller: 'ConnectCtrl' diff --git a/public/js/controllers/ForumDirCtrl.js b/public/js/controllers/ForumDirCtrl.js new file mode 100644 index 0000000..5544fdf --- /dev/null +++ b/public/js/controllers/ForumDirCtrl.js @@ -0,0 +1,31 @@ +angular.module('ForumDirCtrl', ['SessionsServ', 'ForumServ', 'NotifyServ']).controller('ForumDirCtrl', ['$scope', 'SessionServ', 'ForumServ', 'NotifyServ', + function ($scope, SessionServ, ForumServ, NotifyServ) { + $scope.convs = []; + $scope.formData = {}; + + $scope.session = SessionServ.cur; + SessionServ.onChange(function () { + $scope.session = SessionServ.cur; + }); + + ForumServ.getConvs(function(err, convs) { + if (!err) + $scope.convs = convs; + }); + + $scope.createConv = function () { + ForumServ.createConv($scope.formData, function(err, conv) { + if (!err) + $scope.convs.push(conv); + }); + }; + + $scope.deleteConv = function (index) { + ForumServ.deleteConv($scope.convs[index]._id, function(err) { + if (!err) + $scope.convs.splice(index, 1); + }); + }; + + } +]); diff --git a/public/js/services/ForumServ.js b/public/js/services/ForumServ.js new file mode 100644 index 0000000..b65668e --- /dev/null +++ b/public/js/services/ForumServ.js @@ -0,0 +1,41 @@ +angular.module('ForumServ', ['NotifyServ']).service('ForumServ', ['$http', 'NotifyServ', + function ($http, NotifyServ) { + a = { + getConvs: function (cb) { + // TODO Dirs + $http.get('/api/convs') + .success(function (data) { + cb(null, data); + }) + .error(function (data) { // TODO CBs + NotifyServ.error("Impossible d'obtenir la liste des convs", data); + }); + }, + + createConv: function (data, cb) { + var not = NotifyServ.promise("Ajout du conv..."); + $http.post('/api/convs', data) + .success(function (conv) { + not.success("Conv ajouté"); + cb(null, conv); + }) + .error(function (data) { + not.error("Impossible d'ajouter le conv"); + }); + }, + + deleteConv: function (id, cb) { + var not = NotifyServ.promise("Suppression du conv..."); + $http.delete('/api/convs/' + id) + .success(function (conv) { + not.success("Conv supprimé"); + cb(null); + }) + .error(function (data) { + not.error("Impossible de supprimer le conv", data); + }); + } + }; + return a; + } +]); diff --git a/public/views/forumDir.html b/public/views/forumDir.html new file mode 100644 index 0000000..25ed536 --- /dev/null +++ b/public/views/forumDir.html @@ -0,0 +1,53 @@ +