add new file
This commit is contained in:
19
app/controllers/api/clients_controller.rb
Normal file
19
app/controllers/api/clients_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Api::ClientsController < ApplicationController
|
||||
skip_before_filter :verify_authenticity_token
|
||||
|
||||
def index
|
||||
session_token=params[:session_token]
|
||||
check_member= Member.authenticate_session_token(session_token)
|
||||
if !check_member.nil?
|
||||
clients =Client.all
|
||||
arr_client=Array.new
|
||||
clients.each do |client|
|
||||
str={:id => client.id,:name => client.name,:email => client.email,:phone => client.phone,:address => client.address,:product_type => client.product_type}
|
||||
arr_client.push(str)
|
||||
end
|
||||
@out=true,arr_client
|
||||
else
|
||||
@out=false,"Sorry!Unauthorized user!"
|
||||
end
|
||||
end
|
||||
end
|
||||
95
app/controllers/clients_controller.rb
Normal file
95
app/controllers/clients_controller.rb
Normal file
@@ -0,0 +1,95 @@
|
||||
class ClientsController < ApplicationController
|
||||
before_action :set_client, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# GET /clients
|
||||
# GET /clients.json
|
||||
def index
|
||||
@clients = Client.all.page(params[:page])
|
||||
end
|
||||
|
||||
# GET /clients/1
|
||||
# GET /clients/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /clients/new
|
||||
def new
|
||||
@client = Client.new
|
||||
end
|
||||
|
||||
# GET /clients/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /clients
|
||||
# POST /clients.json
|
||||
def create
|
||||
@client = Client.new(client_params)
|
||||
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
|
||||
key=cipher.random_key
|
||||
secrect_key= Base64.encode64(key)
|
||||
@client.secrect_key=secrect_key
|
||||
|
||||
respond_to do |format|
|
||||
if @client.save
|
||||
format.html { redirect_to @client, notice: 'Client was successfully created.' }
|
||||
format.json { render :show, status: :created, location: @client }
|
||||
else
|
||||
format.html { render :new }
|
||||
format.json { render json: @client.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /clients/1
|
||||
# PATCH/PUT /clients/1.json
|
||||
def update
|
||||
name=client_params['name'].delete(' ')
|
||||
find_client=Client.find_by_name(name)
|
||||
check=true
|
||||
if !find_client.nil?
|
||||
if find_client.id !=@client.id
|
||||
check=false
|
||||
@client.errors.add(:name,"This client name is already taken.")
|
||||
end
|
||||
end
|
||||
respond_to do |format|
|
||||
if check && @client.update(client_params)
|
||||
format.html { redirect_to @client, notice: 'Client was successfully updated.' }
|
||||
format.json { render :show, status: :ok, location: @client }
|
||||
else
|
||||
format.html { render :edit }
|
||||
format.json { render json: @client.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /clients/1
|
||||
# DELETE /clients/1.json
|
||||
def destroy
|
||||
message="Client was successfully destroyed."
|
||||
|
||||
find_batch=Batch.find_by_id(@client.id)
|
||||
if !find_batch.nil?
|
||||
message='Unable to delete client named '+ @client.name.to_s+'.'
|
||||
else
|
||||
@client.destroy
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to clients_url, notice: message }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_client
|
||||
@client = Client.find(params[:id])
|
||||
end
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
def client_params
|
||||
params.require(:client).permit(:name, :nrc, :email, :phone, :address,:product_type)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user