parent
c1db2f3a42
commit
4c8aecf1fe
@ -0,0 +1,23 @@
|
|||||||
|
class LocationsController < ApplicationController
|
||||||
|
skip_before_action :verify_authenticity_token
|
||||||
|
|
||||||
|
def index
|
||||||
|
render json: Location.all
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
render json: Location.find(params["id"])
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
render json: Location.create(params["location"])
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete
|
||||||
|
render json: Location.delete(params["id"])
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
render json: Location.update(params["id"], params["location"])
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
class Location
|
||||||
|
# connect to postgres
|
||||||
|
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"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.all
|
||||||
|
results = DB.exec("SELECT * FROM locations;")
|
||||||
|
return results.map { |result| Location.new(result) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.find(id)
|
||||||
|
results = DB.exec("SELECT * FROM locations WHERE id=#{id};")
|
||||||
|
return Location.new(results.first)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.create(opts={})
|
||||||
|
results = DB.exec("INSERT INTO locations (street, city, state) VALUES ( '#{opts["street"]}', '#{opts["city"]}', '#{opts["state"]}' );")
|
||||||
|
return { created:true }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.delete(id)
|
||||||
|
results = DB.exec("DELETE FROM locations WHERE id=#{id};")
|
||||||
|
return { deleted: true }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.update(id, opts={})
|
||||||
|
results = DB.exec("UPDATE locations SET street='#{opts["street"]}', city='#{opts["city"]}', state='#{opts["state"]}' WHERE id=#{id} ;")
|
||||||
|
return { updated: true }
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in new issue