add new file
This commit is contained in:
3
app/assets/javascripts/api/clients.coffee
Normal file
3
app/assets/javascripts/api/clients.coffee
Normal file
@@ -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/
|
||||||
3
app/assets/javascripts/clients.coffee
Normal file
3
app/assets/javascripts/clients.coffee
Normal file
@@ -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/
|
||||||
3
app/assets/stylesheets/api/clients.scss
Normal file
3
app/assets/stylesheets/api/clients.scss
Normal file
@@ -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/
|
||||||
3
app/assets/stylesheets/clients.scss
Normal file
3
app/assets/stylesheets/clients.scss
Normal file
@@ -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/
|
||||||
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
|
||||||
2
app/helpers/api/clients_helper.rb
Normal file
2
app/helpers/api/clients_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
module Api::ClientsHelper
|
||||||
|
end
|
||||||
2
app/helpers/clients_helper.rb
Normal file
2
app/helpers/clients_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
module ClientsHelper
|
||||||
|
end
|
||||||
7
app/models/client.rb
Normal file
7
app/models/client.rb
Normal file
@@ -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
|
||||||
7
app/views/api/clients/index.json.jbuilder
Normal file
7
app/views/api/clients/index.json.jbuilder
Normal file
@@ -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
|
||||||
2
app/views/clients/_client.json.jbuilder
Normal file
2
app/views/clients/_client.json.jbuilder
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
json.extract! client, :id, :created_at, :updated_at
|
||||||
|
json.url client_url(client, format: :json)
|
||||||
54
app/views/clients/_form.html.erb
Normal file
54
app/views/clients/_form.html.erb
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<%= simple_form_for(@client) do |f| %>
|
||||||
|
<nav class="breadcrumb">
|
||||||
|
<a class="breadcrumb-item" href="<%= dashboard_path %>">Home</a>
|
||||||
|
<a class="breadcrumb-item active" href="<%= clients_path %>">Client</a>
|
||||||
|
<a class="breadcrumb-item active" href="#">
|
||||||
|
<% if !@client.id.nil? %>
|
||||||
|
Edit
|
||||||
|
<% else %>
|
||||||
|
New
|
||||||
|
<% end %>
|
||||||
|
</a>
|
||||||
|
</nav>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-6" id="textbox_group">
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="name" class="string optional control-label">Name:</label>
|
||||||
|
<%= f.input :name ,:label =>false,:error => false,:placeholder =>'Please enter client name',input_html: { class: "form-control" } %>
|
||||||
|
<%= f.error :name ,style: 'color: red' %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="nrc" class="string optional control-label">NRC:</label>
|
||||||
|
<%= f.input :nrc ,:error=>false,:label =>false,:placeholder =>'Please enter client NRC',input_html: { class: "form-control" } %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="email" class="string optional control-label">Email:</label>
|
||||||
|
<%= f.input :email ,:error=>false,:label =>false ,:placeholder =>'Please enter client email',input_html: { class: "form-control" } %>
|
||||||
|
<%= f.error :email ,style: 'color: red' %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="phone" class="string optional control-label">Phone:</label>
|
||||||
|
<%= f.input :phone,:error=>false,:label =>false,:placeholder =>'Please enter client phone',input_html: { class: "form-control" } %>
|
||||||
|
<%= f.error :phone ,style: 'color: red' %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="address" class="string optional control-label">Address:</label>
|
||||||
|
<%= f.input :address,:error=>false,:label =>false,:placeholder =>'Please enter client address',input_html: { class: "form-control" } %>
|
||||||
|
<%= f.error :address ,style: 'color: red' %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label for="product_type" class="string optional control-label">Product Type:</label>
|
||||||
|
<%= f.select :product_type,options_for_select([['CARD','card'],['TICKET','ticket']], params[:product_type]),
|
||||||
|
{}, { :class => 'form-control' } %>
|
||||||
|
<%= f.error :product_type ,style: 'color: red' %>
|
||||||
|
</div>
|
||||||
|
<div class ="form-group" >
|
||||||
|
<label></label>
|
||||||
|
<div class="actions">
|
||||||
|
<%= f.button :submit, :class => 'btn btn-primary',:id =>'btn_submit' %>
|
||||||
|
<%= link_to 'Cancel', users_path ,:class => 'btn btn-primary',:id => 'btnback' %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
1
app/views/clients/edit.html.erb
Normal file
1
app/views/clients/edit.html.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<%= render 'form', client: @client %>
|
||||||
55
app/views/clients/index.html.erb
Normal file
55
app/views/clients/index.html.erb
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<div class="row ">
|
||||||
|
<nav class="breadcrumb">
|
||||||
|
<a class="breadcrumb-item active" href="<%= dashboard_path %>">Home</a>
|
||||||
|
<a class="breadcrumb-item active" href="#">Client</a>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
<div class="row top-content">
|
||||||
|
<span style="float: right">
|
||||||
|
<%= link_to t('.new', :default => t("helpers.links.new")),new_client_path,:class => 'btn btn-primary' %>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="row content">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<strong>Client List</strong>
|
||||||
|
</div>
|
||||||
|
<div class="card-block">
|
||||||
|
<table class="table" style="border-top:none">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Nrc</th>
|
||||||
|
<th>Email</th>
|
||||||
|
<th>Phone</th>
|
||||||
|
<th>Address</th>
|
||||||
|
<th>Product Type</th>
|
||||||
|
<th>Created At </th>
|
||||||
|
<th>Action</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<% @clients.each do |client| %>
|
||||||
|
<tr>
|
||||||
|
<td><%= client.name rescue '' %></td>
|
||||||
|
<td><%= client.nrc rescue '' %></td>
|
||||||
|
<td><%= client.email rescue '' %></td>
|
||||||
|
<td><%= client.phone rescue '' %></td>
|
||||||
|
<td><%= client.address rescue '' %></td>
|
||||||
|
<td><%= client.product_type rescue '' %></td>
|
||||||
|
<td><%= client.created_at.strftime("%e,%b %Y %I:%M %p") rescue '' %></td>
|
||||||
|
<td>
|
||||||
|
<%= 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' %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<%=paginate @clients %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
1
app/views/clients/index.json.jbuilder
Normal file
1
app/views/clients/index.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
|||||||
|
json.array! @clients, partial: 'clients/client', as: :client
|
||||||
1
app/views/clients/new.html.erb
Normal file
1
app/views/clients/new.html.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<%= render 'form', client: @client %>
|
||||||
40
app/views/clients/show.html.erb
Normal file
40
app/views/clients/show.html.erb
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<div class="row">
|
||||||
|
<nav class="breadcrumb">
|
||||||
|
<a class="breadcrumb-item" href="<%= dashboard_path %>">Home</a>
|
||||||
|
<a class="breadcrumb-item active" href="<%= users_path %>">Clients</a>
|
||||||
|
<a class="breadcrumb-item active" href="#">Show</a>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
<div class="row content">
|
||||||
|
<div class="col-lg-6 show">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-4"><strong>Name:</strong></div>
|
||||||
|
<div class="col-lg-8 uppercase"><%= @client.name %></div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-4"><strong>NRC:</strong></div>
|
||||||
|
<div class="col-lg-8"><%= @client.nrc %></div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-4"><strong>Email:</strong></div>
|
||||||
|
<div class="col-lg-8"><%= @client.email %></div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-4"><strong>Phone:</strong></div>
|
||||||
|
<div class="col-lg-8"><%= @client.phone %></div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-4"><strong>Address:</strong></div>
|
||||||
|
<div class="col-lg-8"><%= @client.address %></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-8">
|
||||||
|
<div class="col-lg-3"></div>
|
||||||
|
<div class="col-lg-4 btn-show-action">
|
||||||
|
<%= link_to 'Edit', edit_client_path(@client),:class => 'btn btn-primary' %>
|
||||||
|
<%= link_to 'Back', clients_path ,:class => 'btn btn-primary'%>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
1
app/views/clients/show.json.jbuilder
Normal file
1
app/views/clients/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
|||||||
|
json.partial! "clients/client", client: @client
|
||||||
15
db/migrate/20170203091136_create_clients.rb
Normal file
15
db/migrate/20170203091136_create_clients.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
class CreateClients < ActiveRecord::Migration[5.0]
|
||||||
|
def change
|
||||||
|
create_table :clients do |t|
|
||||||
|
t.string :name,:null => false
|
||||||
|
t.string :nrc
|
||||||
|
t.string :email
|
||||||
|
t.string :phone
|
||||||
|
t.string :address
|
||||||
|
t.string :product_type
|
||||||
|
t.string :secrect_key
|
||||||
|
t.timestamps null: false
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
7
test/controllers/api/clients_controller_test.rb
Normal file
7
test/controllers/api/clients_controller_test.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class Api::ClientsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
||||||
48
test/controllers/clients_controller_test.rb
Normal file
48
test/controllers/clients_controller_test.rb
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class ClientsControllerTest < ActionDispatch::IntegrationTest
|
||||||
|
setup do
|
||||||
|
@client = clients(:one)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get index" do
|
||||||
|
get clients_url
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get new" do
|
||||||
|
get new_client_url
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should create client" do
|
||||||
|
assert_difference('Client.count') do
|
||||||
|
post clients_url, params: { client: { } }
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_redirected_to client_url(Client.last)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should show client" do
|
||||||
|
get client_url(@client)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get edit" do
|
||||||
|
get edit_client_url(@client)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should update client" do
|
||||||
|
patch client_url(@client), params: { client: { } }
|
||||||
|
assert_redirected_to client_url(@client)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should destroy client" do
|
||||||
|
assert_difference('Client.count', -1) do
|
||||||
|
delete client_url(@client)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_redirected_to clients_url
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user