-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
56 lines (44 loc) · 1.24 KB
/
server.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
52
53
54
55
56
require "sinatra"
require "pg"
require "pry" if development? || test?
require "sinatra/reloader" if development?
require_relative "./app/models/user"
set :bind, '0.0.0.0' # bind to all interfaces
set :views, File.join(File.dirname(__FILE__), "app", "views")
configure :development do
set :db_config, { dbname: "launcher_users" }
end
# points our app at a development version of "users" database when tests are run
configure :test do
set :db_config, { dbname: "launcher_users" }
end
# points our app at a test version of "users" database when tests are run
def db_connection
begin
connection = PG.connect(Sinatra::Application.db_config)
# connection = PG.connect("users")
yield(connection)
ensure
connection.close
end
end
# this is the method that we will use to executre queries agaisnt our DB
get "/users" do
sql_users = nil
db_connection do |connection_object|
sql_users = connection_object.exec("SELECT * FROM users;")
end
@users = sql_users.to_a
# @users = User.all
erb :index
end
# get "/users/:id" do
# user = nil
# db_connection do |conn|
# user = conn.exec_params(
# "SELECT * FROM users WHERE users (id) VALUES ($1)",
# [params["id"]]
# )
# end
# binding.pry
# end