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

#5 - students search #36

Merged
merged 10 commits into from
Sep 21, 2023
8 changes: 8 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ QUEUE_CONNECTION=sync
SESSION_DRIVER=array
SESSION_LIFETIME=120
MAIL_MAILER=array

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=keating
DB_USERNAME=keating
DB_PASSWORD=password
DB_ROOT_PASSWORD=example
18 changes: 16 additions & 2 deletions app/Http/Controllers/Dashboard/StudentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@
use App\Http\Requests\StoreStudentRequest;
use App\Http\Requests\UpdateStudentRequest;
use App\Models\Student;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Response;

class StudentController extends Controller
{
public function index(): Response
public function index(Request $request): Response
{
$searchText = $request->query("search");
$students = Student::query()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment: I would replace it with #37 in future.

->when(
$searchText !== null,
fn(Builder $query): Builder => $query
->where("name", "ILIKE", "%$searchText%")
->orWhere("surname", "ILIKE", "%$searchText%")
->orWhere("index_number", "LIKE", "%$searchText%"),
)
->paginate()
->withQueryString();

return inertia("Dashboard/Student/Index", [
"students" => Student::query()->paginate(),
"students" => $students,
]);
}

Expand Down
21 changes: 19 additions & 2 deletions resources/js/Pages/Dashboard/Student/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ import Section from '@/Shared/Components/Section.vue'
import Button from '@/Shared/Components/Buttons/Button.vue'
import EmptyState from '@/Shared/Components/EmptyState.vue'
import RemoveModal from '@/Shared/Modals/RemoveModal.vue'
import { ref } from 'vue'
import { ref, watch } from 'vue'
import { Inertia } from '@inertiajs/inertia'
import { debounce } from 'lodash'
import TextInput from '@/Shared/Forms/TextInput.vue'
import { useForm } from '@inertiajs/inertia-vue3'

defineProps({
const props = defineProps({
students: Object,
search: String,
})
const showModal = ref(false)
const studentToDeleteId = ref(0)
const form = useForm({
search: props.search,
})

watch(form, debounce(() => {
Inertia.get('/dashboard/students', {
search: form.search,
}, {
preserveState: true,
replace: true,
})
}, 300), { deep: true })
</script>

<template>
Expand All @@ -29,6 +45,7 @@ const studentToDeleteId = ref(0)
Dodaj
</Button>
</div>
<TextInput id="filter" v-model="form.search" placeholder="Szukaj" type="search" class="max-w-xs" />
<div v-if="students.data.length">
<TableWrapper class="mt-2">
<template #header>
Expand Down
90 changes: 90 additions & 0 deletions tests/Feature/StudentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Frontend;

use App\Models\Student;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;

class StudentTest extends TestCase
{
use RefreshDatabase;

public function testStudentCanBeCreated(): void
{
$this->assertDatabaseCount("students", 0);

$this->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();

$this->assertDatabaseCount("students", 1);
}

public function testStudentCanBeUpdated(): void
{
$student = Student::factory()->create();

$this->assertDatabaseMissing("students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
]);

$this->patch("/dashboard/students/{$student->id}", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
]);
}

public function testStudentCannotBeCreatedWithBusyIndex(): void
{
Student::factory()->create(["index_number" => "12345"]);
$this->assertDatabaseCount("students", 1);

$this->post("/dashboard/students", [
"name" => "Name",
"surname" => "Surname",
"index_number" => "12345",
])->assertSessionHasErrors("index_number");

$this->assertDatabaseCount("students", 1);
}

public function testStudentCannotBeCreatedWithInvalidData(): void
{
$this->post("/dashboard/students", [
"name" => Str::random(256),
"surname" => Str::random(256),
"index_number" => Str::random(256),
])->assertSessionHasErrors([
"name",
"surname",
"index_number",
]);

$this->assertDatabaseCount("students", 0);
}

public function testStudentCanBeDeleted(): void
{
$student = Student::factory()->create();
$this->assertDatabaseCount("students", 1);

$this->delete("/dashboard/students/{$student->id}");

$this->assertDatabaseCount("students", 0);
}
}
Loading