Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Aug 16, 2023
1 parent 6463a9c commit f394f12
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
47 changes: 13 additions & 34 deletions app/Http/Controllers/Api/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,41 @@

namespace App\Http\Controllers\Api;

use App\Enums\ImageRating;
use App\Http\Controllers\Controller;
use App\Http\Requests\ShowImageRequest;
use App\Models\Image;
use BenSampo\Enum\Rules\EnumValue;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ImageController extends Controller
{
public function index(): JsonResponse
public function index(ShowImageRequest $request): JsonResponse
{
return response()->json(Image::query()
->when($request->validated('rating'), fn (Builder $query, string $rating) => $query->where('rating', $rating))
->orderBy('id', 'desc')
->with('tags')
->cursorPaginate()
);
}

public function indexRating(Request $request, string $rating): JsonResponse
{
$this->validateRating($request);

$images = Image::orderBy('id', 'desc')->with('tags');

if ($rating) {
$images = $images->whereRating($rating);
}

return response()->json($images->get());
}

public function show(Image $image): JsonResponse
{
return response()->json($image);
}

public function showRandom(Request $request, string $rating = null): JsonResponse
public function showRandom(ShowImageRequest $request): JsonResponse
{
$this->validateRating($request);
$randomImage = Image::query()
->when($request->validated('rating'), fn (Builder $query, string $rating) => $query->where('rating', $rating))
->inRandomOrder()
->first();

$image = Image::query();

if ($rating) {
$image = $image->whereRating($rating);
if (! $randomImage) {
abort(Response::HTTP_NOT_FOUND);
}

$image = $image->inRandomOrder()->first();

return response()->json($image);
}

private function validateRating(Request $request)
{
$request->merge(['rating' => $request->route('rating')]);
$request->validate([
'rating' => ['nullable', new EnumValue(ImageRating::class)],
]);
return response()->json($randomImage);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TagController extends Controller
{
public function index(): JsonResponse
{
return response()->json(Tag::all());
return response()->json(Tag::cursorPaginate());
}

public function show(Tag $tag): JsonResponse
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Requests/ShowImageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests;

use App\Enums\ImageRating;
use BenSampo\Enum\Rules\EnumValue;
use Illuminate\Foundation\Http\FormRequest;

class ShowImageRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
/**
* The rating of the image.
* Available values: `unknown`, `safe`, `questionable`, `explicit`
*
* @var string|null
* @example safe
*/
'rating' => ['nullable', new EnumValue(ImageRating::class)],
];
}
}
3 changes: 2 additions & 1 deletion app/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Enums\ImageRating;
use Illuminate\Database\Eloquent\Model;
use ImageManager;
use Intervention\Image\Exception\NotReadableException;
Expand Down Expand Up @@ -74,7 +75,7 @@ class Image extends Model
* @var array
*/
protected $casts = [
//
'rating' => ImageRating::class,
];

/**
Expand Down
7 changes: 7 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class Tag extends Model
//
];

/**
* The number of models to return for pagination.
*
* @var int
*/
protected $perPage = 24;

public function images()
{
return $this->belongsToMany(Image::class);
Expand Down
3 changes: 1 addition & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
Route::get('/health_check', [HomeController::class, 'healthCheck'])->name('health_check');
Route::get('/version', [HomeController::class, 'version'])->name('version');

Route::get('/images/random/{rating?}', [ImageController::class, 'showRandom'])->name('images.random');
Route::get('/images/random', [ImageController::class, 'showRandom'])->name('images.random');
Route::get('/images', [ImageController::class, 'index'])->name('images.index');
Route::get('/images/rating/{rating}', [ImageController::class, 'index'])->name('images.index_rating');
Route::get('/images/{image}', [ImageController::class, 'show'])->name('images.show');

Route::resource('/tags', TagController::class)->only(['index', 'show']);
Expand Down

0 comments on commit f394f12

Please sign in to comment.