From a2dec55265d10a9fa8624b6b77ab6d76aa78cf49 Mon Sep 17 00:00:00 2001 From: Krzysztof Rewak Date: Tue, 20 Aug 2024 08:44:10 +0200 Subject: [PATCH] #114 - batch students import (#118) * #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> --- app/Actions/WuStudentsImport.php | 43 ++ .../Dashboard/StudentController.php | 16 + .../js/Pages/Dashboard/Student/Import.vue | 56 +++ .../js/Pages/Dashboard/Student/Index.vue | 3 + resources/js/Pages/Public/News/Index.vue | 1 + resources/js/Pages/Public/News/News.vue | 1 + resources/tests/wu/protocol001.txt | 438 ++++++++++++++++++ routes/web.php | 2 + tests/Feature/StudentImportTest.php | 68 +++ tests/Feature/StudentTest.php | 2 +- 10 files changed, 629 insertions(+), 1 deletion(-) create mode 100644 app/Actions/WuStudentsImport.php create mode 100644 resources/js/Pages/Dashboard/Student/Import.vue create mode 100644 resources/tests/wu/protocol001.txt create mode 100644 tests/Feature/StudentImportTest.php diff --git a/app/Actions/WuStudentsImport.php b/app/Actions/WuStudentsImport.php new file mode 100644 index 0000000..674c125 --- /dev/null +++ b/app/Actions/WuStudentsImport.php @@ -0,0 +1,43 @@ +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(); + } + } + } +} diff --git a/app/Http/Controllers/Dashboard/StudentController.php b/app/Http/Controllers/Dashboard/StudentController.php index 69a3e9f..82b5f2a 100644 --- a/app/Http/Controllers/Dashboard/StudentController.php +++ b/app/Http/Controllers/Dashboard/StudentController.php @@ -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; @@ -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"); + } } diff --git a/resources/js/Pages/Dashboard/Student/Import.vue b/resources/js/Pages/Dashboard/Student/Import.vue new file mode 100644 index 0000000..6f69a6b --- /dev/null +++ b/resources/js/Pages/Dashboard/Student/Import.vue @@ -0,0 +1,56 @@ + + +