Skip to content

kqarlos/election-year

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Election Year


Languages Top Language Code Size Repo Size Total Lines MySQL2 Version Express Version Express-Handlebars Version Chart.js Version Sequelize Version Last Commit Issues Followers

Description

Election Year informs users about their congressional representatives and their campaign finance donation history.

Table of Contents

Installation

Live Site

Usage

  1. Search Functionality

Site

  1. Profile Demo

Site

  1. Database population

Site

Code Snippets

  1. Populating the database
        var options = {
            method: 'GET',
            headers: { 'X-API-Key': congressAPIKey },
            url: url
        };
        axios(options).then(function (response) {
            var members = response.data.results[0].members;

            for (let i = 0; i < members.length; i++) {
                db.Senator.create({
                    name: members[i].first_name + " " + members[i].last_name,
                    state: members[i].state,
                    fecId: members[i].fec_candidate_id,
                    memberId: members[i].id,
                    party: members[i].party,
                    gender: members[i].gender,
                    crpid: members[i].crp_id
                }).then(function (r) {
                    db.VotingRecord.create({
                        missed_pct: members[i].missed_votes_pct,
                        votesWParty_pct: members[i].votes_with_party_pct,
                        votesWOParty_pct: members[i].votes_against_party_pct,
                        SenatorId: r.dataValues.id
                    });
                });
            }
        });

    
  • This block of code took care of populating both the Seantors and Voting Record tables in our database. We used axios to make the API request to Pro Publica API. The URL asked for all senators. This gave us back a list that we iterated through to create objects to send to our data models. We took advantage of the callback functions to go from the query response, to inserting a senator to our Senator table, and into creating a voting record for that specific senator. This avoided conflicts since each function was working on return data from the function before it.
  1. User model
module.exports = function (sequelize, DataTypes) {
    var User = sequelize.define("User", {
        name: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: 3,
                    msg: "Name must be atleast 3 characters in length"
                }
            }
        },
        location: {
            type: DataTypes.STRING
        }
    });

    User.associate = models => {
        models.User.hasOne(models.Login, {
            onDelete: "cascade"
        })
    }
    return User;
}
  • This is our definition for the user model. We had a simple model with a name and a location. Both of these fields had some validations that check the data before getting inserted into the table. Before the end of our model definition we inserted an association to the Login table. This one-to-one relationship will ensure that a login object can reference a unique User object.
  1. Handlebars
<div class="jumbotron">

  <h1 class="display-4" data-id={{id}} data-location={{location}} id="info">Welcome {{name}}</h1>
  <p class="lead">Your Representatives From {{location}}!</p>


  <hr class="my-4">
  <div class="container" id="manip">
    <div class="senators">
      <h3>Senators</h3>
      <table id="senators">
      </table>
    </div>
    <div class="house">
      <h3>House of Representatives</h3>
      <table id="reps">

      </table>
    </div>


  </div>

</div>
  • Handlebars makes HTML more flexible by giving us the ability to work with objects. This handlebar takes care of displaying the user data passed on from the server-side. Not only does it display the logged-in user's name and location, it also hides user data such as user id inside the HTML. This allows for the user to feel a more personalized experience and for the developer to have easy access to commonly needed user data.

Credits

Authors

Built With


HMTL CSS Javascript Bootstrap Node Express MySQL Handlebars

License


MIT license

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 75.2%
  • Handlebars 23.0%
  • CSS 1.8%