From f463d0b6b241d0c2b947c86e96d0b3dcf2c995db Mon Sep 17 00:00:00 2001 From: Zain Fauzan Rofie Azizi Date: Tue, 12 Jun 2018 09:49:24 +0800 Subject: [PATCH] completed the user intergration --- app/DeliveryInfo.php | 5 + .../Controllers/DeliveryInfoController.php | 118 ++++++++---------- ...4_25_024318_create_deliveryinfos_table.php | 1 + ...3330_add_column_to_table_delivery_info.php | 37 ++++++ database/seeds/DeliveryInfoSeeder.php | 16 +++ routes/.api.php.swp | Bin 12288 -> 0 bytes routes/.api_BASE_14556.php.swp | Bin 12288 -> 0 bytes routes/.api_LOCAL_14556.php.swp | Bin 12288 -> 0 bytes routes/.api_REMOTE_14556.php.swp | Bin 12288 -> 0 bytes routes/api.php | 17 +-- routes/api_BACKUP_14556.php | 46 ------- routes/api_BASE_14556.php | 18 --- routes/api_LOCAL_14556.php | 29 ----- routes/api_REMOTE_14556.php | 29 ----- tests/Unit/DeliveryinfoTest.php | 69 +++++----- 15 files changed, 148 insertions(+), 237 deletions(-) create mode 100644 database/migrations/2018_06_11_033330_add_column_to_table_delivery_info.php create mode 100644 database/seeds/DeliveryInfoSeeder.php delete mode 100644 routes/.api.php.swp delete mode 100644 routes/.api_BASE_14556.php.swp delete mode 100644 routes/.api_LOCAL_14556.php.swp delete mode 100644 routes/.api_REMOTE_14556.php.swp delete mode 100644 routes/api_BACKUP_14556.php delete mode 100644 routes/api_BASE_14556.php delete mode 100644 routes/api_LOCAL_14556.php delete mode 100644 routes/api_REMOTE_14556.php diff --git a/app/DeliveryInfo.php b/app/DeliveryInfo.php index 264ee64..150dbaf 100644 --- a/app/DeliveryInfo.php +++ b/app/DeliveryInfo.php @@ -19,5 +19,10 @@ class Deliveryinfo extends Model 'contact_person_no', ]; + public function company() { + + return $this->belongsTo(App\Company); + + } } diff --git a/app/Http/Controllers/DeliveryInfoController.php b/app/Http/Controllers/DeliveryInfoController.php index 7602f62..56a59cb 100644 --- a/app/Http/Controllers/DeliveryInfoController.php +++ b/app/Http/Controllers/DeliveryInfoController.php @@ -8,103 +8,95 @@ use App\Deliveryinfo; 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); } } diff --git a/database/migrations/2018_04_25_024318_create_deliveryinfos_table.php b/database/migrations/2018_04_25_024318_create_deliveryinfos_table.php index 9914edc..c8dfc96 100644 --- a/database/migrations/2018_04_25_024318_create_deliveryinfos_table.php +++ b/database/migrations/2018_04_25_024318_create_deliveryinfos_table.php @@ -27,6 +27,7 @@ class CreateDeliveryinfosTable extends Migration $table->integer('contact_person_no'); $table->timestamps(); + }); } diff --git a/database/migrations/2018_06_11_033330_add_column_to_table_delivery_info.php b/database/migrations/2018_06_11_033330_add_column_to_table_delivery_info.php new file mode 100644 index 0000000..adc10de --- /dev/null +++ b/database/migrations/2018_06_11_033330_add_column_to_table_delivery_info.php @@ -0,0 +1,37 @@ +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) { + // + }); + } +} diff --git a/database/seeds/DeliveryInfoSeeder.php b/database/seeds/DeliveryInfoSeeder.php new file mode 100644 index 0000000..9bab6a5 --- /dev/null +++ b/database/seeds/DeliveryInfoSeeder.php @@ -0,0 +1,16 @@ +7|8`?z#hyW2F0z`la z5CI}U1c<;}Nx)=d><%)2r_R1vU&mg!)*tCY1c(3;AOb{y2oM1xKm>>Y5g-CYfC#*W z1pJV(Ywt1Ed>_f<|Nq0k|Ns4nv45aP&_n0}^gVP3YC+S`-ybseE9664&<3;)U4-6+ zo}OgvFX&I`H%LR*p(f-)PfjrQ82SOa3oSw|=;;TH-G}Z$e?WJkEy#sVL(h=w@6a#M z4irFJkgdUmXb=G+Km>>Y5g-CYfCwBHflf7eyVVZbp?_vJYPYA_t+`ed;fqMjM04G# zaBe=DiDu_#qiJ?~Vv%u-;nm)2VG5n%l4mN-MPtJ4Z09O%tUHlX&Qize`r6s7H(s^} zLl+i$!ZaM55H-$e9u;X|BsNU!GwV0HBvCzVz#?b4F!5I^HCiQ!(C6z3xDJ=&$Z3X{ zyD|(D(f74*_)>?jJKKHJ^s(95+#HR?_t|i3rgCJXMWFVCeo3t1%t5ef2Xc_jhxBogx>yQBK!oAC%j0Y?TW*a_is;lz=_E ze1tG>T&|4x&~XA7mZ~wN5sE|@(QxeHT`y3(nV$~4ecZS*yzODum1(4G*()NEAU0UT z=aENK8926UFW8)r_Zo?LeCuWqe% zo41yhZa0!3IW{pSK>!3!KwyRzZ#E~d`nB>LU!Hw@g1uM@0w4eaAOHd&00JNY0w8eK z3Am(02U5hjqLA5QEd4bWw-`VG1V8`;KmY_l00ck)1V8`;KmY{JAOYnQ9nKJa6XpN^ zkG}tZULg7*`Xu@&`XD+K9f%f1P0^S0MDImP)D?9^&qS9*-wV&Lxi10)KmY_l00ck) z1V8`;KmY_l;J*`iRa+*Ou-aud;_FsF&vfeQD_N>-RVFv{PV81|Xna4^BV~29qB0k> zq&2%ztFH{St5&>xapeYYXHnmoIO6K&)aJKejb9SYd$dpcY*JQfY6g+^*#(-*VpZlp z33wpg-${elqMdlmz08EZi3U85Gs~+R?K!cSwuiHvhQnC-V$>T?ye6mJ(ss}E^@ff2 zjIS+^j0;S}YHG}*!1Mz#S=X{%4l@e!3$f?EiUh>Kq)v88s-ryHv7M$@NfMgA%4=-! xqd=!Ruc&@%A*agIPO3;x)!3USlBdM&FwBM~Qcic)ol82!gOcw4yZ7?;`d>HG%YOg> diff --git a/routes/.api_LOCAL_14556.php.swp b/routes/.api_LOCAL_14556.php.swp deleted file mode 100644 index b8d71b580208a59042d07bd3117f74a25088aab2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2%T5$Q6ow18ir1BiCI>VbAoetf7&91@fRRZs61*&wgf!DN(*-?UZFM!{$n6DO z=vL#-hj8ypn5Yk+E_C5WSN=WI$R&{wU?It=ApfQ>uE8mc&wS8_h@JGS-tCwEMM2`~XBzyz286JP>NfC(@GCcp%kz#$~y z`9i!qD#XMwB#;09*MI;2J|V;}=o9o2`T#wL)}TRX0Qz!Vh_{dj%|bKKW9St0vr~v4 z(0AxPv<9t0!%zwOhIn70*U&2{i+>y9#sruE6JP>NfC(@GCcp%k!2eHRI(xudD)};$ zR`wLKm%MI8*JCegx=R>1%3pCY>!_%z3zw)cE!%&)=ZG%EDo*(Cy zaccMBm&U$l_VvZI2p%QcrT7aRq?VKi*!GbiOec;4y+-o nENrWaQR1U+mt|HulnF(v-nf#z|y{_NLUf2HtTfkui!WELzt+w!OG5P<$vyW10hH#XfSV*e`fFIz5ko{o4tZC)q%%T)BJXMfMJ|t z?CqOxPu^ebVwXo5YkI4^AtJkS=s;ZQC~+?`L-&D5R*i~X`K%?^N?t5otx_Wr7mv%c z6VngpCYA?p4iDcd$3c8(X<8%#MBvB-y4lc;!S+x6YF{tEaDM*CnG{F_hyW2F0z`la z5CI}U1c<=NCt%_(wuLrMWvyJv=B|Bn_K_wcKm>>Y5g-CYfCvx)B0vO)01+SpMBoGx z@O;L8on~zN3^tGd|L1@I|LI}uH}n1d$nu9Ju-%;-q^bUFj+4?Ub8bp8y5CI}U1c(3;AOb{y2>ka1o)cSKp+$1*6b-t2DH6xKw z++fykZ%C->NI;M?o|@pkii}oaDD=H-0j_yCt|rH*xh8!-6irVHhmTeGg41Z4vWH}6 zX=!gQzQOX;SS8qv7B#gl^Z~W z&|cHxX$n}SN}32=XfJ1N@3vcTY?mXrn(g2O+JIdfzK1X`%yx`;?pU-K`dzlkHn~i= zmn5B<* zZLjuj9|^rKYhp&Lb?FPuo6-a_;$Fv?Hv?G6(l@BNiu(i5Ihj!uxRN IuDx9U7humv-~a#s diff --git a/routes/api.php b/routes/api.php index 42a0098..39f9f85 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,17 +13,6 @@ use Illuminate\Http\Request; | */ -/* Routes for the DeliveryInfo in profile*/ -Route::get('/company/deliveryinfo','DeliveryinfoController@index'); -Route::get('/company/{com_id}/deliveryinfo','DeliveryinfoController@show'); - -Route::post('/company/deliveryinfo', 'DeliveryinfoController@store'); -Route::put('/company/{com_id}/deliveryinfo', 'DeliveryinfoController@update'); -Route::delete('/company/{com_id}/deliveryinfo', 'DeliveryinfoController@destroy'); - - - - @@ -42,4 +31,10 @@ Route::group(['middleware' => ['jwt.auth']], function() { Route::get('test', function(){ return response()->json(['foo'=>'bar']); }); + + /*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'); }); diff --git a/routes/api_BACKUP_14556.php b/routes/api_BACKUP_14556.php deleted file mode 100644 index 4be34e1..0000000 --- a/routes/api_BACKUP_14556.php +++ /dev/null @@ -1,46 +0,0 @@ -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 diff --git a/routes/api_BASE_14556.php b/routes/api_BASE_14556.php deleted file mode 100644 index c641ca5..0000000 --- a/routes/api_BASE_14556.php +++ /dev/null @@ -1,18 +0,0 @@ -get('/user', function (Request $request) { - return $request->user(); -}); diff --git a/routes/api_LOCAL_14556.php b/routes/api_LOCAL_14556.php deleted file mode 100644 index d91cd5f..0000000 --- a/routes/api_LOCAL_14556.php +++ /dev/null @@ -1,29 +0,0 @@ -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'); diff --git a/routes/api_REMOTE_14556.php b/routes/api_REMOTE_14556.php deleted file mode 100644 index aff41ed..0000000 --- a/routes/api_REMOTE_14556.php +++ /dev/null @@ -1,29 +0,0 @@ -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']); - }); -}); \ No newline at end of file diff --git a/tests/Unit/DeliveryinfoTest.php b/tests/Unit/DeliveryinfoTest.php index 8d927f2..015b741 100644 --- a/tests/Unit/DeliveryinfoTest.php +++ b/tests/Unit/DeliveryinfoTest.php @@ -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); + } + + }