-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from IsaacHatilima/album
Album
- Loading branch information
Showing
188 changed files
with
1,600 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\AlbumRequest; | ||
use App\Http\Requests\UpdateAlbumRequest; | ||
use App\Models\Album; | ||
use App\Services\AlbumService; | ||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||
use Illuminate\Support\Facades\Redirect; | ||
use Inertia\Inertia; | ||
|
||
class AlbumController extends Controller | ||
{ | ||
use AuthorizesRequests; | ||
private AlbumService $albumService; | ||
|
||
public function __construct(AlbumService $albumService) | ||
{ | ||
$this->albumService = $albumService; | ||
} | ||
|
||
public function index() | ||
{ | ||
$this->authorize('viewAny', Album::class); | ||
|
||
$albums = Album::all(); | ||
|
||
return Inertia::render('Albums', ['albums' => $albums]); | ||
} | ||
|
||
public function trashed() | ||
{ | ||
$this->authorize('view', Album::class); | ||
|
||
$trashedAlbums = Album::onlyTrashed()->get(); | ||
|
||
return Inertia::render('Albums', ['albums' => $trashedAlbums]); | ||
} | ||
|
||
public function store(AlbumRequest $request) | ||
{ | ||
$this->authorize('create', Album::class); | ||
|
||
$this->albumService->createAlbum($request); | ||
|
||
return response(null, 200); | ||
} | ||
|
||
public function update(UpdateAlbumRequest $request, Album $album) | ||
{ | ||
$this->authorize('update', $album); | ||
|
||
$this->albumService->updateAlbum($request, $album); | ||
|
||
return Redirect::route('albums.index'); | ||
} | ||
|
||
public function destroy($public_id) | ||
{ | ||
$album = Album::withTrashed()->where('public_id', $public_id)->first(); | ||
|
||
if (!$album) { | ||
return response([], 404); | ||
} | ||
|
||
$this->authorize('forceDelete', $album); | ||
|
||
$album->forceDelete(); | ||
|
||
return Redirect::route('albums.index'); | ||
} | ||
|
||
public function archive(Album $album) | ||
{ | ||
$this->authorize('delete', $album); | ||
|
||
$album->delete(); | ||
|
||
return Redirect::route('albums.index'); | ||
} | ||
|
||
public function restore( $public_id) | ||
{ | ||
$album = Album::withTrashed()->where('public_id', $public_id)->first(); | ||
|
||
if (!$album) { | ||
return response()->json([], 404); | ||
} | ||
|
||
$this->authorize('restore', $album); | ||
|
||
$album->restore(); | ||
|
||
return Redirect::route('albums.trashed'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Validations\AlbumValidation; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class AlbumRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return array_merge( | ||
AlbumValidation::rules(), | ||
[ | ||
'name' => ['required', 'unique:albums,name'] | ||
] | ||
); | ||
} | ||
|
||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Models\Album; | ||
use App\Validations\AlbumValidation; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class UpdateAlbumRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return (bool) auth()->user(); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return array_merge( | ||
AlbumValidation::rules(), | ||
[ | ||
'name' => ['required', Rule::unique(Album::class)->ignore($this->route('album'))] | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Traits\ModelHelpers; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class Album extends Model | ||
{ | ||
use SoftDeletes, HasFactory, ModelHelpers; | ||
|
||
protected $guarded = [ | ||
'id', | ||
'public_id', | ||
]; | ||
|
||
protected $hidden = ['id']; | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Album; | ||
use App\Models\User; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class AlbumPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
public function viewAny(User $user): bool | ||
{ | ||
return (bool) $user; | ||
} | ||
|
||
public function view(User $user): bool | ||
{ | ||
return (bool) $user; | ||
} | ||
|
||
public function create(User $user): bool | ||
{ | ||
return (bool) $user; | ||
} | ||
|
||
public function update(User $user, Album $album): bool | ||
{ | ||
return $album->user->id === $user->id; | ||
} | ||
|
||
public function delete(User $user, Album $album): bool | ||
{ | ||
return $album->user->id === $user->id; | ||
} | ||
|
||
public function restore(User $user, Album $album): bool | ||
{ | ||
return $album->user->id === $user->id; | ||
} | ||
|
||
public function forceDelete(User $user, Album $album): bool | ||
{ | ||
return $album->user->id === $user->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\Album; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AlbumService | ||
{ | ||
private User $user; | ||
function __construct() | ||
{ | ||
$this->user = Auth::user(); | ||
} | ||
|
||
public function createAlbum($request) | ||
{ | ||
return Album::create([ | ||
'user_id' => $this->user->id, | ||
'name' => strtoupper($request->name), | ||
'description' => $request->description, | ||
]); | ||
} | ||
|
||
public function updateAlbum($request,$album) | ||
{ | ||
$album->name = strtoupper($request->name); | ||
$album->description = $request->description; | ||
return $album->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Validations; | ||
|
||
class AlbumValidation | ||
{ | ||
public static function rules(): array | ||
{ | ||
return [ | ||
'description' => ['nullable'], | ||
'thumbnail' => ['nullable'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Album; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Carbon; | ||
|
||
class AlbumFactory extends Factory | ||
{ | ||
protected $model = Album::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->name(), | ||
'description' => $this->faker->text(), | ||
'thumbnail' => $this->faker->word(), | ||
'created_at' => Carbon::now(), | ||
'updated_at' => Carbon::now(), | ||
|
||
'user_id' => User::factory(), | ||
]; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
database/migrations/2024_10_07_193651_create_albums_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('albums', function (Blueprint $table) { | ||
$table->id(); | ||
$table->uuid('public_id')->unique(); | ||
$table->string('name')->unique(); | ||
$table->foreignId('user_id'); | ||
$table->text('description')->nullable(); | ||
$table->string('thumbnail')->nullable(); | ||
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('albums'); | ||
} | ||
}; |
Oops, something went wrong.