Skip to content

Commit

Permalink
Merge pull request #31 from minhaufcg/multiMap
Browse files Browse the repository at this point in the history
Multi map
  • Loading branch information
GustavoDinizMonteiro authored Dec 7, 2017
2 parents 031bcf3 + 8814e91 commit ddcf3bf
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 39 deletions.
30 changes: 30 additions & 0 deletions app/controllers/locations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const mongoose = require('mongoose');
const Campus = require('../models/campi');
const Coordinations = require('../models/coords');
const RestHelper = require('../helpers/rest-helper');
mongoose.Promise = require('bluebird');

function getAllCampi (req, res) {
Campus
.find({}).then(function (data) {
console.log(data);
RestHelper.sendJsonResponse(res, 200, data);
})
}

function getCampusCoords(req, res) {
if (!req.params || !req.params.campusId)
RestHelper.sendJsonResponse(res, 404, {error : "No campus ID provided"});

var campusId = req.params.campusId;

Coordinations
.getByCampus(campusId).then(function (coords) {
RestHelper.sendJsonResponse(res, 200, coords);
})
}

module.exports = {
getAllCampi : getAllCampi,
getCampusCoords : getCampusCoords
};
9 changes: 5 additions & 4 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const usersReadOne = function (req, res) {

const usersCreateOne = function (req, res) {
const newUser = new User();
newUser.name = req.body.name,
newUser.email = req.body.email,
newUser.role = req.body.role,
newUser.registration = req.body.registration
newUser.name = req.body.name;
newUser.email = req.body.email;
newUser.role = req.body.role;
newUser.registration = req.body.registration;
newUser.setPassword(req.body.password);
newUser.campus = req.body.campus;

newUser
.save()
Expand Down
11 changes: 11 additions & 0 deletions app/models/campi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const campusSchema = Schema({
name: {
type: String,
required: true
}
}, {collection : 'campi'});

module.exports = mongoose.model("Campus", campusSchema);
37 changes: 37 additions & 0 deletions app/models/coords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const coordsSchema = Schema({
campus: {
type: Schema.Types.ObjectId,
ref : "Campus",
required: true
},
center : {
lat : {
type : Number,
required : true
},
lng : {
type : Number,
required : true
}
},
coords : {
type : [{
lat : Number,
lng : Number
}],
required: true
}
}, {collection : 'coords'});

coordsSchema.statics.getByCampus = function (campusId) {
return this.findOne({ campus: campusId }).exec(function (coords) {
return coords;
});
};


module.exports = mongoose.model("Coordinations", coordsSchema);

2 changes: 2 additions & 0 deletions app/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ process.on('SIGTERM', function () {
// Models
require('./requests');
require('./users');
require('./coords');
require('./campi');
8 changes: 7 additions & 1 deletion app/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const userSchema = Schema({
unique: false,
required: true
},
campus : {
type: Schema.Types.ObjectId,
ref : "Campus",
required: true
},
hash: String
});

Expand All @@ -51,7 +56,8 @@ userSchema.methods.generateJwt = function() {
const payload = {
id: this._id,
registration: this.registration,
name: this.name
name: this.name,
campus: this.campus
};

const token = jwt.sign(
Expand Down
4 changes: 4 additions & 0 deletions app/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jwt = require('express-jwt');
const constants = require('../config/constants')
const requestsCtrl = require('../controllers/requests');
const usersCtrl = require('../controllers/users');
const locationsCtrl = require('../controllers/locations');

const router = express.Router();
const auth = jwt({
Expand All @@ -24,4 +25,7 @@ router.put('/users/:userId', auth, usersCtrl.usersUpdateOne);
router.delete('/users/:userId', auth, usersCtrl.usersDeleteOne);
router.post('/login/', usersCtrl.login);

router.get('/campi/', locationsCtrl.getAllCampi);
router.get('/campi/:campusId/coords', locationsCtrl.getCampusCoords);

module.exports = router;
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gulp.task('angular', () => {
presets: ['es2015'],
plugins: ['angularjs-annotate']
}))
.pipe(uglify())
//.pipe(uglify())
.pipe(gulp.dest('public/dist'));
});

Expand Down
11 changes: 5 additions & 6 deletions public/js/components/map-modal.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
angular.module('mufcg')
.controller('MapModalCtrl', function ($scope, LOCATIONS, NgMap, messagebox, $uibModalInstance, mapHelper) {
.controller('MapModalCtrl', function ($scope, NgMap, messagebox, $uibModalInstance, mapHelper, AuthService) {
var map = undefined;
var ufcgPolygon = undefined;

NgMap.getMap().then(function(mapResult) {
map = mapResult;

google.maps.event.trigger(map, "resize");
map.setCenter(LOCATIONS.UFCG.center);
mapHelper.initMap(map,AuthService.getCurrentUser());

ufcgPolygon = mapHelper.getPolygon(LOCATIONS.UFCG.polygon);
mapHelper.deleteAllMarkers();
});

Expand All @@ -18,7 +17,7 @@ angular.module('mufcg')
var lat = event.latLng.lat();
var lng = event.latLng.lng();

if (!google.maps.geometry.poly.containsLocation(event.latLng, ufcgPolygon)) {
if (!google.maps.geometry.poly.containsLocation(event.latLng, mapHelper.getPolygon())) {
messagebox.fail('O local que você escolheu para criar a ocorrência fica fora das dependências do seu campus','Local inválido')
}
else {
Expand Down Expand Up @@ -50,7 +49,7 @@ angular.module('mufcg')
lng: position.coords.longitude
};

if (!google.maps.geometry.poly.containsLocation(posFun, ufcgPolygon)) {
if (!google.maps.geometry.poly.containsLocation(posFun, mapHelper.getPolygon())) {
messagebox.fail("Você não está no campus, não é possível marcar esta localização", "Local inválido");
}
else {
Expand All @@ -72,7 +71,7 @@ angular.module('mufcg')
};

$scope.dragLimit = function() {
mapHelper.dragEnd(ufcgPolygon, LOCATIONS.UFCG.center);
mapHelper.dragEnd(mapHelper.getPolygon());
};

function setMarker(lat, lng, geolocation) {
Expand Down
19 changes: 8 additions & 11 deletions public/js/controllers/home-controller.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
angular.module('mufcg')
.controller('HomeCtrl', function ($scope, NgMap, mapHelper, LOCATIONS, AuthService, Request) {
.controller('HomeCtrl', function ($scope, NgMap, mapHelper, LOCATIONS, Location, AuthService, Request) {
var map = undefined;
var ufcgPolygon = undefined;

$scope.initMap = function () {
NgMap.getMap().then(function (mapResult) {
map = mapResult;

if (!mapHelper.getMap())
mapHelper.initMap(map);

mapHelper.deleteAllMarkers();

ufcgPolygon = mapHelper.getPolygon(LOCATIONS.UFCG.polygon);
mapHelper.initMap(map, AuthService.getCurrentUser());

loadAuthorRequests();
});
};

$scope.dragLimit = function () {
mapHelper.dragEnd(ufcgPolygon, LOCATIONS.UFCG.center);
};
// $scope.dragLimit = function () {
// mapHelper.dragEnd(ufcgPolygon, LOCATIONS.UFCG.center);
// };

function loadAuthorRequests() {
Request.getByAuthor(AuthService.getCurrentUser().id).then(function (res) {
Expand All @@ -29,7 +24,7 @@ angular.module('mufcg')
if ('location' in request) {
location.lat = request.location.lat;
location.lng = request.location.lng;
let image = undefined;
var image = undefined;
if (request.img) {
image = 'data:'.concat(request.img.filetype, ';base64,', request.img.base64);
}
Expand All @@ -39,6 +34,8 @@ angular.module('mufcg')
});
}



function createMarker(title, description, image, date, location) {
var contentString = `
<h1>${title}</h1>
Expand Down
17 changes: 13 additions & 4 deletions public/js/controllers/register-controller.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
angular.module('mufcg')
.controller('RegisterCtrl', function ($scope, $state, AuthService) {

.controller('RegisterCtrl', function ($scope, $state, AuthService, Location, messagebox) {
$scope.user = {};

function loadCampi() {
Location.getCampi().then(function (res) {
$scope.campi = res.data;
});
}

loadCampi();

$scope.register = function() {
if ($scope.user.password !== $scope.confirmPassword) {
alert('Senhas divergem!');
} else {
$scope.user.role = "student";
AuthService.register($scope.user).then(function () {
alert('Usuário cadastrado com sucesso');
messagebox.success('Usuário cadastrado com sucesso');
$state.go("login");
}, function error(response) {
console.warn(response.message);
messagebox.fail(response.data.message);
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion public/js/directives/nav-bar/nav-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ angular.module('mufcg')
templateUrl : '/templates/directives/nav-bar/nav-bar.html',
scope : {},
controller : function ($scope, $location, AuthService) {
$scope.enabled = false;
$scope.isCollapsed = false;
const FORBIDDEN = ['/login', '/register', '/'];
$scope.enabled = FORBIDDEN.indexOf($location.url()) === -1;

$scope.getCollapseClass = function () {
return $scope.isCollapsed ?
Expand Down
11 changes: 11 additions & 0 deletions public/js/factories/rest/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
angular.module("mufcg").factory('Location', function RequestFactory($http, PROPERTIES) {
return {
getCampi : function () {
return $http.get(PROPERTIES.restBasePath + "/campi");
},

getCampusCoords : function (campusId) {
return $http.get(PROPERTIES.restBasePath + "/campi/" + campusId + "/coords/");
}
}
});
3 changes: 2 additions & 1 deletion public/js/services/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ angular
return {
name: payload.name,
registration: payload.registration,
id: payload.id
id: payload.id,
campus: payload.campus
};
}
};
Expand Down
25 changes: 20 additions & 5 deletions public/js/services/mapHelper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
angular.module('mufcg')
.service('mapHelper', function (NgMap ) {
.service('mapHelper', function (NgMap, Location) {
var markers = [];
var map = undefined;
var campusCenter = undefined;
var coords = undefined;
var polygon = undefined;

function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
}

return {
initMap : function (markersMap) {
initMap : function (markersMap, user) {
map = markersMap;

Location.getCampusCoords(user.campus).then(function (res) {
map.setCenter(res.data.center);
campusCenter = res.data.center;
coords = res.data.coords;
polygon = new google.maps.Polygon({paths: coords});
});
},
getCampusCenter : function () {
return campusCenter;
},
getMap : function () {
return map;
Expand All @@ -32,10 +47,10 @@ angular.module('mufcg')
clearMarkers();
markers = [];
},
getPolygon : function (coords) {
return new google.maps.Polygon({paths: coords});
getPolygon : function () {
return polygon;
},
dragEnd : function (polygon, center) {
dragEnd : function (center) {
if(!google.maps.geometry.poly.containsLocation(map.getCenter(), polygon))
map.setCenter(center);
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/services/messagebox-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ angular.module('mufcg')
};

const failModal = function (message, title, callback) {
defaultModal(getCustomTitle(title || "Falha de autenticação", 'white', true), message, 'small', getButton('ok', 'OK', 'btn-danger'), false, "danger-modal");
defaultModal(getCustomTitle(title || "Erro", 'white', true), message, 'small', getButton('ok', 'OK', 'btn-danger'), false, "danger-modal");
};

const defaultModal = function (title, message, size, buttons, closeButton, customClass, callback) {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added public/src/app/pipes/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion public/templates/components/map-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h3 class="modal-title" style="font-weight: bold;">Mapa do campus</h3>
<div class="modal-body" id="modal-body">
<div class="col-xs-offset-1 col-md-offset-3" map-lazy-load="https://maps.googleapis.com/maps/api/js"
map-lazy-load-params="https://maps.googleapis.com/maps/api/js?key=AIzaSyCg-DgACUXO_nUZ5W9A5VHe2H0wD-9tycs">
<ng-map center="-7.214250, -35.909188" zoom="17" min-zoom="17" style="height: 600px; width: 700px;" on-dragend="dragLimit()" on-click="createMarker()">
<ng-map zoom="17" map-type-id="hybrid" style="height: 600px; width: 700px;" on-dragend="dragLimit()" on-click="createMarker()">
</ng-map>
</div>
<br>
Expand Down
2 changes: 1 addition & 1 deletion public/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<div map-lazy-load="https://maps.googleapis.com/maps/api/js"
map-lazy-load-params="https://maps.googleapis.com/maps/api/js?key=AIzaSyCg-DgACUXO_nUZ5W9A5VHe2H0wD-9tycs" ng-init="initMap()" style="margin-top: -20px; min-width: 400px;">
<ng-map center="-7.214850, -35.907788" zoom="17" min-zoom="17" style="width: 100%; height: auto;" on-dragend="dragLimit()">
<ng-map map-type-id="hybrid" center="0,0" zoom="18" style="width: 100%; height: auto;" on-dragend="dragLimit()" on-click="click()">
</ng-map>
</div>
Loading

0 comments on commit ddcf3bf

Please sign in to comment.