diff --git a/app/assets/javascripts/api/clients.coffee b/app/assets/javascripts/api/clients.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api/clients.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/clients.coffee b/app/assets/javascripts/clients.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/clients.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api/clients.scss b/app/assets/stylesheets/api/clients.scss new file mode 100644 index 0000000..55f5114 --- /dev/null +++ b/app/assets/stylesheets/api/clients.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api/clients controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/clients.scss b/app/assets/stylesheets/clients.scss new file mode 100644 index 0000000..6531617 --- /dev/null +++ b/app/assets/stylesheets/clients.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the clients controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api/clients_controller.rb b/app/controllers/api/clients_controller.rb new file mode 100644 index 0000000..9006537 --- /dev/null +++ b/app/controllers/api/clients_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb new file mode 100644 index 0000000..e6dc4cd --- /dev/null +++ b/app/controllers/clients_controller.rb @@ -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 diff --git a/app/helpers/api/clients_helper.rb b/app/helpers/api/clients_helper.rb new file mode 100644 index 0000000..3c56f1d --- /dev/null +++ b/app/helpers/api/clients_helper.rb @@ -0,0 +1,2 @@ +module Api::ClientsHelper +end diff --git a/app/helpers/clients_helper.rb b/app/helpers/clients_helper.rb new file mode 100644 index 0000000..9015906 --- /dev/null +++ b/app/helpers/clients_helper.rb @@ -0,0 +1,2 @@ +module ClientsHelper +end diff --git a/app/models/client.rb b/app/models/client.rb new file mode 100644 index 0000000..2525c51 --- /dev/null +++ b/app/models/client.rb @@ -0,0 +1,7 @@ +class Client < ApplicationRecord + validates :name, presence: { message: "Please enter client name." } + validates :name, :uniqueness => {:message =>"This client name is already taken." } ,on: :create + validates :email, presence: { message: "Please enter client email." } + validates :phone, presence: { message: "Please enter client phone." } + validates :address, presence: { message: "Please enter client address." } +end diff --git a/app/views/api/clients/index.json.jbuilder b/app/views/api/clients/index.json.jbuilder new file mode 100644 index 0000000..b5299b2 --- /dev/null +++ b/app/views/api/clients/index.json.jbuilder @@ -0,0 +1,7 @@ +if @out[0] == true + json.set! :status, @out[0] + json.set! :data, @out[1] +else + json.set! :status, @out[0] + json.set! :message,@out[1] +end \ No newline at end of file diff --git a/app/views/clients/_client.json.jbuilder b/app/views/clients/_client.json.jbuilder new file mode 100644 index 0000000..e7dc337 --- /dev/null +++ b/app/views/clients/_client.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! client, :id, :created_at, :updated_at +json.url client_url(client, format: :json) \ No newline at end of file diff --git a/app/views/clients/_form.html.erb b/app/views/clients/_form.html.erb new file mode 100644 index 0000000..849cca9 --- /dev/null +++ b/app/views/clients/_form.html.erb @@ -0,0 +1,54 @@ +<%= simple_form_for(@client) do |f| %> + +
| Name | +Nrc | +Phone | +Address | +Product Type | +Created At | +Action | +|
|---|---|---|---|---|---|---|---|
| <%= client.name rescue '' %> | +<%= client.nrc rescue '' %> | +<%= client.email rescue '' %> | +<%= client.phone rescue '' %> | +<%= client.address rescue '' %> | +<%= client.product_type rescue '' %> | +<%= client.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %> | ++ <%= link_to 'Detail', + client_path(client), :class => 'btn btn-primary btn-sm' %> + <%= link_to 'Edit', + edit_client_path(client), :class => 'btn btn-primary btn-sm' %> + <%= link_to 'Delete', clients_path(client), method: :delete, data: { confirm: 'Are you sure?' },:class => 'btn btn-primary btn-sm' %> + | +