refactor: move bon exchange requests to form requests

This commit is contained in:
Roardom
2023-01-08 04:37:05 -06:00
parent c97bc20e25
commit 210985cf7b
4 changed files with 106 additions and 53 deletions
@@ -14,8 +14,9 @@
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreBonExchangeRequest;
use App\Http\Requests\Staff\UpdateBonExchangeRequest;
use App\Models\BonExchange;
use Illuminate\Http\Request;
class BonExchangeController extends Controller
{
@@ -40,33 +41,15 @@ class BonExchangeController extends Controller
/**
* Store A Bon Exchange.
*/
public function store(Request $request): \Illuminate\Http\RedirectResponse
public function store(StoreBonExchangeRequest $request): \Illuminate\Http\RedirectResponse
{
$bonExchange = new BonExchange();
$bonExchange->description = $request->description;
$bonExchange->value = $request->value;
$bonExchange->cost = $request->cost;
$bonExchange->upload = $request->type === 'upload';
$bonExchange->download = $request->type === 'download';
$bonExchange->personal_freeleech = $request->type === 'personal_freeleech';
$bonExchange->invite = $request->type === 'invite';
$v = \validator($bonExchange->toArray(), [
'description' => 'required',
'value' => 'required|numeric',
'cost' => 'required|numeric',
'upload' => 'required|boolean',
'download' => 'required|boolean',
'personal_freeleech' => 'required|boolean',
'invite' => 'required|boolean',
]);
if ($v->fails()) {
return \to_route('staff.bon_exchanges.create')
->withErrors($v->errors());
}
$bonExchange->save();
BonExchange::create([
'upload' => $request->type === 'upload',
'download' => $request->type === 'download',
'personal_freeleech' => $request->type === 'personal_freeleech',
'upload' => $request->type === 'invite',
]
+ $request->validated());
return \to_route('staff.bon_exchanges.index')
->withSuccess('Bon Exchange Successfully Added');
@@ -85,33 +68,15 @@ class BonExchangeController extends Controller
/**
* Update A Bon Exchange.
*/
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
public function update(UpdateBonExchangeRequest $request, int $id): \Illuminate\Http\RedirectResponse
{
$bonExchange = BonExchange::findOrFail($id);
$bonExchange->description = $request->description;
$bonExchange->value = $request->value;
$bonExchange->cost = $request->cost;
$bonExchange->upload = $request->type === 'upload';
$bonExchange->download = $request->type === 'download';
$bonExchange->personal_freeleech = $request->type === 'personal_freeleech';
$bonExchange->invite = $request->type === 'invite';
$v = \validator($bonExchange->toArray(), [
'description' => 'required',
'value' => 'required|numeric',
'cost' => 'required|numeric',
'upload' => 'required|boolean',
'download' => 'required|boolean',
'personal_freeleech' => 'required|boolean',
'invite' => 'required|boolean',
]);
if ($v->fails()) {
return \to_route('staff.bon_exchanges.edit', ['bonExchange' => $id])
->withErrors($v->errors());
}
$bonExchange->save();
BonExchange::where('id', '=', $id)->update([
'upload' => $request->type === 'upload',
'download' => $request->type === 'download',
'personal_freeleech' => $request->type === 'personal_freeleech',
'upload' => $request->type === 'invite',
]
+ $request->validated());
return \to_route('staff.bon_exchanges.index')
->withSuccess('Bon Exchange Successfully Modified');
@@ -0,0 +1,40 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Requests\Staff;
use Illuminate\Foundation\Http\FormRequest;
class StoreBonExchangeRequest 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.
*/
public function rules(): array
{
return [
'description' => 'required|string',
'value' => 'required|numeric',
'cost' => 'required|numeric',
'type' => 'required|string|in:upload,download,personal_freeleech,invite|exclude',
];
}
}
@@ -0,0 +1,41 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Requests\Staff;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateBonExchangeRequest 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.
*/
public function rules(): array
{
return [
'description' => 'required|string',
'value' => 'required|numeric',
'cost' => 'required|numeric',
'type' => 'required|string|in:upload,download,personal_freeleech,invite|exclude',
];
}
}
+7
View File
@@ -45,4 +45,11 @@ class BonExchange extends Model
'personal_freeleech' => 'boolean',
'invite' => 'boolean',
];
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = ['id'];
}