Skip to content

Instantly share code, notes, and snippets.

@MrJadaml
Created August 2, 2016 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrJadaml/e3303c056ef402639eb7f49887074f8f to your computer and use it in GitHub Desktop.
Save MrJadaml/e3303c056ef402639eb7f49887074f8f to your computer and use it in GitHub Desktop.
(function() {
'use strict';
const server = 'https://galvanize-todos.herokuapp.com/is-persons';
const app = angular.module('todoApp');
app.controller('PeopleCtrl', PeopleCtrl);
app.controller('PersonCtrl', PersonCtrl);
app.controller('TodoListCtrl', TodoListCtrl);
PeopleCtrl.$inject = ['people'];
PersonCtrl.$inject = ['$routeParams', 'people'];
TodoListCtrl.$inject = ['personTodos'];
function TodoListCtrl(personTodos) {
this.todoToAdd = '';
this.todos = [];
this.addTodo = (person) => {
personTodos.addTodo(person, this.todoToAdd)
.then((todo) => {
person.todos.push(todo);
this.todoToAdd = '';
})
.catch((err) => {
throw err;
});
};
}
function PeopleCtrl(peopleSvc) {
this.nameToAdd = '';
this.people = [];
this.addPerson = () => {
peopleSvc.addPerson(this.nameToAdd).then((person) => {
this.people.push(person);
this.nameToAdd = '';
})
.catch((err) => {
throw err;
});
};
const activate = () => {
peopleSvc.getAll().then((peopleList) => {
this.people = peopleList;
})
.catch((err) => {
throw err;
})
};
activate();
}
function PersonCtrl($routeParams, peopleSvc) {
this.person = {};
const { id } = $routeParams;
const activate = () => {
peopleSvc.getPerson(id).then((person) => {
this.person = person;
})
.catch((err) => {
throw err;
});
};
activate();
}
}());
(function() {
'use strict';
const app = angular.module('todoApp');
const server = 'https://galvanize-todos.herokuapp.com/is-persons';
app.factory('people', people);
people.$inject = ['$http', '$q'];
function people($http, $q) {
return {
getAll: function() {
return $http.get(server).then((res) => {
return $q.all(res.data.map((person) => {
return $http.get(`${server}/${person.id}/is-todos`)
.then((todos) => {
person.todos = todos.data;
return person;
})
.catch((err) => {
throw err;
});
}));
})
.catch((err) => {
throw err;
});
},
addPerson: (name) => {
return $http.post(server, { name })
.then((response) => {
const person = response.data;
person.todos = [];
return person;
})
.catch((err) => {
console.error(err);
});
},
getPerson: (id) => {
let person;
return $http.get(`${server}/${id}`)
.then((res) => {
person = res.data;
return $http.get(`${server}/${id}/is-todos`);
})
.then((res) => {
person.todos = res.data;
return person;
})
.catch((err) => {
throw err;
});
}
}
}
app.factory('personTodos', personTodos);
personTodos.$inject = ['$http'];
function personTodos($http) {
return {
addTodo: (person, todoText) => {
return $http.post(`${server}/${person.id}/is-todos`, {
completed: false,
text: todoText
})
.then((res) => {
return res.data;
})
.catch((err) => {
throw err;
});
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment