diff --git a/app/Entities/Partners/Vendor.php b/app/Entities/Partners/Vendor.php new file mode 100644 index 0000000..8188df1 --- /dev/null +++ b/app/Entities/Partners/Vendor.php @@ -0,0 +1,10 @@ +where('name', 'like', '%'.request('q').'%'); + })->paginate(25); + + if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { + $editableVendor = Vendor::find(request('id')); + } + + return view('vendors.index', compact('vendors', 'editableVendor')); + } + + /** + * Store a newly created vendor in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $this->validate($request, [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]); + + Vendor::create($request->only('name', 'description')); + + flash(trans('vendor.created'), 'success'); + return redirect()->route('vendors.index'); + } + + /** + * Update the specified vendor in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Entities\Partners\Vendor $vendor + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Vendor $vendor) + { + $this->validate($request, [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]); + + $routeParam = request()->only('page', 'q'); + + $vendor = $vendor->update($request->only('name', 'description')); + + flash(trans('vendor.updated'), 'success'); + return redirect()->route('vendors.index', $routeParam); + } + + /** + * Remove the specified vendor from storage. + * + * @param \App\Entities\Partners\Vendor $vendor + * @return \Illuminate\Http\Response + */ + public function destroy(Vendor $vendor) + { + $this->validate(request(), [ + 'vendor_id' => 'required', + ]); + + $routeParam = request()->only('page', 'q'); + + if (request('vendor_id') == $vendor->id && $vendor->delete()) { + flash(trans('vendor.deleted'), 'warning'); + return redirect()->route('vendors.index', $routeParam); + } + + flash(trans('vendor.undeleted'), 'danger'); + return back(); + } +} diff --git a/app/Policies/Partners/VendorPolicy.php b/app/Policies/Partners/VendorPolicy.php new file mode 100644 index 0000000..1df1785 --- /dev/null +++ b/app/Policies/Partners/VendorPolicy.php @@ -0,0 +1,64 @@ + 'App\Policies\Partners\VendorPolicy', 'App\Entities\Partners\Customer' => 'App\Policies\Partners\CustomerPolicy', 'App\Entities\Projects\Project' => 'App\Policies\Projects\ProjectPolicy', 'App\Entities\Agencies\Agency' => 'App\Policies\AgencyPolicy', diff --git a/database/factories/VendorFactory.php b/database/factories/VendorFactory.php new file mode 100644 index 0000000..f4c6db9 --- /dev/null +++ b/database/factories/VendorFactory.php @@ -0,0 +1,12 @@ +define(Vendor::class, function (Faker $faker) { + + return [ + 'name' => $faker->word, + 'description' => $faker->sentence, + ]; +}); diff --git a/database/migrations/2017_11_01_185745_create_vendors_table.php b/database/migrations/2017_11_01_185745_create_vendors_table.php new file mode 100644 index 0000000..124e02c --- /dev/null +++ b/database/migrations/2017_11_01_185745_create_vendors_table.php @@ -0,0 +1,33 @@ +increments('id'); + $table->string('name', 60); + $table->string('description')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('vendors'); + } +} diff --git a/resources/lang/id/vendor.php b/resources/lang/id/vendor.php new file mode 100644 index 0000000..5a04da7 --- /dev/null +++ b/resources/lang/id/vendor.php @@ -0,0 +1,29 @@ + 'Vendor', + 'list' => 'Daftar Vendor', + 'search' => 'Cari Vendor', + 'not_found' => 'Vendor tidak ditemukan', + 'empty' => 'Belum ada Vendor', + 'back_to_show' => 'Kembali ke detail Vendor', + 'back_to_index' => 'Kembali ke daftar Vendor', + + // Actions + 'create' => 'Input Vendor Baru', + 'created' => 'Input Vendor baru telah berhasil.', + 'show' => 'Detail Vendor', + 'edit' => 'Edit Vendor', + 'update' => 'Update Vendor', + 'updated' => 'Update data Vendor telah berhasil.', + 'delete' => 'Hapus Vendor', + 'delete_confirm' => 'Anda yakin akan menghapus Vendor ini?', + 'deleted' => 'Hapus data Vendor telah berhasil.', + 'undeleted' => 'Data Vendor gagal dihapus.', + 'undeleteable' => 'Data Vendor tidak dapat dihapus.', + + // Attributes + 'name' => 'Nama Vendor', + 'description' => 'Deskripsi Vendor', +]; diff --git a/resources/views/vendors/forms.blade.php b/resources/views/vendors/forms.blade.php new file mode 100644 index 0000000..cc49fa4 --- /dev/null +++ b/resources/views/vendors/forms.blade.php @@ -0,0 +1,47 @@ +@if (Request::get('action') == 'create') + {!! Form::open(['route' => 'vendors.store']) !!} + {!! FormField::text('name', ['required' => true]) !!} + {!! FormField::textarea('description') !!} + {!! Form::submit(trans('vendor.create'), ['class' => 'btn btn-success']) !!} + {{ link_to_route('vendors.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} + {!! Form::close() !!} +@endif +@if (Request::get('action') == 'edit' && $editableVendor) + {!! Form::model($editableVendor, ['route' => ['vendors.update', $editableVendor->id],'method' => 'patch']) !!} + {!! FormField::text('name', ['required' => true]) !!} + {!! FormField::textarea('description') !!} + @if (request('q')) + {{ Form::hidden('q', request('q')) }} + @endif + @if (request('page')) + {{ Form::hidden('page', request('page')) }} + @endif + {!! Form::submit(trans('vendor.update'), ['class' => 'btn btn-success']) !!} + {{ link_to_route('vendors.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} + {!! Form::close() !!} +@endif +@if (Request::get('action') == 'delete' && $editableVendor) +
+

{{ trans('vendor.delete') }}

+
+ +

{{ $editableVendor->name }}

+ {!! $errors->first('vendor_id', ':message') !!} +
+
+
{{ trans('app.delete_confirm') }}
+ +
+@endif diff --git a/resources/views/vendors/index.blade.php b/resources/views/vendors/index.blade.php new file mode 100644 index 0000000..f710b1a --- /dev/null +++ b/resources/views/vendors/index.blade.php @@ -0,0 +1,63 @@ +@extends('layouts.app') + +@section('title', trans('vendor.list')) + +@section('content') +

+
+ {{ link_to_route('vendors.index', trans('vendor.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }} +
+ {{ trans('vendor.list') }} + {{ trans('app.total') }} : {{ $vendors->total() }} {{ trans('vendor.vendor') }} +

+
+
+
+
+ {{ Form::open(['method' => 'get','class' => 'form-inline']) }} + {!! FormField::text('q', ['value' => request('q'), 'label' => trans('vendor.search'), 'class' => 'input-sm']) !!} + {{ Form::submit(trans('vendor.search'), ['class' => 'btn btn-sm']) }} + {{ link_to_route('vendors.index', trans('app.reset')) }} + {{ Form::close() }} +
+ + + + + + + + + + + @foreach($vendors as $key => $vendor) + + + + + + + @endforeach + +
{{ trans('app.table_no') }}{{ trans('vendor.name') }}{{ trans('vendor.description') }}{{ trans('app.action') }}
{{ $vendors->firstItem() + $key }}{{ $vendor->name }}{{ $vendor->description }} + {!! link_to_route( + 'vendors.index', + trans('app.edit'), + ['action' => 'edit', 'id' => $vendor->id] + Request::only('page', 'q'), + ['id' => 'edit-vendor-' . $vendor->id] + ) !!} | + {!! link_to_route( + 'vendors.index', + trans('app.delete'), + ['action' => 'delete', 'id' => $vendor->id] + Request::only('page', 'q'), + ['id' => 'del-vendor-' . $vendor->id] + ) !!} +
+
{{ $vendors->appends(Request::except('page'))->render() }}
+
+
+
+ @includeWhen(Request::has('action'), 'vendors.forms') +
+
+@endsection diff --git a/routes/web.php b/routes/web.php index e5c95a0..d6dc8ec 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,4 +25,9 @@ Route::group(['middleware' => ['web', 'auth']], function () { * Customers Routes */ Route::resource('customers', 'Partners\CustomersController'); + + /* + * Vendors Routes + */ + Route::apiResource('vendors', 'Partners\VendorsController'); }); diff --git a/tests/Feature/ManageVendorsTest.php b/tests/Feature/ManageVendorsTest.php new file mode 100644 index 0000000..6bdc07d --- /dev/null +++ b/tests/Feature/ManageVendorsTest.php @@ -0,0 +1,88 @@ +create(['name' => 'Testing name', 'description' => 'Testing 123']); + $vendor2 = factory(Vendor::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + + $this->adminUserSigningIn(); + $this->visit(route('vendors.index')); + $this->see($vendor1->name); + $this->see($vendor2->name); + } + + /** @test */ + public function user_can_create_a_vendor() + { + $this->adminUserSigningIn(); + $this->visit(route('vendors.index')); + + $this->click(trans('vendor.create')); + $this->seePageIs(route('vendors.index', ['action' => 'create'])); + + $this->type('Vendor 1 name', 'name'); + $this->type('Vendor 1 description', 'description'); + $this->press(trans('vendor.create')); + + $this->seePageIs(route('vendors.index')); + + $this->seeInDatabase('vendors', [ + 'name' => 'Vendor 1 name', + 'description' => 'Vendor 1 description', + ]); + } + + /** @test */ + public function user_can_edit_a_vendor_within_search_query() + { + $this->adminUserSigningIn(); + $vendor = factory(Vendor::class)->create(['name' => 'Testing 123']); + + $this->visit(route('vendors.index', ['q' => '123'])); + $this->click('edit-vendor-'.$vendor->id); + $this->seePageIs(route('vendors.index', ['action' => 'edit', 'id' => $vendor->id, 'q' => '123'])); + + $this->type('Vendor 1 name', 'name'); + $this->type('Vendor 1 description', 'description'); + $this->press(trans('vendor.update')); + + $this->seePageIs(route('vendors.index', ['q' => '123'])); + + $this->seeInDatabase('vendors', [ + 'name' => 'Vendor 1 name', + 'description' => 'Vendor 1 description', + ]); + } + + /** @test */ + public function user_can_delete_a_vendor() + { + $this->adminUserSigningIn(); + $vendor = factory(Vendor::class)->create(); + + $this->visit(route('vendors.index', [$vendor->id])); + $this->click('del-vendor-'.$vendor->id); + $this->seePageIs(route('vendors.index', ['action' => 'delete', 'id' => $vendor->id])); + + $this->seeInDatabase('vendors', [ + 'id' => $vendor->id, + ]); + + $this->press(trans('app.delete_confirm_button')); + + $this->dontSeeInDatabase('vendors', [ + 'id' => $vendor->id, + ]); + } +} diff --git a/tests/Unit/Models/VendorTest.php b/tests/Unit/Models/VendorTest.php new file mode 100644 index 0000000..4f968bd --- /dev/null +++ b/tests/Unit/Models/VendorTest.php @@ -0,0 +1,19 @@ +create(['name' => 'Vendor 1 name']); + $this->assertEquals('Vendor 1 name', $vendor->name); + } +} diff --git a/tests/Unit/Policies/VendorPolicyTest.php b/tests/Unit/Policies/VendorPolicyTest.php new file mode 100644 index 0000000..427a98f --- /dev/null +++ b/tests/Unit/Policies/VendorPolicyTest.php @@ -0,0 +1,43 @@ +adminUserSigningIn(); + $this->assertTrue($user->can('create', new Vendor)); + } + + /** @test */ + public function user_can_view_vendor() + { + $user = $this->adminUserSigningIn(); + $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $this->assertTrue($user->can('view', $vendor)); + } + + /** @test */ + public function user_can_update_vendor() + { + $user = $this->adminUserSigningIn(); + $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $this->assertTrue($user->can('update', $vendor)); + } + + /** @test */ + public function user_can_delete_vendor() + { + $user = $this->adminUserSigningIn(); + $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $this->assertTrue($user->can('delete', $vendor)); + } +}