angularJS refactoring code into service