var app = angular.module( "Temp", [])
    .controller("TempController", function( $scope, tempService) {

            $scope.tempData = [];

            loadRemoteData();

            function applyRemoteData(newData)
            {
                $scope.tempData = newData;
            }

            function loadRemoteData() {
                console.log('Loading data...');
                tempService.getTemp()
                    .then(
                        function(tempData) {
                            applyRemoteData(tempData);
                        }
                    )
            }
        }
    ).service(
        "tempService",
        function($http, $q) {
            return({getTemp:getTemp});

            function getTemp() {
                var request = $http({
                    method: "get",
                    url:"http://api.silvrtree.co.uk/temp/all",
                    params: {
                        /* action:"get"*/
                    }
                });
                return (request.then(handleSuccess, handleError));
            }


            function handleError(response) {
                if ( !angular.isObject(response.data) || !response.data.message) {
                    return( $q.reject("An unknown error occured"));
                }

                return($q.reject(response.data.message));
            }

            function handleSuccess(response)
            {
                return(response.data);
            }

        }
    );