forked from AdaGold/video-store-api
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcustomers_controller.rb
More file actions
35 lines (27 loc) · 956 Bytes
/
customers_controller.rb
File metadata and controls
35 lines (27 loc) · 956 Bytes
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
class CustomersController < ApplicationController
def index
customers = Customer.all
render json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok
end
def show
customer = Customer.find_by(id: params[:id])
if customer.nil?
render json: {errors: {id: ["No customer like that found, #{params[:id]}"]}}, status: :not_found
else
render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok
end
end
def create
customer = Customer.new(customer_params)
if customer.save
#do something
render json: { id: customer.id}, status: :created
else
render json: { 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)
end
end