mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-22 14:23:57 -07:00
feat(emoji): add request validation for emoji and alias indexing
Introduced new form request classes, IndexEmojiRequest and IndexEmojiAliasRequest, to handle validation for indexing requests in the Emoji and EmojiAlias controllers. Updated the index methods in both controllers to utilize these new request classes, enhancing validation and code organization. This change improves the overall structure and maintainability of the emoji management system.
This commit is contained in:
parent
e424120c24
commit
de856d38c0
4 changed files with 97 additions and 2 deletions
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\Emoji\IndexEmojiAliasRequest;
|
||||||
use App\Http\Requests\Emoji\SearchEmojiAliasRequest;
|
use App\Http\Requests\Emoji\SearchEmojiAliasRequest;
|
||||||
use App\Http\Requests\Emoji\StoreEmojiAliasRequest;
|
use App\Http\Requests\Emoji\StoreEmojiAliasRequest;
|
||||||
use App\Http\Requests\Emoji\UpdateEmojiAliasRequest;
|
use App\Http\Requests\Emoji\UpdateEmojiAliasRequest;
|
||||||
|
@ -15,7 +16,7 @@ class EmojiAliasController extends Controller
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(IndexEmojiAliasRequest $request)
|
||||||
{
|
{
|
||||||
$aliases = EmojiAlias::query()
|
$aliases = EmojiAlias::query()
|
||||||
->when($request->get('emoji_id'), function ($query, $emojiId) {
|
->when($request->get('emoji_id'), function ($query, $emojiId) {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\Emoji\IndexEmojiRequest;
|
||||||
use App\Http\Requests\Emoji\SearchEmojiRequest;
|
use App\Http\Requests\Emoji\SearchEmojiRequest;
|
||||||
use App\Http\Requests\Emoji\StoreEmojiRequest;
|
use App\Http\Requests\Emoji\StoreEmojiRequest;
|
||||||
use App\Http\Requests\Emoji\UpdateEmojiRequest;
|
use App\Http\Requests\Emoji\UpdateEmojiRequest;
|
||||||
|
@ -15,7 +16,7 @@ class EmojiController extends Controller
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(IndexEmojiRequest $request)
|
||||||
{
|
{
|
||||||
$emojis = Emoji::query()
|
$emojis = Emoji::query()
|
||||||
->when($request->get('category_id'), function ($query, $categoryId) {
|
->when($request->get('category_id'), function ($query, $categoryId) {
|
||||||
|
|
44
app/Http/Requests/Emoji/IndexEmojiAliasRequest.php
Normal file
44
app/Http/Requests/Emoji/IndexEmojiAliasRequest.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Emoji;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class IndexEmojiAliasRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'emoji_id' => 'sometimes|integer|exists:emojis,id',
|
||||||
|
'search' => 'sometimes|string|max:255',
|
||||||
|
'per_page' => 'sometimes|integer|min:1|max:100',
|
||||||
|
'with_emoji' => 'sometimes|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the data for validation.
|
||||||
|
*/
|
||||||
|
protected function prepareForValidation(): void
|
||||||
|
{
|
||||||
|
// Convert string boolean values to actual booleans
|
||||||
|
if ($this->has('with_emoji')) {
|
||||||
|
$this->merge([
|
||||||
|
'with_emoji' => filter_var($this->with_emoji, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
app/Http/Requests/Emoji/IndexEmojiRequest.php
Normal file
49
app/Http/Requests/Emoji/IndexEmojiRequest.php
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\Emoji;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class IndexEmojiRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'category_id' => 'sometimes|integer|exists:emoji_categories,id',
|
||||||
|
'search' => 'sometimes|string|max:255',
|
||||||
|
'per_page' => 'sometimes|integer|min:1|max:100',
|
||||||
|
'with_category' => 'sometimes|boolean',
|
||||||
|
'with_aliases' => 'sometimes|boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the data for validation.
|
||||||
|
*/
|
||||||
|
protected function prepareForValidation(): void
|
||||||
|
{
|
||||||
|
// Convert string boolean values to actual booleans
|
||||||
|
$booleanFields = ['with_category', 'with_aliases'];
|
||||||
|
|
||||||
|
foreach ($booleanFields as $field) {
|
||||||
|
if ($this->has($field)) {
|
||||||
|
$this->merge([
|
||||||
|
$field => filter_var($this->get($field), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue