Skip to content

Commit

Permalink
#114 - batch students import (#118)
Browse files Browse the repository at this point in the history
* #114 - batch students import

* Update resources/js/Pages/Dashboard/Student/Import.vue

Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>

---------

Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>
  • Loading branch information
krzysztofrewak and EwelinaSkrzypacz authored Aug 20, 2024
1 parent 2f713a2 commit a2dec55
Show file tree
Hide file tree
Showing 10 changed files with 629 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/Actions/WuStudentsImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Student;

class WuStudentsImport
{
protected array $students = [];

public function import(string $content): array
{
$previousLine = null;

foreach (explode("\n", $content) as $line) {
if (str_starts_with($line, "nr albumu:")) {
$names = explode(" ", $previousLine);

$this->students[] = new Student([
"first_name" => $names[1] ?? "",
"surname" => $names[0] ?? "",
"index_number" => str_replace("nr albumu:", "", $line),
]);
}

$previousLine = $line;
}

return $this->students;
}

public function save(): void
{
/** @var Student $student */
foreach ($this->students as $student) {
if (Student::query()->where("index_number", $student->index_number)->count() === 0) {
$student->save();
}
}
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Dashboard/StudentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Controllers\Dashboard;

use App\Actions\WuStudentsImport;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreStudentRequest;
use App\Http\Requests\UpdateStudentRequest;
Expand Down Expand Up @@ -73,4 +74,19 @@ public function destroy(Student $student): RedirectResponse
return redirect()->back()
->with("success", "Usunięto studenta");
}

public function import(): Response
{
return inertia("Dashboard/Student/Import");
}

public function storeMany(Request $request, WuStudentsImport $importer): RedirectResponse
{
$importer->import($request->get("content") ?? "");
$importer->save();

return redirect()
->route("students.index")
->with("success", "Dodano studentów");
}
}
56 changes: 56 additions & 0 deletions resources/js/Pages/Dashboard/Student/Import.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script setup>
import DashboardLayout from '@/Layouts/DashboardLayout.vue'
import Section from '@/Shared/Components/Section.vue'
import SubmitButton from '@/Shared/Components/Buttons/SubmitButton.vue'
import FormGroup from '@/Shared/Forms/FormGroup.vue'
import FormLabel from '@/Shared/Forms/FormLabel.vue'
import { useForm } from '@inertiajs/inertia-vue3'
import FormError from '@/Shared/Forms/FormError.vue'
import ManagementHeader from '@/Shared/Components/ManagementHeader.vue'
import ManagementHeaderItem from '@/Shared/Components/ManagementHeaderItem.vue'
const form = useForm({
content: '',
})
function importStudents() {
form.post('/dashboard/students/import')
}
</script>

<template>
<DashboardLayout>
<div class="flex flex-col gap-8">
<ManagementHeader>
<template #header>
Zarządzanie studentami (masowo)
</template>
<template #statistics>
<ManagementHeaderItem>
Formularz dodawania nowych studentów
</ManagementHeaderItem>
</template>
</ManagementHeader>

<form class="grid grid-cols-2" @submit.prevent="importStudents">
<Section>
<div class="flex flex-col justify-between gap-4">
<FormGroup>
<FormLabel for="content">
Źródło strony z WU
<span class="text-gray-500">(uczelnia -> protokoły -> edytuj)</span>
</FormLabel>
<textarea v-model="form.content" class="block h-[320px] w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 sm:text-sm sm:leading-6" />
<FormError :error="form.errors.content" />
</FormGroup>
<div class="mt-4 flex justify-end">
<SubmitButton>
Importuj
</SubmitButton>
</div>
</div>
</Section>
</form>
</div>
</DashboardLayout>
</template>
3 changes: 3 additions & 0 deletions resources/js/Pages/Dashboard/Student/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ watch(form, debounce(() => {
</template>
<template #actions>
<TextInput v-if="total || form.search.length > 0" id="filter" v-model="form.search" placeholder="Szukaj" type="search" class="max-w-lg" />
<Button :href="`/dashboard/students/import`">
Dodaj masowo
</Button>
<Button :href="`/dashboard/students/create`">
Dodaj
</Button>
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Public/News/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defineProps({
{{ post.title }}
</a>
</h3>
<!-- eslint-disable vue/no-v-html (as is sanitized) -->
<p class="mt-5 line-clamp-3 text-sm leading-6 text-gray-600" v-html="sanitizeHtml(post.content)" />
</div>
</article>
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Public/News/News.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defineProps({
</template>
</SectionHeader>

<!-- eslint-disable vue/no-v-html (as is sanitized) -->
<div class="mt-10" v-html="sanitizeHtml(news.content)" />
</div>
</div>
Expand Down
Loading

0 comments on commit a2dec55

Please sign in to comment.