-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_database.rb
53 lines (41 loc) · 1.53 KB
/
contact_database.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## TODO: Implement CSV reading/writing
# require_relative 'contact.rb'
require 'csv'
require 'pg'
module ContactDatabase
# Psql: heroku pg:psql --app heroku-postgres-05dfbfb3 HEROKU_POSTGRESQL_CRIMSON
CONN = PG::Connection.new(
host: 'ec2-23-23-215-150.compute-1.amazonaws.com',
user: 'wzmatxiwqinsmz',
password: 'Mf3EaZWWK3YtRXZTA3nNz49m3Z',
dbname: 'davsgajl46v1v9'
)
def self.add(contact)
CONN.exec_params('INSERT INTO contacts (fname, lname, email)
VALUES ($1, $2, $3) returning id', [contact.fname, contact.lname, contact.email])
end
def self.read
all=[]
CONN.exec_params('SELECT * FROM contacts ORDER BY id') do |rows|
rows.each do |row|
all << row
end
end
all
end
def self.delete(id)
CONN.exec_params('DELETE FROM contacts WHERE id = $1',[id])
end
def self.update(contact)
CONN.exec_params('UPDATE contacts SET fname = $1, lname = $2 ,email = $3 WHERE id = $4 returning id', [contact.fname, contact.lname, contact.email, contact.id])
end
def self.find_by_id(id)
CONN.exec_params('SELECT * FROM contacts WHERE id = $1',[id])
end
def self.search(string)
CONN.exec_params("SELECT * FROM contacts
WHERE LOWER(fname) LIKE $1
OR LOWER(lname) LIKE $1
OR LOWER(email) LIKE $1 ORDER BY id" ,["%#{string}%"])
end
end