Skip to content

Commit

Permalink
- Composer update
Browse files Browse the repository at this point in the history
- Create method update() on user UpdateRequest class
- Modify feature tests
- Create branch module on master page (WIP)
  • Loading branch information
ianriizky committed Feb 4, 2022
1 parent de35855 commit 6542d20
Show file tree
Hide file tree
Showing 19 changed files with 541 additions and 77 deletions.
29 changes: 3 additions & 26 deletions app/Http/Controllers/BranchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use App\Http\Requests\Branch\UpdateRequest;
use App\Http\Resources\DataTables\BranchResource;
use App\Models\Branch;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Yajra\DataTables\Facades\DataTables;

class BranchController extends Controller
Expand Down Expand Up @@ -46,11 +44,7 @@ public function datatable()

return DataTables::eloquent(Branch::query())
->setTransformer(fn ($model) => BranchResource::make($model)->resolve())
->orderColumn('branch_name', function ($query, $direction) {
$query->orderBy('branches.name', $direction);
})->filterColumn('branch_name', function ($query, $keyword) {
$query->where('branches.name', 'like', '%' . $keyword . '%');
})->toJson();
->toJson();
}

/**
Expand All @@ -71,16 +65,7 @@ public function create()
*/
public function store(StoreRequest $request)
{
/** @var \App\Models\Branch $branch */
$branch = Branch::make($request->validated())->setBranchRelationValue(
$request->getBranch()
);

$branch->save();

$branch->syncRoles($request->input('role'));

Event::dispatch(new Registered($branch));
$request->store();

return redirect()->route('master.branch.index')->with([
'alert' => [
Expand Down Expand Up @@ -109,8 +94,6 @@ public function show(Branch $branch)
*/
public function edit(Branch $branch)
{
$branch->append('role');

return view('master.branch.edit', compact('branch'));
}

Expand All @@ -123,13 +106,7 @@ public function edit(Branch $branch)
*/
public function update(UpdateRequest $request, Branch $branch)
{
$branch = $branch->fill($request->validated())->setBranchRelationValue(
$request->getBranch()
);

$branch->save();

$branch->syncRoles($request->input('role'));
$branch = $request->update($branch);

return redirect()->route('master.branch.edit', $branch)->with([
'alert' => [
Expand Down
8 changes: 1 addition & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ public function edit(User $user)
*/
public function update(UpdateRequest $request, User $user)
{
$user = $user->fill($request->validated())->setBranchRelationValue(
$request->getBranch()
);

$user->save();

$user->syncRoles($request->input('role'));
$user = $request->update($user)->syncRoles($request->input('role'));

return redirect()->route('master.user.edit', $user)->with([
'alert' => [
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Requests/Branch/AbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Requests\Branch;

use App\Infrastructure\Foundation\Http\FormRequest;

abstract class AbstractRequest extends FormRequest
{
/**
* {@inheritDoc}
*/
public function authorize()
{
return !is_null($this->user());
}

/**
* {@inheritDoc}
*/
public function attributes()
{
return [
'name' => trans('Name'),
'address' => trans('Address'),
];
}
}
37 changes: 37 additions & 0 deletions app/Http/Requests/Branch/StoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Requests\Branch;

use App\Models\Branch;
use App\Models\Role;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules;

class StoreRequest extends AbstractRequest
{
/**
* {@inheritDoc}
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'address' => 'required|string',
];
}

/**
* Store a newly created resource in storage.
*
* @return \App\Models\Branch
*/
public function store(): Branch
{
/** @var \App\Models\Branch $branch */
$branch = Branch::make($this->validated());

$branch->save();

return $branch;
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/Branch/UpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests\Branch;

use App\Models\Branch;

class UpdateRequest extends AbstractRequest
{
/**
* {@inheritDoc}
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'address' => 'required|string',
];
}

/**
* Update the specified resource in storage.
*
* @param \App\Models\Branch $branch
* @return \App\Models\Branch
*/
public function update(Branch $branch): Branch
{
$branch->update($this->validated());

return $branch;
}
}
17 changes: 17 additions & 0 deletions app/Http/Requests/User/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ public function rules()
'role' => ['required', Rule::exists(Role::class, 'name')],
];
}

/**
* Update the specified resource in storage.
*
* @param \App\Models\User $user
* @return \App\Models\User
*/
public function update(User $user): User
{
$user = $user->fill($this->validated())->setBranchRelationValue(
$this->getBranch()
);

$user->save();

return $user;
}
}
46 changes: 46 additions & 0 deletions app/Http/Resources/DataTables/BranchResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Resources\DataTables;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @property \App\Models\Branch $resource
*/
class BranchResource extends JsonResource
{
/**
* {@inheritDoc}
*/
public function toArray($request)
{
$elements = [];

if ($request->user()->can('view', $this->resource)) {
$elements[] = view('components.datatables.link-show', [
'url' => route('master.branch.show', $this->resource),
])->render();
}

if ($request->user()->can('update', $this->resource)) {
$elements[] = view('components.datatables.link-edit', [
'url' => route('master.branch.edit', $this->resource),
])->render();
}

if ($request->user()->can('delete', $this->resource)) {
$elements[] = view('components.datatables.link-destroy', [
'url' => route('master.branch.destroy', $this->resource),
])->render();
}

return [
'checkbox' => view('components.datatables.checkbox', [
'value' => $this->resource->getKey(),
])->render(),
'name' => $this->resource->name,
'address' => $this->resource->address,
'action' => view('components.datatables.button-group', compact('elements'))->render(),
];
}
}
109 changes: 109 additions & 0 deletions app/Policies/BranchPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Policies;

use App\Models\Branch;
use App\Models\Role;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class BranchPolicy
{
use HandlesAuthorization;

/**
* Perform pre-authorization checks.
*
* @param \App\Models\User $user
* @param string $ability
* @return void|bool
*/
public function before(User $user, $ability)
{
if ($user->hasRole(Role::ROLE_ADMIN)) {
return true;
}
}

/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
return $user->hasRole(Role::ROLE_STAFF);
}

/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\Branch $branch
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, Branch $branch)
{
return $user->branch->is($branch);
}

/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
return false;
}

/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\Branch $branch
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, Branch $branch)
{
return false;
}

/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Branch $branch
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, Branch $branch)
{
return false;
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\Branch $branch
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, Branch $branch)
{
return false;
}

/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Branch $branch
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, Branch $branch)
{
return false;
}
}
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6542d20

Please sign in to comment.