@ -10,7 +10,11 @@ class LocationsController < ApplicationController
end
def create
render json: Location.create(params["location"])
created_location = Location.create(params["location"])
if params["id"]
updated_person = Person.addHome(params["id"], created_location.id)
render json: { location: created_location, person: updated_person }
def delete
@ -1,4 +1,5 @@
class Location
attr_reader :id, :street, :city, :state
# connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
@ -48,4 +48,16 @@ class Person
)
return Person.new(results.first)
def self.addHome(person_id, home_id)
results = DB.exec(
<<-SQL
UPDATE people
SET home_id = #{home_id}
WHERE id = #{person_id}
RETURNING id, name, age, home_id;
SQL
@ -19,5 +19,6 @@ Rails.application.routes.draw do
put '/companies/:id', to: 'companies#update'
post '/locations/:id/people', to: 'people#create'
post '/people/:id/locations', to: 'locations#create'