Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed show and create methods from customers controller #4

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,4 @@ def index
render json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]),
status: :ok
end

def show
customer = Customer.find_by(id: params[:id])

if !customer.nil?
render json: customer.as_json(only: [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]),
status: :ok
else
render json: {ok: false, errors: {customer: ["customer not found"]}},
status: :not_found
end
end

def create
customer = Customer.new(customer_params)

if customer.save
render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count, :id]),
status: :ok
else
render json: {ok: false, errors: customer.errors.messages},
status: :bad_request
end
end

private

def customer_params
params.require(:customer).permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count)
end
end
62 changes: 0 additions & 62 deletions test/controllers/customers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,4 @@
end
end
end

describe "show" do
it "can get a customer with valid info" do
get customer_path(customers(:two).id)
must_respond_with :success
expect(response.header["Content-Type"]).must_include "json"
end
it "raises an error and return not found if given invalid params" do
get customer_path(100)
must_respond_with :not_found
body = JSON.parse(response.body)
expect(body).must_be_kind_of Hash
expect(body).must_include "errors"
expect(body["errors"]).must_include "customer"
end
end

describe "create" do
let(:customer_data) {
{
name: "Chris Tucker",
address: "555 Scammer Drive",
city: "Seattle",
state: "WA",
postal_code: "11111",
phone: "123-867-5309",
movies_checked_out_count: 2,
registered_at: DateTime.now,
}
}

it "creates a new customer given valid data" do
expect {
post customers_path, params: {customer: customer_data}
}.must_change "Customer.count", 1

body = JSON.parse(response.body)
expect(response.header["Content-Type"]).must_include "json"
expect(body).must_be_kind_of Hash
expect(body).must_include "id"

customer = Customer.find(body["id"].to_i)

expect(customer.name).must_equal customer_data[:name]
must_respond_with :success
end

it "returns an error for invalid customer data" do
customer_data["name"] = nil

expect {
post customers_path, params: {customer: customer_data}
}.wont_change "Customer.count"

body = JSON.parse(response.body)
expect(response.header["Content-Type"]).must_include "json"
expect(body).must_be_kind_of Hash
expect(body).must_include "errors"
expect(body["errors"]).must_include "name"
must_respond_with :bad_request
end
end
end