add use auth

This commit is contained in:
Asif
2018-06-12 15:24:26 +08:00
16 changed files with 174 additions and 254 deletions
+5
View File
@@ -19,5 +19,10 @@ class Deliveryinfo extends Model
'contact_person_no',
];
public function company() {
return $this->belongsTo(App\Company);
}
}
+56 -63
View File
@@ -4,107 +4,100 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Deliveryinfo;
use Auth;
class DeliveryinfoController extends Controller
{
/**
* Display a listing of the resource.
* Display the specified delivery-info.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function index()
public function show(Deliveryinfo $id)
{
$com_id = Deliveryinfo::all();
return view('company.deliveryinfo.index');
$deliveryinfo = deliveryinfo::find($id);
return response()->json($deliveryinfo, 200);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
* Store a newly delivery-info in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$deliveryinfo = Deliveryinfo::create($request->all());
$user = Auth::user();
return response()->json($deliveryinfo, 201);
$deliveryinfo = new Deliveryinfo;
$deliveryinfo->branch = $request->input('branch');
$deliveryinfo->deli_info = $request->input('deli_info');
$deliveryinfo->address = $request->input('address');
$deliveryinfo->city = $request->input('city');
$deliveryinfo->postcode = $request->input('postcode');
$deliveryinfo->state = $request->input('state');
$deliveryinfo->country = $request->input('country');
$deliveryinfo->contact_person = $request->input('contact_person');
$deliveryinfo->contact_person_no = $request->input('contact_person_no');
$deliveryinfo->com_id = $user->company();
$deliveryinfo->save();
return response()->json(['deliveryinfo'=>$deliveryinfo],201);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Deliveryinfo $com_id)
{
$deliveryinfo = deliveryinfo::find($com_id);
return response()->json($deliveryinfo, 200);
// return $delievryinfo;
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
* Update the delivery-info.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $com_id)
public function update(Request $request, $id)
{
$com_id = Deliveryinfo::find($com_id);
//$com_id->id = $request->input('id');
$com_id->branch = $request->input('branch');
$com_id->deli_info = $request->input('deli_info');
$com_id->address = $request->input('address');
$com_id->city = $request->input('city');
$com_id->postcode = $request->input('postcode');
$com_id->state = $request->input('state');
$com_id->country = $request->input('country');
$com_id->contact_person = $request->input('contact_person');
$com_id->contact_person_no = $request->input('contact_person_no');
$com_id->save();
$deliveryinfo = Deliveryinfo::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();
return response()->json(['com_id'=>$com_id],200);
if (!$deliveryinfo)
{
return response()->json(['message'=>'Access Denied!'], 404);
}
$deliveryinfo->branch = $request->input('branch');
$deliveryinfo->deli_info = $request->input('deli_info');
$deliveryinfo->address = $request->input('address');
$deliveryinfo->city = $request->input('city');
$deliveryinfo->postcode = $request->input('postcode');
$deliveryinfo->state = $request->input('state');
$deliveryinfo->country = $request->input('country');
$deliveryinfo->contact_person = $request->input('contact_person');
$deliveryinfo->contact_person_no = $request->input('contact_person_no');
$deliveryinfo->com_id = $user->company();
$deliveryinfo->save();
return response()->json(['deliveryinfo'=>$deliveryinfo],200);
}
/**
* Remove the specified resource from storage.
* Remove the delivery-info.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Deliveryinfo $com_id)
public function destroy(Deliveryinfo $id)
{
$com_id->delete($com_id);
$deliveryinfo = Deliveryinfo::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();
return response()->json($com_id, 204);
if (!$deliveryinfo)
{
return response()->json(['message'=>'Access Denied!'], 404);
}
$deliveryinfo->delete($id);
return response()->json($deliveryinfo, 204);
}
}
@@ -27,6 +27,7 @@ class CreateDeliveryinfosTable extends Migration
$table->integer('contact_person_no');
$table->timestamps();
});
}
@@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnToTableDeliveryInfo extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('deliveryinfos', function (Blueprint $table) {
$table->unsignedInteger('com_id');//FK wrn id
$table->foreign('com_id')
->references('id')->on('companies')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('deliveryinfos', function (Blueprint $table) {
//
});
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
use Illuminate\Database\Seeder;
class DeliveryInfoSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
+22 -28
View File
@@ -1339,40 +1339,34 @@
})
$('#del-inf').click(function() {
$.ajax({
type: "GET",
url: "api/company/deliveryinfo",
// $('#del-inf').click(function() {
// $.ajax({
// type: "GET",
// url: "api/company/deliveryinfo",
// success: function (data) {
// document.location.href = "home.html";
// },
success: function (data) {
//document.location.href = "home.html";
$('#del-no').append(' <div class="list-box-emp">\n' +
' <a href="#" class="list-item" data-popup="formPopup4">\n' +
' <i class="fa fa-truck"></i>\n' +
' <em class="seperate"></em>\n' +
' <span class="list-item-title"> Delivery 1</span>\n' +
' </a>\n' +
' </div>');
},
error: function (data) {
alert('error');
var errors = $.parseJSON(data.responseText);
//console.log(errors.error);
if (errors != null) {
for (var i in errors.error) {
//console.log(errors.error[i][0]);
$('.alert').append(errors.error[i][0] + "<br/>");
}
}
$('.alert').show();
}
// success: function (data) {
// //document.location.href = "home.html";
// $('#del-no').append(
// ''},
// error: function (data) {
// //alert('error');
// var errors = $.parseJSON(data.responseText);
// //console.log(errors.error);
// if (errors != null) {
// for (var i in errors.error) {
// //console.log(errors.error[i][0]);
// $('.alert').append(errors.error[i][0] + "<br/>");
// }
// }
// $('.alert').show();
// }
});
})
//});
// })
})
</script>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+9
View File
@@ -14,6 +14,7 @@ use App\Company;
*/
/* Routes for the DeliveryInfo in profile*/
Route::get('/company/deliveryinfo','DeliveryinfoController@index');
Route::get('/company/{com_id}/deliveryinfo','DeliveryinfoController@show');
@@ -51,6 +52,14 @@ Route::group(['middleware' => ['jwt.auth']], function() {
return response()->json(['user'=> $request->user()]);
});
/*Delivery-Info in profile menu*/
Route::get('/deliveryinfo/{com_id}','DeliveryinfoController@show');
Route::post('/deliveryinfo', 'DeliveryinfoController@store');
Route::put('/deliveryinfo/{com_id}', 'DeliveryinfoController@update');
Route::delete('/deliveryinfo/{com_id}', 'DeliveryinfoController@destroy');
});
-46
View File
@@ -1,46 +0,0 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
<<<<<<< HEAD
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
/* Routes for the DeliveryInfo in profile*/
Route::get('/api/company/del-info','DeliveryInfoController@index');
Route::get('/api/company/del-info','DeliveryInfoController@show');
Route::post('/api/company/{com-id}/del-info', 'DeliveryInfoController@store');
Route::put('/api/company/{com-id}/del-info', 'DeliveryInfoController@update');
Route::delete('/api/company/{com-id}/del-info', 'DeliveryInfoController@delete');
=======
//Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
//});
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('recover', 'AuthController@recover');
Route::group(['middleware' => ['jwt.auth']], function() {
Route::get('logout', 'AuthController@logout');
Route::get('test', function(){
return response()->json(['foo'=>'bar']);
});
});
>>>>>>> 737c7da54f7707363ff7c7fe2f6b0d68f2f484f1
-18
View File
@@ -1,18 +0,0 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
-29
View File
@@ -1,29 +0,0 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
/* Routes for the DeliveryInfo in profile*/
Route::get('/api/company/del-info','DeliveryInfoController@index');
Route::get('/api/company/del-info','DeliveryInfoController@show');
Route::post('/api/company/{com-id}/del-info', 'DeliveryInfoController@store');
Route::put('/api/company/{com-id}/del-info', 'DeliveryInfoController@update');
Route::delete('/api/company/{com-id}/del-info', 'DeliveryInfoController@delete');
-29
View File
@@ -1,29 +0,0 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
//Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
//});
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('recover', 'AuthController@recover');
Route::group(['middleware' => ['jwt.auth']], function() {
Route::get('logout', 'AuthController@logout');
Route::get('test', function(){
return response()->json(['foo'=>'bar']);
});
});
+28 -41
View File
@@ -7,57 +7,35 @@ use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Artisan;
use App\Deliveryinfo;
class DeliveryinfoTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testGET()
{
$response = $this->json('GET', '/api/company/deliveryinfo');
protected $company;
protected $user;
$response->assertStatus(200);
public function setUp()
{
parent::setUp();
Artisan::call('db:seed');
$this->user = factory(User::class)->create();
$this->company = $this->user->each(function ($u) {
factory(Company::class)->create([
'user_id' => $u->id,
]);
});
}
public function testPOST()
public function testPUT()
{
$response = $this->json('POST', '/api/company/com_id/deliveryinfo', [
$response = $this->actingAs($this->company)
->patchJson('/api/deliveryinfo', [
'branch' => 'Testing',
'deli_info' => 'Testing',
'address' => 'Testing',
'city' => 'Testing',
'postcode' => '12345',
'state' => 'Testing',
'country' => 'Testing',
'contact_person' => 'Testing',
'contact_person_no' => '12345',
]);
$response->assertStatus(201);
}
public function testDELETE()
{
$response = $this->json('DELETE', '/api/company/4/deliveryinfo');
$response->assertStatus(204);
}
public function testPUT()
{
$response = $this->json('PUT', '/api/company/1/deliveryinfo', [
//'id' => '1',
'branch' => 'Ci',
'deli_info' => 'Testing',
'address' => 'Testing',
'city' => 'Test',
'postcode' => '12345',
'state' => 'Testing',
@@ -67,6 +45,15 @@ class DeliveryinfoTest extends TestCase
]);
$response->assertStatus(200);
$response->assertStatus(200);
}
public function testDELETE()
{
$response = $this->json('DELETE', '/api/company/1/deliveryinfo');
$response->assertStatus(204);
}
}