Skip to content

Commit

Permalink
feature: added reservation search
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtendo12 committed Jan 15, 2024
1 parent a80d726 commit c6e8889
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app/Http/Controllers/ReservationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
use App\Enums\ModifiedEnum;
use App\Enums\ResponseStatus;
use App\Http\Requests\ReserveRequest;
use App\Http\Requests\SearchReservationRequest;
use App\Models\Item;
use App\Models\Reservation;
use App\Models\User;
use App\Traits\CommonTrait;
use Illuminate\Http\Request;
use Laravel\SerializableClosure\UnsignedSerializableClosure;

class ReservationController extends Controller
{
Expand Down Expand Up @@ -98,4 +102,20 @@ public function destroy(Reservation $reservation)
null,
);
}

public function search(SearchReservationRequest $request)
{
$validated = $request->validated();
$search = $validated['search'];

$item = Item::where('name', 'like', "%{$search}%")->orWhere('ISBN', 'like', "%{$search}%")->first();
$user = User::where('last_name', 'like', "%{$search}%")->orWhere('email', 'like', "%{$search}%")->orWhere('zip_code', 'like', "%{$search}%")->first();

$reservations = Reservation::where('item_id', $item->id)->orWhere('user_id', $user->id)->get();

return $this->CommonResponse(ResponseStatus::success,
'Reservation found',
["reservations" => $reservations],
);
}
}
30 changes: 30 additions & 0 deletions src/app/Http/Requests/SearchReservationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use App\Models\Reservation;
use Illuminate\Foundation\Http\FormRequest;

class SearchReservationRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$user = auth()->user();
return $user->can('search', Reservation::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'search' => 'required|string|min:3|max:255',
];
}
}

0 comments on commit c6e8889

Please sign in to comment.