Skip to content

Commit

Permalink
Merge pull request #1 from IsaacHatilima/album
Browse files Browse the repository at this point in the history
Album
  • Loading branch information
IsaacHatilima authored Oct 9, 2024
2 parents 62a81af + 91defb7 commit 6715d89
Show file tree
Hide file tree
Showing 188 changed files with 1,600 additions and 221 deletions.
97 changes: 97 additions & 0 deletions app/Http/Controllers/AlbumController.php
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');
}
}
24 changes: 24 additions & 0 deletions app/Http/Requests/AlbumRequest.php
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;
}
}
34 changes: 34 additions & 0 deletions app/Http/Requests/UpdateAlbumRequest.php
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'))]
]
);
}
}
26 changes: 26 additions & 0 deletions app/Models/Album.php
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);
}
}
47 changes: 47 additions & 0 deletions app/Policies/AlbumPolicy.php
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;
}
}
32 changes: 32 additions & 0 deletions app/Services/AlbumService.php
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();
}
}
14 changes: 14 additions & 0 deletions app/Validations/AlbumValidation.php
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'],
];
}
}
26 changes: 26 additions & 0 deletions database/factories/AlbumFactory.php
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 database/migrations/2024_10_07_193651_create_albums_table.php
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');
}
};
Loading

0 comments on commit 6715d89

Please sign in to comment.