From 0601e27697c0436f648e33087cba42e98256e400 Mon Sep 17 00:00:00 2001 From: fvieira Date: Sat, 4 Mar 2017 14:13:31 +0100 Subject: [PATCH] preliminar validation done --- backend/app/people/inquisitor-validator.js | 3 ++- backend/app/people/people.js | 8 ++++---- frontend/app/core/people.js | 10 ++++++++-- frontend/app/core/peopleService.js | 16 ++++++++-------- frontend/app/error-partial.html | 9 +++++++++ 5 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 frontend/app/error-partial.html diff --git a/backend/app/people/inquisitor-validator.js b/backend/app/people/inquisitor-validator.js index d6fcf64..0a9937c 100644 --- a/backend/app/people/inquisitor-validator.js +++ b/backend/app/people/inquisitor-validator.js @@ -6,7 +6,7 @@ var areBadPetsHere = function (pets) { return false } var m = pets.map(function (pet) { - return pet == '' + return pet == '' || pet == undefined }) return m.reduce(function (i,j) { return i || j @@ -14,6 +14,7 @@ var areBadPetsHere = function (pets) { } var inquisitor = function (next) { + console.log(this) if(this.name == ''){ next(erroneous_name) } diff --git a/backend/app/people/people.js b/backend/app/people/people.js index 1aefc2c..4c918ea 100644 --- a/backend/app/people/people.js +++ b/backend/app/people/people.js @@ -54,10 +54,10 @@ db.once('open',function () { } people.modifyPet = function (id, old_pet_name, new_pet_name, callback) { - people.removePet(id,old_pet_name,function () { - people.addPet(id,new_pet_name,function () { - callback() - }) + people.model.findById(id).then(function (person) { + person.pets.splice(person.pets.indexOf(old_pet_name),1) + person.pets.push(new_pet_name) + person.save(callback) }) } diff --git a/frontend/app/core/people.js b/frontend/app/core/people.js index d170256..319fc18 100644 --- a/frontend/app/core/people.js +++ b/frontend/app/core/people.js @@ -8,6 +8,12 @@ app.controller('peopleCtrl',function ($scope,ngDialog,people) { }) } + $scope.badData = function () { + ngDialog.open({ + template:'../error-partial.html' + }) + } + this.load() $scope.remove = function (item) { @@ -16,7 +22,7 @@ app.controller('peopleCtrl',function ($scope,ngDialog,people) { $scope.add = function () { console.log("adding") - people.add($scope.name,self.load) + people.add($scope.name,self.load,$scope.badData) $scope.name = '' } @@ -34,7 +40,7 @@ app.controller('peopleCtrl',function ($scope,ngDialog,people) { people.addPet(person, pet, function () { self.load() $scope.pet = '' - }) + }, $scope.badData) } $scope.do = function (person,pet) { diff --git a/frontend/app/core/peopleService.js b/frontend/app/core/peopleService.js index 5447619..fbb582a 100644 --- a/frontend/app/core/peopleService.js +++ b/frontend/app/core/peopleService.js @@ -7,18 +7,18 @@ app.service('people', function ($http) { $http.delete('http://127.0.0.1:8000/people/'+item._id).then(callback) } - this.add = function (n,callback) { - $http.post('http://127.0.0.1:8000/people',{name:n}).then(callback) + this.add = function (n,callback,reject) { + $http.post('http://127.0.0.1:8000/people',{name:n}).then(callback).catch(reject) } - this.modify = function (item,callback) { + this.modify = function (item,callback,reject) { $http.put('http://127.0.0.1:8000/people',{person_id: item._id, name: item.name}) - .then(callback) + .then(callback).catch(reject) } - this.addPet = function (item,pet_name,callback) { + this.addPet = function (item,pet_name,callback,reject) { $http.post('http://127.0.0.1:8000/people/pets',{person_id: item._id, petname: pet_name}) - .then(callback) + .then(callback).catch(reject) } this.removePet = function (item, pet_name, callback) { @@ -26,12 +26,12 @@ app.service('people', function ($http) { .then(callback) } - this.modifyPet = function (item,old_pet_name,new_petname,callback) { + this.modifyPet = function (item,old_pet_name,new_petname,callback,reject) { $http.put('http://127.0.0.1:8000/people/pets', { person_id: item._id, old_petname: old_pet_name, new_petname: new_petname - }).then(callback) + }).then(callback).catch(reject) } }) diff --git a/frontend/app/error-partial.html b/frontend/app/error-partial.html new file mode 100644 index 0000000..7194fcb --- /dev/null +++ b/frontend/app/error-partial.html @@ -0,0 +1,9 @@ +
+

Error

+ Erroneous data. +
+
+ +
+
+
-- 2.18.1