Вы находитесь на странице: 1из 6

class CustomersController class CustomersController < ApplicationController

# before_action :set_customer, only: [:show, :edit, :update, :destroy]

# GET /customers
# GET /customers.json
def index
@customers = Customer.all
end

# GET /customers/1
# GET /customers/1.json
def show
end

# GET /customers/new
def new
@customer = Customer.new
end

# GET /customers/1/edit
def edit
end

# POST /customers
# POST /customers.json
def create
@customer = Customer.new
@customer.firstName = params[:firstName]
@customer.lastName = params[:lastName]
@customer.email = params[:email]
#@customer.id = params[:id]
@customer.award = 0
@customer.lastOrder = 0
@customer.lastOrder2 = 0
@customer.lastOrder3 = 0
@customer.save
render json: @customer.to_json, status:201
end

# GET "customers/:id"
def retrieve

if (params[:email]) != nil
@customer = Customer.find_by(email: params[:email])
if @customer
#success
render json: @customer , status: 200
else
#failure
head 404
end
end

if (params[:id]) != nil
@customer = Customer.find_by(id: params[:id])

if @customer
#success
render json: @customer , status: 200
else
#failure
head 404
end
end
end

# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if @customer.update(customer_params)
format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @customer }
else
format.html { render :edit }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end

def order
#p params
@id = params[:itemId]
@award = params[:award]
@total = params[:total]
@item = Item.find_by(id: @id)
@customer = Customer.find_by(id: @id)
#p @customer
#@award = Customer.find(order_params[:award])
#@total = Customer.find(order_params[:total])
#@item = Item.find(order_params[:itemId])
#@customer = Customer.find(order_params[:customerId]) rescue nil

if @award == 0
@customer.lastOrder3 = @customer.lastOrder2
@customer.lastOrder2 = @customer.lastOrder
@customer.lastOrder = @total

if (@customer.lastOrder != 0 && @customer.lastOrder2 != 0 && @customer.lastOrder3 != 0)


@customer.award = '%.2f' % (0.10 * (@customer.lastOrder + @customer.lastOrder2 +
@customer.lastOrder3)/3)
else

end
else
@customer.award = 0
@customer.lastOrder = 0
@customer.lastOrder2 = 0
@customer.lastOrder3 = 0
end
@customer.save
render json: "", status: 204
end

end

< ApplicationController
# before_action :set_customer, only: [:show, :edit, :update, :destroy]

# GET /customers
# GET /customers.json
def index
@customers = Customer.all
end

# GET /customers/1
# GET /customers/1.json
def show
end

# GET /customers/new
def new
@customer = Customer.new
end

# GET /customers/1/edit
def edit
end

# POST /customers
# POST /customers.json
def create
@customer = Customer.new
@customer.firstName = params[:firstName]
@customer.lastName = params[:lastName]
@customer.email = params[:email]
#@customer.id = params[:id]
@customer.award = 0
@customer.lastOrder = 0
@customer.lastOrder2 = 0
@customer.lastOrder3 = 0
@customer.save
render json: @customer.to_json, status:201
end

# GET "customers/:id"
def retrieve

if (params[:email]) != nil
@customer = Customer.find_by(email: params[:email])
if @customer
#success
render json: @customer , status: 200
else
#failure
head 404
end
end

if (params[:id]) != nil
@customer = Customer.find_by(id: params[:id])
if @customer
#success
render json: @customer , status: 200
else
#failure
head 404
end
end
end

# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if @customer.update(customer_params)
format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @customer }
else
format.html { render :edit }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end

def order
#p params
@id = params[:itemId]
@award = params[:award]
@total = params[:total]
@item = Item.find_by(id: @id)
@customer = Customer.find_by(id: @id)
#p @customer
#@award = Customer.find(order_params[:award])
#@total = Customer.find(order_params[:total])
#@item = Item.find(order_params[:itemId])
#@customer = Customer.find(order_params[:customerId]) rescue nil

if @award == 0
@customer.lastOrder3 = @customer.lastOrder2
@customer.lastOrder2 = @customer.lastOrder
@customer.lastOrder = @total
if (@customer.lastOrder != 0 && @customer.lastOrder2 != 0 && @customer.lastOrder3 != 0)
@customer.award = '%.2f' % (0.10 * (@customer.lastOrder + @customer.lastOrder2 +
@customer.lastOrder3)/3)
else

end
else
@customer.award = 0
@customer.lastOrder = 0
@customer.lastOrder2 = 0
@customer.lastOrder3 = 0
end
@customer.save
render json: "", status: 204
end

end

Вам также может понравиться