Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added reservation search #29

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
];
}
}
Loading