Added form requests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-18 00:26:42 +01:00
parent 0c933bcc5d
commit d9b7df5b85

View file

@ -4,12 +4,12 @@ namespace App\Http\Controllers\Accessories;
use App\Events\CheckoutableCheckedOut; use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\AccessoryCheckoutRequest;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\User; use App\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use \Illuminate\Contracts\View\View; use \Illuminate\Contracts\View\View;
use \Illuminate\Http\RedirectResponse; use \Illuminate\Http\RedirectResponse;
@ -57,44 +57,29 @@ class AccessoryCheckoutController extends Controller
* *
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @param Request $request * @param Request $request
* @param int $accessoryId * @param int $accessory
*/ */
public function store(Request $request, $accessoryId) : RedirectResponse public function store(AccessoryCheckoutRequest $request, Accessory $accessory) : RedirectResponse
{ {
// Check if the accessory exists
if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) {
// Redirect to the accessory management page with error
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.user_not_found'));
}
$this->authorize('checkout', $accessory); $this->authorize('checkout', $accessory);
$accessory->assigned_to = $request->input('assigned_to');
$user = User::find($request->input('assigned_to'));
$accessory->checkout_qty = $request->input('checkout_qty', 1);
if (!$user = User::find($request->input('assigned_to'))) { for ($i = 0; $i < $accessory->checkout_qty; $i++) {
return redirect()->route('accessories.checkout.show', $accessory->id)->with('error', trans('admin/accessories/message.checkout.user_does_not_exist'));
}
// Make sure there is at least one available to checkout
if ($accessory->numRemaining() <= 0){
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
}
// Update the accessory data
$accessory->assigned_to = e($request->input('assigned_to'));
$accessory->users()->attach($accessory->id, [ $accessory->users()->attach($accessory->id, [
'accessory_id' => $accessory->id, 'accessory_id' => $accessory->id,
'created_at' => Carbon::now(), 'created_at' => Carbon::now(),
'user_id' => Auth::id(), 'user_id' => Auth::id(),
'assigned_to' => $request->get('assigned_to'), 'assigned_to' => $request->input('assigned_to'),
'note' => $request->input('note'), 'note' => $request->input('note'),
]); ]);
}
DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first();
event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note'))); event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note')));
// Redirect to the new accessory page // Redirect to the new accessory page
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.checkout.success')); return redirect()->route('accessories.index')
->with('success', trans('admin/accessories/message.checkout.success'));
} }
} }