Last active
May 13, 2016 00:21
-
-
Save garrettgsb/fb3ab95056e928fde456166ed63b566b to your computer and use it in GitHub Desktop.
Contact Manager
This file contains 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' | |
require 'active_record' | |
require_relative 'db' | |
# Represents a person in an address book. | |
# The ContactList class will work with Contact objects instead of interacting with the CSV file directly | |
class Contact < ActiveRecord::Base | |
# @@conn = PG.connect({ | |
# host: 'localhost', | |
# dbname: 'contact_manager', | |
# user: 'development', | |
# password: 'development' | |
# }) | |
# attr_reader :id | |
# attr_accessor :name, :email | |
# | |
# def initialize(name, email, id=nil) | |
# #get_smallest_unused_ID | |
# @name = clean_if_empty_input(name) | |
# @email = clean_if_empty_input(email) | |
# @id = id | |
# puts "New contact created:\nName: #{@name}\nEmail: #{@email}" | |
# #@contact_list.add_row(@firstname, surname: @surname, email: @email, id: 100}) | |
# #@contact_list << "#{@firstname},#{@surname},#{@email},100" | |
# #@contact_list.add_row(["#{@firstname}","#{@surname}", "#{@email}", "100"]) | |
# end | |
# | |
# | |
# ## NEW METHOD | |
# def self.create_from_hash(contact_hash) | |
# # [SQL results][0] | |
# Contact.new(contact_hash["name"],contact_hash["email"]) | |
# end | |
# | |
# def self.all | |
# ##TODO: Make this print something prettier | |
# ## Totally works fine right now though. | |
# array =[] | |
# @@conn.exec("SELECT * FROM contacts").each do |x| | |
# puts x | |
# # array << Contact.new(x.name, x.email) | |
# end #comm loop | |
# end #all | |
# | |
# def self.create(name, email) | |
# newGuy = Contact.new(name, email) | |
# save(newGuy) | |
# puts "New Contact Created: #{newGuy.name}, #{newGuy.email}, #{newGuy.id}" | |
# end #create method | |
# | |
# def self.update(id) | |
# contact = @@conn.exec("SELECT * FROM contacts WHERE id=$1", [id])[0] | |
# puts "Enter new name (Or don't type anything for no change)" | |
# new_name = STDIN.gets.chomp | |
# new_name = contact["name"] if new_name == "" | |
# puts "Enter new email address (Or don't type anything for no change)" | |
# new_email = STDIN.gets.chomp | |
# new_email = contact["email"] if new_email == "" | |
# | |
# ## TODO: Refactor --v | |
# # contact_object = Contact.new(contact[0]["name"], contact[0]["email"]) | |
# # contact_object.name = new_name | |
# | |
# @@conn.exec_params("UPDATE contacts SET name= $1, email= $2 WHERE id=$3;", [new_name, new_email, id]) | |
# end #update | |
# | |
# # Invoked by Create and Update to push value to database | |
# def self.save(contact) | |
# #binding.pry | |
# if contact.id == nil | |
# @@conn.exec_params("INSERT INTO contacts (name, email) VALUES ($1, $2)", [contact.name, contact.email]) | |
# else | |
# @@conn.exec_params("UPDATE contacts SET name = $1, email $2", [contact.name, contact.email]) | |
# end #if/else | |
# end #save | |
# | |
# ## Takes ID as a parameter, finds that contact. | |
# def self.find(id) | |
# contact = @@conn.exec_params("SELECT * FROM contacts WHERE id = $1::int;", [id])[0] | |
# puts contact["name"] + " " + contact["email"] | |
# end #find | |
# | |
# def self.search(query) | |
# search = @@conn.exec_params("SELECT * FROM contacts WHERE name LIKE $1;",[query]) | |
# search.each do |x| | |
# puts x["name"] | |
# puts x["email"] | |
# puts x["id"] | |
# end #loop | |
# end #search | |
# | |
# private | |
# | |
# def clean_if_empty_input(string) | |
# if string == "" | |
# string = "Not provided." | |
# end #if | |
# end #clean_if_empty_input | |
end #Contact class |
This file contains 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
firstname | surname | id | ||
---|---|---|---|---|
Bruce | Wayne | batman@wayne-enterprises.com | 1 | |
Peter | Parker | spiderman@thedailybugle.com | 2 | |
Clark | Kent | supes@fortressofsolitude.ru | 3 |
This file contains 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
firstname | surname | id | ||
---|---|---|---|---|
Bruce | Wayne | batman@wayne-enterprises.com | 1 | |
Peter | Parker | spiderman@thedailybugle.com | 2 | |
Clark | Kent | supes@fortressofsolitude.ru | 3 | |
Mickey | Mouse | mouse@email.com | 1212 | |
Donald | Duck | duck@email.com | 1212 | |
qwer | asdf | zxcv | 111 | |
Dr. | Who | thedoctor@email.com | 111 | |
Matt | Attfield | matt@field.com | 111 | |
Stephen | Harper | steve@vancats.com | 111 | |
Justin | Trudeau | justin@trudeau.com | 111 | |
asdfa | werq | zcxv | 111 | |
axcvas | werqt | dfhs | 111 | |
dnfgd | rewgf | vbf | 111 | |
fghjklnm | adfgsdfad | sdfrt | 111 | |
ahfaksjd | asjdfahskjdhf | askdjhfaskj | 1212 | |
zcvkdnklj | wkejrhakje | xkjcvakj | 1212 | |
dgbsdfbg | awerawer | xvbsdf | 1212 | |
xcvsdfgs | sdghsdg | sdfgdrg | 1213 | |
sdfbsfn | sdfgsdfg | ertesg | 1214 |
This file contains 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' | |
require_relative 'db' | |
require 'pry' | |
require 'active_record' | |
# Interfaces between a user and their contact list. Reads from and writes to standard I/O. | |
class ContactList | |
# def initialize(filename) | |
# # I dunno. | |
# end #initialize | |
def initialize(argv) | |
args = argv | |
puts " | |
Here is a list of available commands: | |
create - Create a new contact | |
all - List all contacts | |
find - Find contact by ID | |
search - Find contact(s) by keyword | |
update - Edit a contact name/email | |
\n" | |
case args[0] | |
when 'all' | |
Contact.all | |
when 'create' | |
puts "Name?" | |
name = STDIN.gets.chomp | |
puts "Email?" | |
email = STDIN.gets.chomp | |
Contact.create(name: name, email: email) | |
when 'find' | |
puts "What ID do you want to find?" | |
id = STDIN.gets.chomp | |
Contact.find(id) | |
when 'search' | |
puts "What name do you want to search for?" | |
query = STDIN.gets.chomp | |
Contact.search(query) | |
#TODO: Write this function. | |
puts "Search... Working on it." | |
when 'update' | |
puts "ID?" | |
id = STDIN.gets.chomp | |
Contact.update(id) | |
when 'destroy' | |
#TODO: Write this function | |
puts "Destroy... Working on it." | |
else | |
puts "lolwut" | |
end #case | |
end # initialize | |
end #ContactList class | |
# Main | |
list = ContactList.new(ARGV) |
This file contains 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 "active_record" | |
CONN = ActiveRecord::Base.establish_connection( | |
adapter: 'postgresql', | |
database: 'contact_manager', | |
username: 'development', | |
password: 'development', | |
host: 'localhost', | |
port: 5432, | |
pool: 5, | |
encoding: 'unicode', | |
min_messages: 'error' | |
) | |
ActiveRecord::Schema.define do | |
unless ActiveRecord::Base.connection.table_exists?(:contact_manager) | |
create_table :contact_manager do |x| | |
x.column :name, :string | |
x.column :email, :string | |
end #create_table | |
end #unless | |
end #Schema |
This file contains 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 'pry-nav' | |
require 'pg' | |
conn = PG.connect({ | |
host: 'localhost', | |
dbname: 'contact_manager', | |
user: 'development', | |
password: 'development' | |
}) | |
puts conn | |
selection = conn.exec("SELECT * FROM contacts;") | |
binding.pry | |
puts "End" |
This file contains 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 NOT NULL PRIMARY KEY, | |
name varchar(60), | |
email varchar(60) | |
); | |
INSERT INTO contacts(name, email) VALUES ('Soheil', 'soheil@sametableasme.com'); | |
INSERT INTO contacts(name, email) VALUES ('Ryan', 'ryan@sametableasme.com'); | |
INSERT INTO contacts(name, email) VALUES ('Sterling', 'sterling@sametableasme.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment