Last active
August 29, 2015 14:24
-
-
Save szankowski/75b03c3e4c4bf2a18ae2 to your computer and use it in GitHub Desktop.
Contact List SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is a db version of contact list conecting to heroku postgress db |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'pg' | |
| class Contact | |
| attr_reader :id | |
| attr_accessor :firstname, :lastname, :email | |
| def initialize(firstname, lastname, email, id = nil) | |
| @firstname = firstname | |
| @lastname = lastname | |
| @email = email | |
| @id = id.to_i | |
| end | |
| class << self | |
| def established_connection | |
| PG.connect( | |
| host: 'ec2-54-204-26-8.compute-1.amazonaws.com', | |
| dbname: 'd2dd2cms86ppet', | |
| user: 'xbzqjuzksaogsc', | |
| password: '7Bsbw3zffaTLGpyobB_33-ZYNz' | |
| ) | |
| end | |
| def all | |
| established_connection.exec('SELECT * FROM contacts').map do |row| # For each row returned | |
| # puts "Converting row #{row}" | |
| Contact.convert_row_to_object(row) | |
| # Contact.new(row["firstname"], row["lastname"], row["email"], row["id"]) | |
| end | |
| # Returns an array of contact instances | |
| end | |
| def convert_row_to_object(field) #takes row hash from database | |
| # Row looks like : {"id"=>"2", "firstname"=>"Diana", "lastname"=>"Szankowski", "email"=>"[email protected]"} | |
| Contact.new(field["firstname"], field["lastname"], field["email"], field["id"]) | |
| end | |
| end | |
| def save | |
| conn = Contact.established_connection | |
| if(id) # If ID exists - this is an existing contact, we UPDATE | |
| puts "Updating" | |
| conn.exec_params("UPDATE contacts SET firstname = $1, lastname = $2, email = $3 WHERE id = $4", | |
| [firstname, lastname, email, id]) | |
| else # If ID does not exists - this is a new contact, we INSERT | |
| puts "Creating" | |
| result = conn.exec_params("INSERT INTO contacts (firstname, lastname, email) | |
| VALUES($1, $2, $3) RETURNING id", [firstname, lastname, email]) | |
| @id = result[0]["id"] | |
| end | |
| end | |
| def destroy | |
| conn = Contact.established_connection | |
| end | |
| def to_s | |
| "<Contact: #{firstname} #{lastname}>" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'byebug' | |
| require 'csv' | |
| require_relative 'contact' | |
| require_relative 'db' | |
| if ARGV.empty? | |
| puts "Must provide an argument" | |
| exit | |
| else | |
| arguments = ARGV | |
| end | |
| class Application | |
| def initialize | |
| @database = Db.new | |
| end | |
| def run(command) | |
| case command | |
| when "help" then puts "Available commands: | |
| new - Create a new contact | |
| list - List all contacts | |
| show - Show a contact | |
| find - Find a contact" | |
| when "new" then @database.new_contact | |
| when "list" then puts "list contacts" | |
| when "show" then puts "show contact" | |
| when "find" then puts "find contact" | |
| else | |
| puts "Invalid Comand" | |
| end | |
| end | |
| end | |
| app = Application.new | |
| app.run(arguments[0]) | |
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 3 columns, instead of 2 in line 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| chrystian, szankowski, 12312312 | |
| diana, szankowski |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Db | |
| attr_accessor :contacs, :user_input | |
| def initialize | |
| @contacts = [] | |
| @user_input = user_input | |
| @loaded_contacts = CSV.read('contacts.csv') | |
| @loaded_contacts.each do |contact| | |
| @contacts << Contact.new(contact[0],contact[1]) | |
| end | |
| # [ Contact Object 1, Contact Object 2, Contac...] | |
| end | |
| # def new_contact | |
| # input_command | |
| # end | |
| def new_contact #input_command | |
| @user_input = $stdin.gets.chomp | |
| p @user_input | |
| end | |
| end | |
| # contacts = CSV.read('contacts.csv') | |
| # puts contacts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'contact' | |
| puts 'Connecting to the database...' | |
| # conn = PG.connect( | |
| # host: 'ec2-54-204-26-8.compute-1.amazonaws.com', | |
| # dbname: 'd2dd2cms86ppet', | |
| # user: 'xbzqjuzksaogsc', | |
| # password: '7Bsbw3zffaTLGpyobB_33-ZYNz' | |
| # ) | |
| # puts 'Finding contacts...' | |
| # conn.exec('SELECT * FROM contacts;') do |results| | |
| # # results is a collection (array) of records (hashes) | |
| # results.each do |contact| | |
| # puts contact.inspect | |
| # end | |
| # end | |
| # contact = Contact.new("Vasili", "Sviridov", "[email protected]") | |
| # puts contact.inspect | |
| # contact.save | |
| # contact.save | |
| # puts contact.inspect | |
| # contacts = Contact.all | |
| # puts contacts | |
| contacts = Contact.all | |
| contact = contacts.last | |
| puts contact.id | |
| puts 'DONE' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'pg' | |
| puts 'Connecting to the database...' | |
| conn = PG.connect( | |
| host: 'ec2-54-204-26-8.compute-1.amazonaws.com', | |
| dbname: 'd2dd2cms86ppet', | |
| user: 'xbzqjuzksaogsc', | |
| password: '7Bsbw3zffaTLGpyobB_33-ZYNz' | |
| ) | |
| puts 'Finding contacts...' | |
| conn.exec('SELECT * FROM contacts;') do |results| | |
| # results is a collection (array) of records (hashes) | |
| results.each do |contact| | |
| puts contact.inspect | |
| end | |
| end | |
| puts 'Closing the connection...' | |
| conn.close | |
| puts 'DONE' | |
| class Contact | |
| attr_accessor :firstname, :lastname, :email, :id | |
| def initialize(firstname, lastname, email, id = nil) | |
| @firstname = firstname | |
| @lastname = lastname | |
| @email = email | |
| end | |
| # def connection_established | |
| # end | |
| def save | |
| conn = PG.connect( | |
| host: 'ec2-54-204-26-8.compute-1.amazonaws.com', | |
| dbname: 'd2dd2cms86ppet', | |
| user: 'xbzqjuzksaogsc', | |
| password: '7Bsbw3zffaTLGpyobB_33-ZYNz' | |
| ) | |
| conn.exec("INSERT INTO contacts (firstname, lastname, email) | |
| VALUES('#{firstname}', '#{lastname}', '#{email}');") | |
| end | |
| end | |
| contact = Contact.new("Miles", 'Davis', '[email protected]') | |
| contact.save | |
| puts 'After inserting...' | |
| puts contact | |
| # conn.exec('SELECT * FROM contacts;') do |results| | |
| # # results is a collection (array) of records (hashes) | |
| # results.each do |contact| | |
| # puts contact.inspect | |
| # end | |
| # end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE contacts ( | |
| id SERIAL PRIMARY KEY, | |
| firstname VARCHAR(100) NOT NULL, | |
| lastname VARCHAR(100), | |
| email VARCHAR(256) | |
| ); | |
| INSERT INTO contacts (firstname, lastname, email) | |
| VALUES('Chrystian', 'M. Szankowski', '[email protected]'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment