Browse Source

Add vendor owner relation

pull/1/head
Nafies Luthfi 8 years ago
parent
commit
a8dbd96304
  1. 7
      app/Entities/Partners/Vendor.php
  2. 10
      app/Http/Controllers/Partners/VendorsController.php
  3. 14
      app/Policies/Partners/VendorPolicy.php
  4. 6
      database/factories/VendorFactory.php
  5. 5
      database/migrations/2017_11_01_185745_create_vendors_table.php
  6. 3
      tests/Feature/ManageVendorsTest.php
  7. 10
      tests/Unit/Models/VendorTest.php
  8. 6
      tests/Unit/Policies/VendorPolicyTest.php

7
app/Entities/Partners/Vendor.php

@ -6,5 +6,10 @@ use Illuminate\Database\Eloquent\Model;
class Vendor extends Model
{
protected $fillable = ['name', 'description'];
protected $fillable = ['name', 'description', 'owner_id'];
public function owner()
{
return $this->belongsTo('App\Entities\Agencies\Agency', 'owner_id');
}
}

10
app/Http/Controllers/Partners/VendorsController.php

@ -35,12 +35,14 @@ class VendorsController extends Controller
*/
public function store(Request $request)
{
$this->validate($request, [
$newVendorData = $this->validate($request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
Vendor::create($request->only('name', 'description'));
$newVendorData['owner_id'] = auth()->user()->agency->id;
Vendor::create($newVendorData);
flash(trans('vendor.created'), 'success');
return redirect()->route('vendors.index');
@ -55,14 +57,14 @@ class VendorsController extends Controller
*/
public function update(Request $request, Vendor $vendor)
{
$this->validate($request, [
$vendorData = $this->validate($request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
$routeParam = request()->only('page', 'q');
$vendor = $vendor->update($request->only('name', 'description'));
$vendor = $vendor->update($vendorData);
flash(trans('vendor.updated'), 'success');
return redirect()->route('vendors.index', $routeParam);

14
app/Policies/Partners/VendorPolicy.php

@ -2,8 +2,8 @@
namespace App\Policies\Partners;
use App\Entities\Users\User;
use App\Entities\Partners\Vendor;
use App\Entities\Users\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class VendorPolicy
@ -19,8 +19,7 @@ class VendorPolicy
*/
public function view(User $user, Vendor $vendor)
{
// Update $user authorization to view $vendor here.
return true;
return $user->agency->id == $vendor->owner_id;
}
/**
@ -32,8 +31,7 @@ class VendorPolicy
*/
public function create(User $user, Vendor $vendor)
{
// Update $user authorization to create $vendor here.
return true;
return ! ! $user->agency;
}
/**
@ -45,8 +43,7 @@ class VendorPolicy
*/
public function update(User $user, Vendor $vendor)
{
// Update $user authorization to update $vendor here.
return true;
return $this->view($user, $vendor);
}
/**
@ -58,7 +55,6 @@ class VendorPolicy
*/
public function delete(User $user, Vendor $vendor)
{
// Update $user authorization to delete $vendor here.
return true;
return $this->view($user, $vendor);
}
}

6
database/factories/VendorFactory.php

@ -1,12 +1,16 @@
<?php
use App\Entities\Agencies\Agency;
use App\Entities\Partners\Vendor;
use Faker\Generator as Faker;
$factory->define(Vendor::class, function (Faker $faker) {
return [
'name' => $faker->word,
'name' => $faker->word,
'description' => $faker->sentence,
'owner_id' => function () {
return factory(Agency::class)->create()->id;
},
];
});

5
database/migrations/2017_11_01_185745_create_vendors_table.php

@ -1,8 +1,8 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateVendorsTable extends Migration
{
@ -17,6 +17,7 @@ class CreateVendorsTable extends Migration
$table->increments('id');
$table->string('name', 60);
$table->string('description')->nullable();
$table->unsignedInteger('owner_id');
$table->timestamps();
});
}

3
tests/Feature/ManageVendorsTest.php

@ -35,6 +35,7 @@ class ManageVendorsTest extends TestCase
$this->type('Vendor 1 description', 'description');
$this->press(trans('vendor.create'));
$this->see(trans('vendor.created'));
$this->seePageIs(route('vendors.index'));
$this->seeInDatabase('vendors', [
@ -57,6 +58,7 @@ class ManageVendorsTest extends TestCase
$this->type('Vendor 1 description', 'description');
$this->press(trans('vendor.update'));
$this->see(trans('vendor.updated'));
$this->seePageIs(route('vendors.index', ['q' => '123']));
$this->seeInDatabase('vendors', [
@ -81,6 +83,7 @@ class ManageVendorsTest extends TestCase
$this->press(trans('app.delete_confirm_button'));
$this->see(trans('vendor.deleted'));
$this->dontSeeInDatabase('vendors', [
'id' => $vendor->id,
]);

10
tests/Unit/Models/VendorTest.php

@ -2,6 +2,7 @@
namespace Tests\Unit\Models;
use App\Entities\Agencies\Agency;
use App\Entities\Partners\Vendor;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase as TestCase;
@ -11,9 +12,12 @@ class VendorTest extends TestCase
use DatabaseMigrations;
/** @test */
public function it_has_name_attribute()
public function a_vendor_belongs_to_an_agency_as_its_owner()
{
$vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']);
$this->assertEquals('Vendor 1 name', $vendor->name);
$vendor = factory(Vendor::class)->create();
$this->assertTrue(
$vendor->owner instanceof Agency,
'A vendor must belongs to an App\Entities\Agencies\Agency model as its owner.'
);
}
}

6
tests/Unit/Policies/VendorPolicyTest.php

@ -21,7 +21,7 @@ class VendorPolicyTest extends TestCase
public function user_can_view_vendor()
{
$user = $this->adminUserSigningIn();
$vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']);
$vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]);
$this->assertTrue($user->can('view', $vendor));
}
@ -29,7 +29,7 @@ class VendorPolicyTest extends TestCase
public function user_can_update_vendor()
{
$user = $this->adminUserSigningIn();
$vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']);
$vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]);
$this->assertTrue($user->can('update', $vendor));
}
@ -37,7 +37,7 @@ class VendorPolicyTest extends TestCase
public function user_can_delete_vendor()
{
$user = $this->adminUserSigningIn();
$vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']);
$vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]);
$this->assertTrue($user->can('delete', $vendor));
}
}
Loading…
Cancel
Save