Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Item controller
- public function list(Request $request){
- //validate the request
- $this->validate($request, [
- 'company_guid' => 'required|exists:companies,guid',
- 'per_page' => 'integer|between:5,100',//the user cant ask for more than 100 rows in 1 query
- 'sort_order' => 'in:asc,desc'
- ]);
- //check if the token is valid
- if (! $user = JWTAuth::parseToken()->authenticate()) {
- return response()->json(['msg' => 'User not found'], 200);//404
- }
- //set variables from the request
- $guid = $request->input('company_guid');
- $page = $request->input('page');
- $search_query = $request->input('search_query');
- $per_page = $request->input('per_page');
- $sort_by = $request->input('sort_by');
- $sort_order = $request->input('sort_order');
- //check if page is set
- if($page == null || !is_numeric($page)){
- $page = 1;
- }
- // check if table sort order is exsist, if not set default value "asc"
- if($sort_order == null){
- $sort_order = "asc";
- }
- // check if sort by is set, if not set dafault value "name"
- if($sort_by == null){
- $sort_by = "name";
- }
- //check if the column name is exsist in the table
- if(!Schema::hasColumn('clients', $sort_by)){
- $response = [
- 'error' => "$sort_by column is not exsist"
- ];
- return response()->json($response, 200);//201
- }
- //check if the page number is real
- $rows = Company::where('guid',$guid)
- ->first()
- ->items()
- ->get();
- $total_amount = count($rows);
- $max_pages = ceil($total_amount/$per_page);
- if($page > $max_pages){
- $response = [
- 'error' => "page $page is invalid, the pages range is $page - $max_pages"
- ];
- return response()->json($response, 200);//201
- }
- // check if per_page is set
- if($per_page == null || !is_numeric($per_page)){
- $per_page = 5;
- }
- // pull the clients list
- $rows = Company::with(['items' => function($query) use ($sort_order,$sort_by) {
- $query->with('currency')->orderBy($sort_by, $sort_order);
- }])
- ->where('guid',$guid)
- ->first()
- ->items();
- // ->orderBy('name', $sort_order);
- if($search_query != ""){
- $rows = $rows->where(function($query) use ($search_query) {
- $query->where('name','LIKE','%'.$search_query.'%')
- ->orWhere('email','LIKE','%'.$search_query.'%')
- ->orWhere('vat_number','LIKE','%'.$search_query.'%')
- ->orWhere('contact_name','LIKE','%'.$search_query.'%');
- });
- $search_amount = count($rows->get());
- }
- $rows = $rows->skip($per_page*($page-1))
- ->take($per_page);
- $response = [
- 'total_amount' => $total_amount,
- 'rows' => $rows->get()
- ];
- if(isset($search_amount)){
- $response['search_amount'] = $search_amount;
- }
- return response()->json($response, 200);//201
- }
- <?php
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreateItemsTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('items', function (Blueprint $table) {
- $table->increments('id');
- $table->uuid('guid')->nullable();
- $table->string('name');
- $table->string('description')
- $table->float('price');
- $table->integer('currency_id')->unsigned()->nullable();
- $table->foreign('currency_id')->references('id')->on('currencies');
- $table->integer('company_id')->unsigned()->nullable();
- $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('items');
- }
- }
- <?php
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreateCurrenciesTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('currencies', function (Blueprint $table) {
- $table->increments('id');
- $table->uuid('guid')->nullable();
- $table->string('name');
- $table->string('symbol')->nullable();
- $table->string('code')->nullable();
- $table->string('rate')->nullable();
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('currencies');
- }
- }
- <?php
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreateCompaniesTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('companies', function (Blueprint $table) {
- $table->increments('id');
- $table->uuid('guid')->nullable();
- $table->string('name')->nullable();
- $table->string('name_en')->nullable();
- $table->integer('vat_number')->unique();
- $table->string('address')->nullable();
- $table->string('city')->nullable();
- $table->integer('company_type_id')->unsigned();
- $table->foreign('company_type_id')->references('id')->on('company_types')->onDelete('cascade');
- $table->integer('account_id')->unsigned();
- $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
- $table->boolean('is_active')->dafault(false);
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('companies');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement