Advertisement
Guest User

Untitled

a guest
May 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.70 KB | None | 0 0
  1. Item controller
  2.  
  3. public function list(Request $request){
  4.        
  5.         //validate the request
  6.         $this->validate($request, [
  7.             'company_guid' => 'required|exists:companies,guid',
  8.             'per_page' => 'integer|between:5,100',//the user cant ask for more than 100 rows in 1 query
  9.             'sort_order' => 'in:asc,desc'    
  10.         ]);
  11.  
  12.         //check if the token is valid
  13.         if (! $user = JWTAuth::parseToken()->authenticate()) {
  14.          return response()->json(['msg' => 'User not found'], 200);//404
  15.         }
  16.        
  17.         //set variables from the request
  18.         $guid           = $request->input('company_guid');        
  19.         $page           = $request->input('page');
  20.         $search_query   = $request->input('search_query');
  21.         $per_page       = $request->input('per_page');
  22.         $sort_by        = $request->input('sort_by');
  23.         $sort_order     = $request->input('sort_order');
  24.        
  25.         //check if page is set
  26.         if($page == null || !is_numeric($page)){
  27.             $page = 1;
  28.         }
  29.  
  30.         // check if table sort order is exsist, if not set default value "asc"
  31.         if($sort_order == null){
  32.             $sort_order = "asc";
  33.         }
  34.  
  35.         // check if sort by is set, if not set dafault value "name"
  36.         if($sort_by == null){
  37.             $sort_by = "name";
  38.         }
  39.  
  40.         //check if the column name is exsist in the table
  41.         if(!Schema::hasColumn('clients', $sort_by)){
  42.             $response = [
  43.                 'error' => "$sort_by column is not exsist"
  44.             ];
  45.             return response()->json($response, 200);//201
  46.         }
  47.  
  48.         //check if the page number is real
  49.         $rows = Company::where('guid',$guid)
  50.                                     ->first()
  51.                                     ->items()
  52.                                     ->get();
  53.  
  54.         $total_amount    = count($rows);
  55.         $max_pages       = ceil($total_amount/$per_page);
  56.         if($page > $max_pages){
  57.             $response = [
  58.                 'error' => "page $page is invalid, the pages range is $page - $max_pages"
  59.             ];
  60.             return response()->json($response, 200);//201
  61.         }
  62.  
  63.         // check if per_page is set
  64.         if($per_page == null || !is_numeric($per_page)){
  65.             $per_page = 5;
  66.         }
  67.  
  68.         // pull the clients list
  69.         $rows = Company::with(['items' => function($query) use ($sort_order,$sort_by) {
  70.                                 $query->with('currency')->orderBy($sort_by, $sort_order);
  71.                             }])
  72.                             ->where('guid',$guid)
  73.                             ->first()
  74.                             ->items();
  75.                             // ->orderBy('name', $sort_order);
  76.  
  77.         if($search_query != ""){
  78.             $rows = $rows->where(function($query) use ($search_query) {
  79.                 $query->where('name','LIKE','%'.$search_query.'%')
  80.                       ->orWhere('email','LIKE','%'.$search_query.'%')
  81.                       ->orWhere('vat_number','LIKE','%'.$search_query.'%')
  82.                       ->orWhere('contact_name','LIKE','%'.$search_query.'%');
  83.             });
  84.             $search_amount = count($rows->get());
  85.         }
  86.  
  87.         $rows = $rows->skip($per_page*($page-1))
  88.                      ->take($per_page);
  89.  
  90.         $response = [
  91.             'total_amount'  => $total_amount,
  92.             'rows'          => $rows->get()
  93.         ];
  94.  
  95.         if(isset($search_amount)){
  96.             $response['search_amount'] = $search_amount;
  97.         }
  98.  
  99.         return response()->json($response, 200);//201
  100.     }
  101.  
  102.  
  103.  
  104. <?php
  105.  
  106. use Illuminate\Support\Facades\Schema;
  107. use Illuminate\Database\Schema\Blueprint;
  108. use Illuminate\Database\Migrations\Migration;
  109.  
  110. class CreateItemsTable extends Migration
  111. {
  112.     /**
  113.      * Run the migrations.
  114.      *
  115.      * @return void
  116.      */
  117.     public function up()
  118.     {
  119.         Schema::create('items', function (Blueprint $table) {
  120.             $table->increments('id');
  121.             $table->uuid('guid')->nullable();
  122.             $table->string('name');
  123.             $table->string('description')
  124.             $table->float('price');
  125.             $table->integer('currency_id')->unsigned()->nullable();
  126.             $table->foreign('currency_id')->references('id')->on('currencies');
  127.             $table->integer('company_id')->unsigned()->nullable();
  128.             $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  129.             $table->timestamps();
  130.         });
  131.     }
  132.  
  133.     /**
  134.      * Reverse the migrations.
  135.      *
  136.      * @return void
  137.      */
  138.     public function down()
  139.     {
  140.         Schema::dropIfExists('items');
  141.     }
  142. }
  143.  
  144.  
  145. <?php
  146.  
  147. use Illuminate\Support\Facades\Schema;
  148. use Illuminate\Database\Schema\Blueprint;
  149. use Illuminate\Database\Migrations\Migration;
  150.  
  151. class CreateCurrenciesTable extends Migration
  152. {
  153.     /**
  154.      * Run the migrations.
  155.      *
  156.      * @return void
  157.      */
  158.     public function up()
  159.     {
  160.         Schema::create('currencies', function (Blueprint $table) {
  161.             $table->increments('id');
  162.             $table->uuid('guid')->nullable();
  163.             $table->string('name');
  164.             $table->string('symbol')->nullable();
  165.             $table->string('code')->nullable();
  166.             $table->string('rate')->nullable();
  167.             $table->timestamps();
  168.         });
  169.     }
  170.  
  171.     /**
  172.      * Reverse the migrations.
  173.      *
  174.      * @return void
  175.      */
  176.     public function down()
  177.     {
  178.         Schema::dropIfExists('currencies');
  179.     }
  180. }
  181.  
  182. <?php
  183.  
  184. use Illuminate\Support\Facades\Schema;
  185. use Illuminate\Database\Schema\Blueprint;
  186. use Illuminate\Database\Migrations\Migration;
  187.  
  188. class CreateCompaniesTable extends Migration
  189. {
  190.     /**
  191.      * Run the migrations.
  192.      *
  193.      * @return void
  194.      */
  195.     public function up()
  196.     {
  197.         Schema::create('companies', function (Blueprint $table) {
  198.             $table->increments('id');
  199.             $table->uuid('guid')->nullable();
  200.             $table->string('name')->nullable();
  201.             $table->string('name_en')->nullable();
  202.             $table->integer('vat_number')->unique();
  203.             $table->string('address')->nullable();
  204.             $table->string('city')->nullable();
  205.             $table->integer('company_type_id')->unsigned();
  206.             $table->foreign('company_type_id')->references('id')->on('company_types')->onDelete('cascade');
  207.             $table->integer('account_id')->unsigned();
  208.             $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
  209.             $table->boolean('is_active')->dafault(false);
  210.             $table->timestamps();
  211.         });
  212.     }
  213.  
  214.     /**
  215.      * Reverse the migrations.
  216.      *
  217.      * @return void
  218.      */
  219.     public function down()
  220.     {
  221.         Schema::dropIfExists('companies');
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement