Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
chore: enable authorization policy
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Mar 27, 2024
1 parent d61a68e commit 53695cd
Show file tree
Hide file tree
Showing 34 changed files with 258 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function up(): void
$table->softDeletes();
});

Schema::create('file_uploads', function (Blueprint $table) {
Schema::create('files', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('revision_id')->nullable();

Expand All @@ -44,12 +44,12 @@ public function up(): void
$table->softDeletes();
});

Schema::table('file_uploads', function (Blueprint $table) {
$table->foreign('revision_id')->references('id')->on('file_uploads')->nullOnDelete();
Schema::table('files', function (Blueprint $table) {
$table->foreign('revision_id')->references('id')->on('files')->nullOnDelete();
});

Schema::create('file_attached', function (Blueprint $table) {
$table->foreignUuid('file_upload_id')->constrained('file_uploads')->cascadeOnDelete();
$table->foreignUuid('file_id')->constrained('files')->cascadeOnDelete();
$table->nullableMorphs('attachable');
$table->unsignedSmallInteger('type');
});
Expand All @@ -61,7 +61,7 @@ public function up(): void
public function down(): void
{
Schema::dropIfExists('file_attached');
Schema::dropIfExists('file_uploads');
Schema::dropIfExists('files');
Schema::dropIfExists('addresses');
}
};
4 changes: 2 additions & 2 deletions routes/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Route::apiResource('companies', Controllers\CompanyController::class);
Route::apiResource('employees', Controllers\EmployeeController::class);
Route::apiResource('addresses', Controllers\AddressController::class);
Route::apiResource('files', Controllers\FileUploadController::class);
Route::apiResource('files', Controllers\FileController::class);

Route::apiSingleton('profile', Controllers\ProfileController::class);
Route::apiSingleton('setting', Controllers\SettingController::class);
Expand All @@ -29,7 +29,7 @@
foreach (['companies', 'employees'] as $entity) {
Route::apiResources([
"{$entity}.addresses" => Controllers\AddressController::class,
"{$entity}.files" => Controllers\FileUploadController::class,
"{$entity}.files" => Controllers\FileController::class,
], [
'only' => ['index', 'store'],
// 'parameters' => [$entity => 'entity'],
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Factories/Concerns/WithFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Creasi\Base\Database\Factories\Concerns;

use Creasi\Base\Database\Models\FileUpload;
use Creasi\Base\Enums\FileUploadType;
use Creasi\Base\Database\Models\File;
use Creasi\Base\Enums\FileType;

/**
* @mixin \Illuminate\Database\Eloquent\Factories\Factory
*/
trait WithFileUpload
{
public function withFileUpload(?FileUploadType $type = null): static
public function withFileUpload(?FileType $type = null): static
{
return $this->hasAttached(FileUpload::factory(), [
'type' => $type ?? \fake()->randomElement(FileUploadType::cases()),
return $this->hasAttached(File::factory(), [
'type' => $type ?? \fake()->randomElement(FileType::cases()),
], 'files');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Creasi\Base\Database\Factories;

use Creasi\Base\Database\Models\FileUpload;
use Creasi\Base\Database\Models\File;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\Testing\File;
use Illuminate\Http\Testing\File as FileTesting;
use Illuminate\Support\Str;

/**
* @extends Factory<FileUpload>
* @extends Factory<File>
*/
class FileUploadFactory extends Factory
class FileFactory extends Factory
{
protected $model = FileUpload::class;
protected $model = File::class;

/**
* @return array<string, mixed>
Expand All @@ -23,13 +23,13 @@ public function definition(): array
'revision_id' => null,
'title' => $title = $this->faker->word(),
'name' => $name = Str::slug($title),
'path' => File::fake()->create($name)->path(),
'path' => FileTesting::fake()->create($name)->path(),
'disk' => null,
'summary' => $this->faker->sentence(4),
];
}

public function asRevisionOf(FileUpload $file): static
public function asRevisionOf(File $file): static
{
return $this->state(fn () => [
'revision_id' => $file->getKey(),
Expand Down
14 changes: 7 additions & 7 deletions src/Database/Models/Concerns/WithAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Creasi\Base\Database\Models\Concerns;

use Creasi\Base\Enums\FileUploadType;
use Creasi\Base\Enums\FileType;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Http\UploadedFile;

/**
* @property-read null|\Creasi\Base\Database\Models\FileUpload $avatar
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Creasi\Base\Database\Models\FileUpload> $avatarFile
* @property-read null|\Creasi\Base\Database\Models\File $avatar
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Creasi\Base\Database\Models\File> $avatarFile
*
* @mixin \Creasi\Base\Database\Models\Contracts\HasFileUploads
* @mixin \Creasi\Base\Database\Models\Contracts\HasFiles
*/
trait WithAvatar
{
Expand All @@ -25,7 +25,7 @@ final protected function initializeWithAvatar(): void
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphOne|\Creasi\Base\Database\Models\FileUpload
* @return \Illuminate\Database\Eloquent\Relations\MorphOne|\Creasi\Base\Database\Models\File
*/
public function avatar(): Attribute
{
Expand All @@ -36,13 +36,13 @@ public function avatar(): Attribute

public function setAvatar(string|UploadedFile $image)
{
return $this->storeFile(FileUploadType::Avatar, $image, $this->getRouteKey(), 'Avatar Image');
return $this->storeFile(FileType::Avatar, $image, $this->getRouteKey(), 'Avatar Image');
}

public function avatarFile()
{
return $this->files()
->wherePivot('type', '=', FileUploadType::Avatar)
->wherePivot('type', '=', FileType::Avatar)
->latest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Creasi\Base\Database\Models\Concerns;

use Creasi\Base\Database\Models\File;
use Creasi\Base\Database\Models\FileAttached;
use Creasi\Base\Database\Models\FileUpload;
use Creasi\Base\Enums\FileUploadType;
use Creasi\Base\Enums\FileType;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Http\UploadedFile;

/**
* @mixin \Creasi\Base\Database\Models\Contracts\HasFileUploads
* @mixin \Creasi\Base\Database\Models\Contracts\HasFiles
*/
trait WithFileUploads
trait WithFiles
{
/**
* {@inheritdoc}
*/
public function files(): MorphToMany
{
return $this->morphToMany(FileUpload::class, 'attachable', 'file_attached', null, 'file_upload_id')
return $this->morphToMany(File::class, 'attachable', 'file_attached', null, 'file_id')
->using(FileAttached::class)
->withPivot('type')
->as('attachment');
Expand All @@ -28,14 +28,14 @@ public function files(): MorphToMany
* {@inheritdoc}
*/
public function storeFile(
FileUploadType $type,
FileType $type,
string|UploadedFile $path,
string $name,
?string $title = null,
?string $summary = null,
?string $disk = null,
): FileUpload {
$file = FileUpload::store($path, $name, $title, $summary, $disk);
): File {
$file = File::store($path, $name, $title, $summary, $disk);

$this->files()->syncWithPivotValues($file, ['type' => $type], false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

namespace Creasi\Base\Database\Models\Contracts;

use Creasi\Base\Database\Models\FileUpload;
use Creasi\Base\Enums\FileUploadType;
use Creasi\Base\Database\Models\File;
use Creasi\Base\Enums\FileType;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Http\UploadedFile;

/**
* @property-read \Illuminate\Database\Eloquent\Collection<int, FileUpload> $files
* @property-read \Illuminate\Database\Eloquent\Collection<int, File> $files
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
interface HasFileUploads
interface HasFiles
{
/**
* @return MorphToMany|FileUpload
* @return MorphToMany|File
*/
public function files(): MorphToMany;

public function storeFile(
FileUploadType $type,
FileType $type,
string|UploadedFile $path,
string $name,
?string $title = null,
?string $summary = null,
?string $disk = null,
): FileUpload;
): File;

/**
* Retrieve attachable key attribute value
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Creasi\Base\Database\Model;
use Creasi\Base\Database\Models\Concerns\WithAvatar;
use Creasi\Base\Database\Models\Concerns\WithFileUploads;
use Creasi\Base\Database\Models\Contracts\HasFileUploads;
use Creasi\Base\Database\Models\Concerns\WithFiles;
use Creasi\Base\Database\Models\Contracts\HasFiles;
use Creasi\Base\Database\Models\Contracts\Stakeholder;
use Creasi\Nusa\Contracts\HasAddresses;
use Creasi\Nusa\Models\Concerns\WithAddresses;
Expand All @@ -17,11 +17,11 @@
* @property null|string $phone
* @property null|string $summary
*/
abstract class Entity extends Model implements HasAddresses, HasFileUploads, Stakeholder
abstract class Entity extends Model implements HasAddresses, HasFiles, Stakeholder
{
use WithAddresses;
use WithAvatar;
use WithFileUploads;
use WithFiles;

public function getFillable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* @property-read \Illuminate\Database\Eloquent\Collection<int, Personnel> $ownedByPersonnels
*
* @method static static store(string|UploadedFile $path, string $name, ?string $title = null, ?string $summary = null, ?string $disk = null)
* @method static \Creasi\Base\Database\Factories\FileUploadFactory<FileUpload> factory()
* @method static \Creasi\Base\Database\Factories\FileFactory<File> factory()
*/
class FileUpload extends Model
class File extends Model
{
use HasUuids;

Expand Down Expand Up @@ -82,7 +82,7 @@ public function isInternal(): Attribute

protected function attachable(string $owner)
{
return $this->morphedByMany($owner, 'attachable', 'file_attached', 'file_upload_id')
return $this->morphedByMany($owner, 'attachable', 'file_attached', 'file_id')
->as('attachment');
}

Expand Down
10 changes: 5 additions & 5 deletions src/Database/Models/FileAttached.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Creasi\Base\Database\Models;

use Creasi\Base\Enums\FileUploadType;
use Creasi\Base\Enums\FileType;
use Illuminate\Database\Eloquent\Relations\MorphPivot;

/**
* @property string $file_upload_id
* @property string $file_id
* @property int $attachable_id
* @property string $attachable_type
* @property null|FileUploadType $type
* @property null|FileType $type
* @property-read \Creasi\Base\Contracts\HasFileUploads $attachable
*/
class FileAttached extends MorphPivot
Expand All @@ -18,11 +18,11 @@ class FileAttached extends MorphPivot

public $timestamps = false;

protected $fillable = ['type', 'file_upload_id'];
protected $fillable = ['type', 'file_id'];

protected $casts = [
'attachable_id' => 'int',
'type' => FileUploadType::class,
'type' => FileType::class,
];

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Enums/FileType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Creasi\Base\Enums;

enum FileType: int
{
use KeyableEnum;

case Other = 0;
case Document = 1;
case Image = 2;
case Logo = 3;
case Avatar = 4;
}
16 changes: 0 additions & 16 deletions src/Enums/FileUploadType.php

This file was deleted.

14 changes: 4 additions & 10 deletions src/Http/Controllers/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,26 @@ public function store(StoreRequest $request, Entity $entity)
/**
* @return \Illuminate\Http\Response
*/
public function show(Address $model, Request $request, ?int $address = null)
public function show(Address $address, Request $request)
{
$address = $model->exists ? $model : $model->newQuery()->findOrFail($address);

return AddressResource::make($address)->toResponse($request);
}

/**
* @return \Illuminate\Http\Response
*/
public function update(UpdateRequest $request, Address $model, ?int $address = null)
public function update(UpdateRequest $request, Address $address)
{
$request->fulfill(
$address = $model->newQuery()->findOrFail($address)
);
$request->fulfill($address);

return $this->show($address, $request);
}

/**
* @return \Illuminate\Http\Response
*/
public function destroy(Address $model, ?int $address = null)
public function destroy(Address $address)
{
$address = $model->newQuery()->findOrFail($address);

$address->delete();

return response()->noContent();
Expand Down
Loading

0 comments on commit 53695cd

Please sign in to comment.