Skip to content

Commit

Permalink
- Modify model relationship
Browse files Browse the repository at this point in the history
- Creating achievement module on monitoring page (WIP)
  • Loading branch information
ianriizky committed Feb 10, 2022
1 parent a8c9102 commit 82aed66
Show file tree
Hide file tree
Showing 30 changed files with 647 additions and 176 deletions.
27 changes: 24 additions & 3 deletions app/Http/Controllers/AchievementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Resources\DataTables\AchievementResource;
use App\Models\Achievement;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -32,15 +33,35 @@ public function index()
/**
* Return datatable server side response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function datatable()
public function datatable(Request $request)
{
$this->authorize('viewAny', Achievement::class);

return DataTables::eloquent(Achievement::query())
if ($request->user()->isStaff()) {
$query = $request->user()->branch->achievements();
} else {
$query = Achievement::query();
}

$query
->join('branches', 'targets.branch_id', '=', 'branches.id')
->leftJoin('events', 'achievements.event_id', '=', 'events.id')
->select('achievements.*', 'branches.name as branch_name', 'events.name as event_name');

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

/**
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/TargetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use App\Http\Requests\Target\StoreRequest;
use App\Http\Requests\Target\UpdateRequest;
use App\Http\Resources\DataTables\TargetResource;
use App\Models\Branch;
use App\Models\Target;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
Expand Down Expand Up @@ -51,7 +49,7 @@ public function datatable(Request $request)
}

$query
->join((new Branch)->getTable(), 'targets.branch_id', '=', 'branches.id')
->join('branches', 'targets.branch_id', '=', 'branches.id')
->select('targets.*', 'branches.name as branch_name');

return DataTables::eloquent($query)
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Http\Requests\User\StoreRequest;
use App\Http\Requests\User\UpdateRequest;
use App\Http\Resources\DataTables\UserResource;
use App\Models\Branch;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -45,11 +44,12 @@ public function datatable()
{
$this->authorize('viewAny', User::class);

return DataTables::eloquent(
User::query()
->join((new Branch)->getTable(), 'users.branch_id', '=', 'branches.id')
->select('users.*', 'branches.name as branch_name')
)->setTransformer(fn ($model) => UserResource::make($model)->resolve())
$query = User::query()
->join('branches', 'users.branch_id', '=', 'branches.id')
->select('users.*', 'branches.name as branch_name');

return DataTables::eloquent($query)
->setTransformer(fn ($model) => UserResource::make($model)->resolve())
->orderColumn('branch_name', function ($query, $direction) {
$query->orderBy('branches.name', $direction);
})->filterColumn('branch_name', function ($query, $keyword) {
Expand Down
58 changes: 58 additions & 0 deletions app/Http/Resources/DataTables/AchievementResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Resources\DataTables;

use Illuminate\Http\Resources\Json\JsonResource;

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

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

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

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

return [
'checkbox' => view('components.datatables.checkbox', [
'value' => $this->resource->getKey(),
])->render(),
'branch_name' => view('components.datatables.link', [
'url' => route('master.branch.show', $this->resource->target->branch),
'name' => $this->resource->target->branch->name,
])->render(),
'event_name' => view('components.datatables.link', [
'url' => $this->resource->event ? route('monitoring.event.show', $this->resource->event->branch) : '#',
'name' => $this->resource->event ? $this->resource->event->name : '-',
])->render(),
'achieved_date' => $this->resource->achieved_date_iso_format,
'achieved_by' => view('components.datatables.link', [
'url' => $this->resource->achievedBy ? route('master.user.show', $this->resource->achievedBy) : '#',
'name' => $this->resource->achievedBy ? $this->resource->achievedBy->name : '-',
])->render(),
'amount' => $this->resource->getRawAttribute('amount'),
'action' => view('components.datatables.button-group', compact('elements'))->render(),
];
}
}
3 changes: 0 additions & 3 deletions app/Infrastructure/Contracts/Model/HasCreatedByAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property \App\Models\User $created_by
*/
interface HasCreatedByAttribute
{
/**
Expand Down
2 changes: 1 addition & 1 deletion app/Infrastructure/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Infrastructure\Database\Eloquent;

use App\Support\Model\HasAttributes;
use App\Support\Models\HasAttributes;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model as BaseModel;

Expand Down
31 changes: 31 additions & 0 deletions app/Infrastructure/Models/Relation/BelongsToBranch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Infrastructure\Models\Relation;

use App\Models\Branch;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

interface BelongsToBranch
{
/**
* Define an inverse one-to-many relationship with \App\Models\Branch.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function branch(): BelongsTo;

/**
* Return \App\Models\Branch model relation value.
*
* @return \App\Models\Branch|null
*/
public function getBranchRelationValue();

/**
* Set \App\Models\Branch model relation value.
*
* @param \App\Models\Branch $branch
* @return $this
*/
public function setBranchRelationValue(Branch $branch);
}
31 changes: 31 additions & 0 deletions app/Infrastructure/Models/Relation/HasManyAchievements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Infrastructure\Models\Relation;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;

interface HasManyAchievements
{
/**
* Define a one-to-many relationship with \App\Models\Achievement.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function achievements(): HasMany;

/**
* Return collection of \App\Models\Achievement model relation value.
*
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Achievement>
*/
public function getAchievementsRelationValue(): Collection;

/**
* Set collection of \App\Models\Achievement model relation value.
*
* @param \Illuminate\Database\Eloquent\Collection<\App\Models\Achievement> $achievements
* @return $this
*/
public function setAchievementsRelationValue(Collection $achievements);
}
9 changes: 8 additions & 1 deletion app/Models/Achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Infrastructure\Contracts\Model\HasCreatedByAttribute;
use App\Infrastructure\Database\Eloquent\Model;
use App\Listeners\FillCreatedByWhenCreatingModel;
use App\Support\Model\HandleCreatedByAttribute;
use App\Support\Models\HandleCreatedByAttribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Achievement extends Model implements HasCreatedByAttribute
Expand All @@ -16,6 +16,13 @@ class Achievement extends Model implements HasCreatedByAttribute
Concerns\Achievement\Event,
Concerns\Achievement\Relation;

/**
* Value of date format ISO.
*
* @var string
*/
const DATE_FORMAT_ISO = 'DD MMMM YYYY';

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 10 additions & 1 deletion app/Models/Concerns/Achievement/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
* @property \Illuminate\Support\Carbon $achieved_date
* @property int $amount
* @property string|null $note
* @property-read string $achieved_date_iso_format
*
* @see \App\Models\Achievement
*/
trait Attribute
{
//
/**
* Return "achieved_date_iso_format" attribute value.
*
* @return string
*/
public function getAchievedDateIsoFormatAttribute(): string
{
return $this->achieved_date->isoFormat(static::DATE_FORMAT_ISO);
}
}
2 changes: 1 addition & 1 deletion app/Models/Concerns/Achievement/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Models\Concerns\Achievement;

use App\Models\Achievement;
use App\Support\Model\Event as ModelEvent;
use App\Support\Models\Event as ModelEvent;
use Illuminate\Support\Facades\Auth;

/**
Expand Down
73 changes: 72 additions & 1 deletion app/Models/Concerns/Achievement/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,88 @@

namespace App\Models\Concerns\Achievement;

use App\Models\Target;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $target_id Foreign key of \App\Models\Target.
* @property-read \App\Models\Target $target
* @property int|null $event_id Foreign key of \App\Models\Event.
* @property-read \App\Models\Event|null $event
* @property int|null $achieved_by Foreign key of \App\Models\User.
* @property-read \App\Models\User|null $achieved_by
* @property-read \App\Models\User|null $achievedBy
*
* @see \App\Models\Achievement
*/
trait Relation
{
/**
* Define an inverse one-to-many relationship with \App\Models\Target.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function target(): BelongsTo
{
return $this->belongsTo(Target::class);
}

/**
* Return \App\Models\Target model relation value.
*
* @return \App\Models\Target
*/
public function getTargetRelationValue(): Target
{
return $this->getRelationValue('target');
}

/**
* Set \App\Models\Target model relation value.
*
* @param \App\Models\Target $target
* @return $this
*/
public function setTargetRelationValue(Target $target)
{
$this->target()->associate($target);

return $this;
}

/**
* Define an inverse one-to-many relationship with \App\Models\Event.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}

/**
* Return \App\Models\Event model relation value.
*
* @return \App\Models\Event|null
*/
public function getEventRelationValue(): ?Event
{
return $this->getRelationValue('event');
}

/**
* Set \App\Models\Event model relation value.
*
* @param \App\Models\Event $event
* @return $this
*/
public function setEventRelationValue(Event $event)
{
$this->event()->associate($event);

return $this;
}

/**
* Define an inverse one-to-one relationship with \App\Models\User.
*
Expand Down
Loading

0 comments on commit 82aed66

Please sign in to comment.