Skip to content
This repository has been archived by the owner on Aug 8, 2018. It is now read-only.

WIP: hit backend api #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/appInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ function appInit($rootScope, $state) {

// Proper Regex Pattern for email input form validation
$rootScope.emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

// $rootScope.swaggerSpec = 'http://127.0.0.1:8000/docs/api-docs/'; // for local backend testing

}

appInit.$inject = ['$rootScope', '$state'];
Expand Down
41 changes: 7 additions & 34 deletions app/components/appMainModule/committeePageModule/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,9 @@ module.exports = function($stateProvider) {
template: '<ui-view></ui-view>',
controller: 'committeePageController',
resolve: {
committee: function($stateParams, $q) {
return $q.resolve({
committee_id: 1234,
name: 'Americans for Liberty',
contribution_by_type: {
unitemized: 2916394,
self_funded: 512554,
political_party: 6426112,
individual: 11134547,
recipient_committee: 986229
},
contribution_by_area: {
inside_location: 0.56,
inside_state: 0.38,
outside_state: 0.06
}
committee: function($stateParams, disclosureApi) {
return disclosureApi.committee.get({
committee_id: $stateProvider.committee_id
});
}
},
Expand Down Expand Up @@ -62,24 +49,10 @@ module.exports = function($stateProvider) {
parent: 'appMain.committee.main'
},
resolve: {
contributors: function($stateParams, $q) {
return $q.resolve([
{
name: 'Samantha Brooks',
amount: 700,
date: new Date('2015-04-12')
},
{
name: 'Lisa Sheppards',
amount: 700,
date: new Date('2015-01-13')
},
{
name: 'Raoul Esponsito',
amount: 700,
date: new Date('2015-04-04')
}
]);
contributors: function($stateParams, disclosureApi) {
return disclosureApi.committee.contributors({
committee_id: $stateProvider.committee_id
});
}
},
data: {
Expand Down
51 changes: 8 additions & 43 deletions app/components/appMainModule/localityPageModule/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ module.exports = function($stateProvider) {
template: '<locality-listing locality="locality"></locality-listing>',
resolve: {
locality: function($stateParams, disclosureApi) {
return disclosureApi.locations.get({locality_id: $stateParams.locality_id});
return disclosureApi.locality.current_ballot({
locality_id: $stateParams.locality_id
}).then(function(ballot) {
disclosureApi.ballot.summary({ballot_id: ballot.ballot_id});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adborden This is more complex than before; I like the way this API is defined, but not sure if this is the right way to chain API calls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but your hunch is right. angular ui-router cleans this up for us:

resolve: {
  locality: function($stateParams, disclosureApi) {
    return disclosureApi.locations.get({locality_id: $stateParams.locality_id});
  },
  ballot: function($stateParams, disclosureApi) {
    return disclosureApi.locality.current_ballot({locality_id: $stateParams.locality_id });
  },
  ballotSummary: function(disclosureApi, ballot) {
   return disclosureApi.ballot.summary({ballot_id: ballot.ballot_id});
  }
}

}
},
data: {
Expand Down Expand Up @@ -53,48 +57,9 @@ module.exports = function($stateProvider) {
parent: 'appMain.locality'
},
resolve: {
ballot: function($q) {
return $q.resolve({
ballot_id: 'ballot1',
locality_id: 'locality2',
contests: [
{
contest_type: 'office',
name: 'Mayor'
},
{
contest_type: 'office',
name: 'City Auditor'
},
{
contest_type: 'office',
name: 'City Treasurer'
},
{
contest_type: 'office',
name: 'Distrit 1 City Council'
},
{
contest_type: 'office',
name: 'Distrit 3 City Council'
},
{
contest_type: 'office',
name: 'Distrit 5 City Council'
},
{
contest_type: 'referendum',
name: 'Measure AA'
},
{
contest_type: 'referendum',
name: 'Measure BB'
},
{
contest_type: 'referendum',
name: 'Measure CC'
}
]
ballot: function($stateParams, disclosureApi) {
return disclosureApi.locality.current_ballot({
locality_id: $stateParams.locality_id
});
}
},
Expand Down
45 changes: 15 additions & 30 deletions app/components/appMainModule/measurePageModule/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,17 @@

module.exports = function($stateProvider) {
$stateProvider

.state({
name: 'appMain.measure',
url: '^/measure/:measure_id',
abstract: true,
controller: 'measurePageController',
template: '<ui-view></ui-view>',
resolve: {
measure: function($stateParams, $q) {
return $q.resolve({
measure_id: 1,
city: {
fips_id: 6075,
location: {
name: 'San Francisco'
}
}, // Not sure if city really makes sense here
number: 'BB',
full_text: 'Shall the Charter of the City of Oakland be amended to provide the Public Ethics Commission greater independence, broader enforcement authority, powers and…',
title: 'Ethics Commission Authority Increase Charter Amendment',
supporting_count: 4,
opposing_count: 6
measure: function($stateParams, disclosureApi) {
return disclosureApi.referendum.get({
referendum_id: $stateParams.measure_id
});
}
},
Expand All @@ -40,6 +30,7 @@ module.exports = function($stateProvider) {
pageDescription: 'Ballot measures.'
}
})

.state({
name: 'appMain.measure.index',
url: '',
Expand All @@ -55,6 +46,7 @@ module.exports = function($stateProvider) {
pageDescription: 'Ballot measures.'
}
})

.state({
name: 'appMain.measure.supporting',
url: '/supporting',
Expand All @@ -65,13 +57,10 @@ module.exports = function($stateProvider) {
parent: 'appMain.measure.index'
},
resolve: {
supporters: function($stateParams, $q) {
return $q.resolve([
{name: 'Citizens for a Better Oakland', contributions: 185859},
{name: 'Oaklanders for Ethical Government', contributions: 152330},
{name: 'Americans for Liberty', contributions: 83199},
{name: 'Golden State Citizens for Positive Reform', contributions: 23988}
]);
supporters: function($stateParams, disclosureApi) {
return disclosureApi.referendum.supporting({
referendum_id: $stateParams.measure_id
});
}
},
data: {
Expand All @@ -81,6 +70,7 @@ module.exports = function($stateProvider) {
pageDescription: 'Supporters of the ballot measure.'
}
})

.state({
name: 'appMain.measure.opposing',
url: '/opposing',
Expand All @@ -91,15 +81,10 @@ module.exports = function($stateProvider) {
parent: 'appMain.measure.index'
},
resolve: {
opposers: function($stateParams, $q) {
return $q.resolve([
{name: 'Citizens for a Better Oakland', contributions: 185859},
{name: 'Oaklanders for Ethical Government', contributions: 152330},
{name: 'Americans for Liberty', contributions: 83199},
{name: 'Golden State Citizens for Positive Reform', contributions: 23988},
{name: 'The Public Commission for Ethical Civic Reform', contributions: 15040},
{name: 'The Committee of True Americans who Dearly Love America and Liberty', contributions: 7943}
]);
opposers: function($stateParams, disclosureApi) {
return disclosureApi.referendum.opposing({
referendum_id: $stateParams.measure_id
});
}
},
data: {
Expand Down
2 changes: 1 addition & 1 deletion app/components/homePageModule/HomePageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function HomePageController($scope, $http, disclosureApi) {
return;
}

disclosureApi.search.get({q: $scope.search})
disclosureApi.locality.search({q: $scope.search})
.then(function(results) {
$scope.searchResults = results;
});
Expand Down
81 changes: 63 additions & 18 deletions app/components/services/disclosure/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,76 @@ describe('disclosureApi', function() {
disclosureApi = _disclosureApi_;
}));

it('exists', function() {
expect(disclosureApi).to.be.ok;
});

it('has contributions', function() {
expect(disclosureApi).to.have.property('contributions');
});

it('has elections', function() {
expect(disclosureApi).to.have.property('elections');
// locality.search
it('get a locality', function() {
// Search for any locality
disclosureApi.locality.search({q: ''})
.then(function(localities) {
localities.each(function(locality) {
expect(locality).to.have.property('name');
expect(locality).to.have.property('type');
expect(locality).to.have.property('id');
});
});
});

it('has locations', function() {
expect(disclosureApi).to.have.property('locations');
// locality.current_ballot
it('get a ballot', function() {
// Search for any locality
disclosureApi.locality.search({q: ''})
.then(function(localities) {
// Get the current ballot for the first locality
var locality = localities[0];
disclosureApi.locality.current_ballot({locality_id: locality.id})
.then(function(ballot) {
expect(ballot).to.have.property('locality_id');
expect(ballot).to.have.property('ballot_id');
expect(ballot).to.have.property('ballot_items');
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use a known-good locality id. Otherwise it's testing too much IMHO.

});

it('has search', function() {
expect(disclosureApi).to.have.property('search');
// office_election.get
it('get an office election', function() {
// Search for any locality
disclosureApi.locality.search({q: ''})
.then(function(localities) {
// Get the current ballot for the first locality
var locality = localities[0];
disclosureApi.locality.current_ballot({locality_id: locality.id})
.then(function(ballot) {
// Get the office election details from the ballot
disclosureApi.office_election.get({ballot_id: ballot.ballot_id})
.then(function(officeElection) {
expect(officeElection).to.have.property('candidates');
});
});
});
});

it('lists some contributions', function() {
// This hits a live API, so changes outside of this project could fail this test :(
return disclosureApi.contributions.list()
.then(function(contributions) {
expect(contributions[0]).to.have.property('amount');
// candidate.get
it('get a candidate', function() {
// Search for any locality
disclosureApi.locality.search({q: ''})
.then(function(localities) {
// Get the current ballot for the first locality
var locality = localities[0];
disclosureApi.locality.current_ballot({locality_id: locality.id})
.then(function(ballot) {
// Get the office election details from the ballot
disclosureApi.office_election.get({ballot_id: ballot.ballot_id})
.then(function(officeElection) {
// Get details from the first candidate.
var candidate = officeElection.candidates[0];
disclosureApi.candidate.get({candidate_id: candidate.id})
.then(function(candidate) {
expect(candidate).to.have.property('last_name');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adborden The API is more granular, so it is necessary to chain API calls to get needed info. I was able to validate the first two tests, but after a while, things didn't seem to be getting through to the backend, and tests seemed to pass regardless of what was inside.

Please take a look.

});
});
});
});
});


});
41 changes: 36 additions & 5 deletions app/components/services/disclosure/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,42 @@ DisclosureService.prototype = {
};
}

setupProxy('contributions', 'list');
setupProxy('contributions', 'retrieve');
setupProxy('elections', 'list');
setupProxy('locations', 'get');
setupProxy('search', 'get');
//////////////////////////////
// Non-disclosure info
//////////////////////////////

// Ballot
setupProxy('ballot', 'get'); // List of ballot items.
setupProxy('candidate', 'get'); // Candidate details
setupProxy('elections', 'list'); // List all elections
setupProxy('office_election', 'get'); // List of candidates
setupProxy('referendum', 'get'); // Referendum details

// Locality
setupProxy('locality', 'get'); // Locality details (e.g. name)


// Non-disclosure information, filtered by presence of disclosure data
setupProxy('locality', 'current_ballot'); // Show current ballot
setupProxy('locality', 'search');

//////////////////////////////
// Disclosure info
//////////////////////////////

// Basic info
setupProxy('committee', 'get'); // Committee details

// Money info
setupProxy('ballot', 'disclosure_summary'); // overall disclosure summary
setupProxy('candidate', 'supporting');
setupProxy('candidate', 'opposing');
setupProxy('committee', 'contributors'); // list of contributors
setupProxy('committee', 'contributions'); // list of contributions made to other campaigns
setupProxy('committee', 'summary'); // summary over contributors
setupProxy('referendum', 'supporting');
setupProxy('referendum', 'opposing');

},

// Proxies the swagger function in an angular promise
Expand Down