Skip to content

Commit

Permalink
#5 - students search (#36)
Browse files Browse the repository at this point in the history
* #5 - wip: crud for students

* #5 - added creating students

* #5 - wip: added editing students

* #5 - added students deleting

* #5 - cr fixes

* #5 - added tests and search input on student index page

* #5 - updated .env.ci
  • Loading branch information
kamilpiech97 authored Sep 21, 2023
1 parent abd9465 commit ddbb902
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
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()
->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);
}
}

0 comments on commit ddbb902

Please sign in to comment.