Commit 7cac6189 authored by Victor Mocelin's avatar Victor Mocelin

Arrumada transações com o banco. Fixes #2 and #3

A pesquisa de pessoas em um evento é feita de forma correta.
A inserção da presença de uma pessoa em um evento foi corrigida.
parent afad6c95
Pipeline #1356 skipped
angular.module('app.controllers', [])
.controller('ceitificatorCtrl', function($scope, $ionicPlatform, EventsService) {
$ionicPlatform.ready(function (){
$scope.events = EventsService.getEvents();
console.log($scope.events);
$ionicPlatform.ready(function () {
$scope.events = EventsService.getEvents();
});
})
.controller('novoEventoCtrl', function($scope, EventsService) {
$scope.formData = {};
$scope.addNewEvent = function (){
$scope.addNewEvent = function () {
// insere um novo evento no banco
if ((!$scope.formData.eventName || $scope.formData.eventName.length === 0 || !$scope.formData.eventName.trim()) ||
(!$scope.formData.eventDesc || $scope.formData.eventDesc.length === 0 || !$scope.formData.eventDesc.trim()) ||
(!$scope.formData.eventDate || $scope.formData.eventDate.length === 0 || !$scope.formData.eventDate.trim()) ||
(!$scope.formData.eventPlace || $scope.formData.eventPlace.length === 0 || !$scope.formData.eventPlace.trim()) ||
(!$scope.formData.eventHours || $scope.formData.eventHours.length === 0 || !$scope.formData.eventHours.trim()))
alert("WTF?");
else
EventsService.addNewEvent({eventName: $scope.formData.eventName, eventDesc: $scope.formData.eventDesc, eventDate: $scope.formData.eventDate, eventPlace: $scope.formData.eventPlace, eventHours: $scope.formData.eventHours});
(!$scope.formData.eventDate || $scope.formData.eventDate.length === 0 || !$scope.formData.eventDate.trim()) ||
(!$scope.formData.eventPlace || $scope.formData.eventPlace.length === 0 || !$scope.formData.eventPlace.trim()) ||
(!$scope.formData.eventHours || $scope.formData.eventHours.length === 0 || !$scope.formData.eventHours.trim()))
alert("WTF?"); // se deixou algum campo em branco, leva um alert na cara
else // se tá tudo certo, manda inserir o novo evento no banco
EventsService.addNewEvent({eventName: $scope.formData.eventName, eventDesc: $scope.formData.eventDesc, eventDate: $scope.formData.eventDate, eventPlace: $scope.formData.eventPlace, eventHours: $scope.formData.eventHours});
};
$scope.showCertText = function () {
// mostra o texto final (ou quase) que ficará no certificado
alert("Certificamos que !NOME! participou " + $scope.formData.eventDesc + " " + $scope.formData.eventName + ", realizada " + $scope.formData.eventDate + " na Universidade Federal do Paraná, " + $scope.formData.eventPlace + ", em Curitiba – PR, com a carga horária total de " + $scope.formData.eventHours + ".");
};
$scope.showCertText = function (){
alert("Certificamos que !NOME! participou " + $scope.formData.eventDesc + " " + $scope.formData.eventName + ", realizada " + $scope.formData.eventDate + " na Universidade Federal do Paraná, " + $scope.formData.eventPlace + ", em Curitiba – PR, com a carga horária total de " + $scope.formData.eventHours + ".");
}
})
.controller('listaDePresenAEventoXCtrl', function($scope, PeopleService) {
$scope.people = PeopleService.getPeople();
$scope.scan = function(){
.controller('listaDePresenAEventoXCtrl', function($scope, PeopleService, eventX) {
$scope.eventX = eventX;
$scope.people = PeopleService.getPeople(eventX.id);
$scope.scan = function() {
// faz o scan do qr code e marca a presença da pessoa
cordova.plugins.barcodeScanner.scan(
function (result) {
console.log(result.text.split(','));
PeopleService.addNewPerson({name: result.text.split(',')[0].trim(), grr: result.text.split(',')[1].trim(), email: result.text.split(',')[2].trim()});
$scope.people = PeopleService.getPeople();
if (result && result.text.split(',').length == 3) {
// pega o que veio do scanner, separa nos devidos campos e insere no banco
PeopleService.addPersonInEvent({name: result.text.split(',')[0].trim(), grr: result.text.split(',')[1].trim(), email: result.text.split(',')[2].trim()}, eventX.id);
}
},
function (error) {
alert("Scanning failed: " + error);
}
);
$scope.people = PeopleService.getPeople(eventX.id);
};
});
......@@ -8,8 +8,6 @@ angular.module('app.routes', [])
// Each state's controller can be found in controllers.js
$stateProvider
.state('ceitificator', {
url: '/home',
templateUrl: 'templates/ceitificator.html',
......@@ -21,6 +19,7 @@ angular.module('app.routes', [])
templateUrl: 'templates/novoEvento.html',
controller: 'novoEventoCtrl'
})
.state('listaDePresenAEventoX', {
url: '/presenca/:eventId',
templateUrl: 'templates/listaDePresenAEventoX.html',
......@@ -34,6 +33,4 @@ angular.module('app.routes', [])
$urlRouterProvider.otherwise('/home');
});
angular.module('app.services', [])
.factory('BlankFactory', [function(){
.factory('BlankFactory', [function() {
}])
.service('BlankService', [function(){
.service('BlankService', [function() {
}])
.service('EventsService', function($cordovaSQLite) {
.service('EventsService', function($cordovaSQLite, $q) {
return {
getEvents: function() {
events = [];
// pega no banco todos os eventos e retorna
var events = [];
var db = $cordovaSQLite.openDB({name: 'ceitificator.db', iosDatabaseLocation: 'default'});
$cordovaSQLite.execute(db, "SELECT * FROM events")
.then(function(res){
for(var i = 0; i < res.rows.length; i++){
.then(function(res) {
for(var i = 0; i < res.rows.length; i++) {
events.push(res.rows.item(i));
}
},
function(err){
console.log("Error");
}, function(err) {
console.log(err);
});
return events;
},
addNewEvent: function(event){
addNewEvent: function(event) {
// adiciona um novo evento no banco
var db = $cordovaSQLite.openDB({name: 'ceitificator.db', iosDatabaseLocation: 'default'});
var query = "INSERT INTO events (name, desc, date, place, hours) VALUES (?,?,?,?,?)";
$cordovaSQLite.execute(db, query, [event.eventName, event.eventDesc, event.eventDate, event.eventPlace, event.eventHours])
.then(function(res){
.then(function(res) {
console.log("res: " + res);
}, function (err){
}, function (err) {
console.error(err);
});
},
getEventById: function(eventId) {
// dado um id, retorna a palestra com o mesmo id
var dfd = $q.defer();
var db = $cordovaSQLite.openDB({name: 'ceitificator.db', iosDatabaseLocation: 'default'});
var query = "SELECT * FROM events WHERE id=\"" + eventId + "\"";
$cordovaSQLite.execute(db, query)
.then(function(res) {
console.log(res.rows);
dfd.resolve(res.rows.item(0));
}, function(err) {
console.log(err);
});
return dfd.promise
}
};
}
})
.service('PeopleService', function($cordovaSQLite) {
return {
getPeople: function() {
people = [];
getPeople: function(eventId) {
// pega no banco todas as pessoas
var people = [];
var db = $cordovaSQLite.openDB({name: 'ceitificator.db', iosDatabaseLocation: 'default'});
$cordovaSQLite.execute(db, "SELECT * FROM people")
.then(function(res){
for(var i = 0; i < res.rows.length; i++){
$cordovaSQLite.execute(db, "SELECT people.* FROM people, people_events WHERE people.id=people_events.person_id AND people_events.event_id=" + eventId)
.then(function(res) {
for(var i = 0; i < res.rows.length; i++) {
people.push(res.rows.item(i));
}
},
function(err){
console.log("Error");
}, function(err) {
console.log(err);
});
return people;
},
addNewPerson: function(person){
addPersonInEvent: function(person, eventId) {
// procura a pessoa, se existir, insere direto na tabela people_events, se não existir, cria a pessoa primeiro
var db = $cordovaSQLite.openDB({name: 'ceitificator.db', iosDatabaseLocation: 'default'});
var query = "INSERT INTO people (name, grr, email) VALUES (?,?,?)";
$cordovaSQLite.execute(db, query, [person.name, person.grr, person.email])
.then(function(res){
console.log("res: " + res);
}, function (err){
var query = "SELECT * FROM people WHERE name=\"" + person.name + "\"AND grr=\"" + person.grr + "\"AND email=\"" + person.email + "\"";
$cordovaSQLite.execute(db, query)
.then(function(res) {
if (res.rows.length == 0) {
console.log("pessoa não existe");
query = "INSERT INTO people (name, grr, email) VALUES (?,?,?)";
$cordovaSQLite.execute(db, query, [person.name, person.grr, person.email])
.then(function(res) {
console.log("id: " + res.insertId);
query = "INSERT INTO people_events (event_id, person_id) VALUES (?,?)";
$cordovaSQLite.execute(db, query, [eventId, res.insertId])
.then(function(res) {
console.log("inseriu em people_events");
}, function (err) {
console.error(err);
});
}, function (err) {
console.error(err);
});
}
else{
console.log("pessoa já existe:" + res.rows.item(0));
console.log("id: " + res.rows.item(0).id);
query = "INSERT INTO people_events (event_id, person_id) VALUES (?,?)";
$cordovaSQLite.execute(db, query, [eventId, res.rows.item(0).id])
.then(function(res) {
console.log("inseriu em people_events");
}, function (err) {
console.error(err);
});
}
}, function (err) {
console.error(err);
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment