|
|
|
@ -1,17 +1,6 @@
|
|
|
|
class Location
|
|
|
|
class Location
|
|
|
|
attr_reader :id, :street, :city, :state, :inhabitants
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
|
|
|
|
DB = PG.connect({:host => "localhost", :port => 5432, :dbname => 'contacts'})
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
|
|
|
|
@id = opts["id"].to_i
|
|
|
|
|
|
|
|
@street = opts["street"]
|
|
|
|
|
|
|
|
@city = opts["city"]
|
|
|
|
|
|
|
|
@state = opts["state"]
|
|
|
|
|
|
|
|
if opts["inhabitants"]
|
|
|
|
|
|
|
|
@inhabitants = opts["inhabitants"]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
def self.all
|
|
|
|
results = DB.exec(
|
|
|
|
results = DB.exec(
|
|
|
|
@ -31,13 +20,13 @@ class Location
|
|
|
|
results.each do |result|
|
|
|
|
results.each do |result|
|
|
|
|
if result["id"] != last_location_id
|
|
|
|
if result["id"] != last_location_id
|
|
|
|
locations.push(
|
|
|
|
locations.push(
|
|
|
|
Location.new({
|
|
|
|
{
|
|
|
|
"id" => result["id"],
|
|
|
|
"id" => result["id"].to_i,
|
|
|
|
"street" => result["street"],
|
|
|
|
"street" => result["street"],
|
|
|
|
"city" => result["city"],
|
|
|
|
"city" => result["city"],
|
|
|
|
"state" => result["state"],
|
|
|
|
"state" => result["state"],
|
|
|
|
"inhabitants" => []
|
|
|
|
"inhabitants" => []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
last_location_id = result["id"]
|
|
|
|
last_location_id = result["id"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ -47,7 +36,7 @@ class Location
|
|
|
|
"name" => result["name"],
|
|
|
|
"name" => result["name"],
|
|
|
|
"age" => result["age"].to_i,
|
|
|
|
"age" => result["age"].to_i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
locations.last.inhabitants.push(new_person)
|
|
|
|
locations.last["inhabitants"].push(new_person)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return locations
|
|
|
|
return locations
|
|
|
|
@ -78,13 +67,13 @@ class Location
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return Location.new({
|
|
|
|
return {
|
|
|
|
"id" => results.first["id"],
|
|
|
|
"id" => results.first["id"].to_i,
|
|
|
|
"street" => results.first["street"],
|
|
|
|
"street" => results.first["street"],
|
|
|
|
"city" => results.first["city"],
|
|
|
|
"city" => results.first["city"],
|
|
|
|
"state" => results.first["state"],
|
|
|
|
"state" => results.first["state"],
|
|
|
|
"inhabitants" => inhabitants
|
|
|
|
"inhabitants" => inhabitants
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.create(opts={})
|
|
|
|
def self.create(opts={})
|
|
|
|
@ -95,7 +84,12 @@ class Location
|
|
|
|
RETURNING id, street, city, state;
|
|
|
|
RETURNING id, street, city, state;
|
|
|
|
SQL
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return Location.new(results.first)
|
|
|
|
return {
|
|
|
|
|
|
|
|
"id" => results.first["id"],
|
|
|
|
|
|
|
|
"street" => results.first["street"],
|
|
|
|
|
|
|
|
"city" => results.first["city"],
|
|
|
|
|
|
|
|
"state" => results.first["state"]
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.delete(id)
|
|
|
|
def self.delete(id)
|
|
|
|
@ -112,6 +106,11 @@ class Location
|
|
|
|
RETURNING id, street, city, state;
|
|
|
|
RETURNING id, street, city, state;
|
|
|
|
SQL
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return Location.new(results.first)
|
|
|
|
return {
|
|
|
|
|
|
|
|
"id" => results.first["id"],
|
|
|
|
|
|
|
|
"street" => results.first["street"],
|
|
|
|
|
|
|
|
"city" => results.first["city"],
|
|
|
|
|
|
|
|
"state" => results.first["state"]
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|