From 2807140c6525af3e32150289b8ff6bec18b36cc2 Mon Sep 17 00:00:00 2001 From: "m.zargarov" Date: Tue, 23 Apr 2024 14:52:49 +0400 Subject: [PATCH] Added views, controllers, services --- app/Http/Controllers/GradeController.php | 18 ++--- app/Http/Controllers/StudentController.php | 91 ++++++++++++++++++++++ app/Http/Controllers/SubjectController.php | 24 +++--- app/Http/Controllers/TeacherController.php | 86 ++++++++++++++++++++ app/Http/Controllers/UserController.php | 64 --------------- app/Http/Requests/StudentPostRequest.php | 36 +++++++++ app/Http/Requests/TeacherPostRequest.php | 35 +++++++++ app/Models/Admin.php | 6 -- app/Models/Student.php | 21 ++++- app/Models/Teacher.php | 15 +++- app/Providers/ModelServiceProvider.php | 55 +++++++++++++ app/Services/GradeService.php | 32 ++++++++ app/Services/ServiceInterface.php | 14 ++++ app/Services/StudentService.php | 36 +++++++++ app/Services/SubjectService.php | 32 ++++++++ app/Services/TeacherService.php | 36 +++++++++ bootstrap/providers.php | 1 + resources/views/includes/header.blade.php | 5 +- resources/views/students/create.blade.php | 5 ++ resources/views/students/edit.blade.php | 5 ++ resources/views/students/form.blade.php | 40 ++++++++++ resources/views/students/index.blade.php | 66 ++++++++++++++++ resources/views/students/show.blade.php | 26 +++++++ resources/views/teachers/create.blade.php | 5 ++ resources/views/teachers/edit.blade.php | 5 ++ resources/views/teachers/form.blade.php | 37 +++++++++ resources/views/teachers/index.blade.php | 66 ++++++++++++++++ resources/views/teachers/show.blade.php | 23 ++++++ routes/web.php | 6 +- 29 files changed, 797 insertions(+), 94 deletions(-) create mode 100644 app/Http/Controllers/StudentController.php create mode 100644 app/Http/Controllers/TeacherController.php delete mode 100644 app/Http/Controllers/UserController.php create mode 100644 app/Http/Requests/StudentPostRequest.php create mode 100644 app/Http/Requests/TeacherPostRequest.php create mode 100644 app/Providers/ModelServiceProvider.php create mode 100644 app/Services/GradeService.php create mode 100644 app/Services/ServiceInterface.php create mode 100644 app/Services/StudentService.php create mode 100644 app/Services/SubjectService.php create mode 100644 app/Services/TeacherService.php create mode 100644 resources/views/students/create.blade.php create mode 100644 resources/views/students/edit.blade.php create mode 100644 resources/views/students/form.blade.php create mode 100644 resources/views/students/index.blade.php create mode 100644 resources/views/students/show.blade.php create mode 100644 resources/views/teachers/create.blade.php create mode 100644 resources/views/teachers/edit.blade.php create mode 100644 resources/views/teachers/form.blade.php create mode 100644 resources/views/teachers/index.blade.php create mode 100644 resources/views/teachers/show.blade.php diff --git a/app/Http/Controllers/GradeController.php b/app/Http/Controllers/GradeController.php index 73623a7..1aee597 100644 --- a/app/Http/Controllers/GradeController.php +++ b/app/Http/Controllers/GradeController.php @@ -4,19 +4,23 @@ namespace App\Http\Controllers; use App\Http\Requests\GradePostRequest; use App\Models\Grade; +use App\Services\ServiceInterface; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Illuminate\View\View; class GradeController extends Controller { + public function __construct( + protected ServiceInterface $gradeService, + ){} + /** * Display a listing of the resource. */ public function index(): View { return view('grades.index', [ - 'grades' => Grade::filter()->paginate(10)->withQueryString(), + 'grades' => $this->gradeService->getAll(), ]); } @@ -33,9 +37,7 @@ class GradeController extends Controller */ public function store(GradePostRequest $request): RedirectResponse { - $grade = Grade::create($request->validated()); - - return redirect()->route('grades.show', $grade); + return redirect()->route('grades.show', $this->gradeService->create($request->validated())); } /** @@ -63,9 +65,7 @@ class GradeController extends Controller */ public function update(GradePostRequest $request, Grade $grade): RedirectResponse { - $grade->update($request->validated()); - - return redirect()->route('grades.show', $grade); + return redirect()->route('grades.show', $this->gradeService->update($grade, $request->validated())); } /** @@ -73,7 +73,7 @@ class GradeController extends Controller */ public function destroy(Grade $grade): RedirectResponse { - $grade->delete(); + $this->gradeService->delete($grade); return redirect()->route('grades.index'); } diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php new file mode 100644 index 0000000..db8f8a3 --- /dev/null +++ b/app/Http/Controllers/StudentController.php @@ -0,0 +1,91 @@ + $this->studentService->getAll(), + ]); + } + + /** + * Show the form for creating a new resource. + */ + public function create(): View + { + return view('students.create', [ + 'grades' => Grade::all(), + ]); + } + + /** + * Store a newly created resource in storage. + */ + public function store(StudentPostRequest $request): RedirectResponse + { + return redirect()->route( + 'students.show', + $this->studentService->create($request->validated()) + ); + } + + /** + * Display the specified resource. + */ + public function show(Student $student): View + { + return view('students.show', [ + 'student' => $student, + 'grades' => Grade::all(), + ]); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Student $student): View + { + return view('students.edit', [ + 'student' => $student, + 'grades' => Grade::all(), + ]); + } + + /** + * Update the specified resource in storage. + */ + public function update(StudentPostRequest $request, Student $student): RedirectResponse + { + return redirect()->route( + 'students.show', + $this->studentService->update($student, $request->validated()) + ); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Student $student): RedirectResponse + { + $this->studentService->delete($student); + + return redirect()->route('students.index'); + } +} diff --git a/app/Http/Controllers/SubjectController.php b/app/Http/Controllers/SubjectController.php index aff017c..e47975d 100644 --- a/app/Http/Controllers/SubjectController.php +++ b/app/Http/Controllers/SubjectController.php @@ -4,19 +4,23 @@ namespace App\Http\Controllers; use App\Http\Requests\SubjectPostRequest; use App\Models\Subject; +use App\Services\ServiceInterface; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Illuminate\View\View; class SubjectController extends Controller { + public function __construct( + protected ServiceInterface $subjectService, + ){} + /** * Display a listing of the resource. */ public function index(): View { return view('subjects.index', [ - "subjects" => Subject::filter()->paginate(10)->withQueryString(), + "subjects" => $this->subjectService->getAll(), ]); } @@ -33,9 +37,10 @@ class SubjectController extends Controller */ public function store(SubjectPostRequest $request): RedirectResponse { - $subject = Subject::create($request->validated()); - - return redirect()->route('subjects.show', $subject); + return redirect()->route( + 'subjects.show', + $this->subjectService->create($request->validated()) + ); } /** @@ -63,9 +68,10 @@ class SubjectController extends Controller */ public function update(SubjectPostRequest $request, Subject $subject): RedirectResponse { - $subject->update($request->validated()); - - return redirect()->route('subjects.show', $subject); + return redirect()->route( + 'subjects.show', + $this->subjectService->update($subject, $request->validated()) + ); } /** @@ -73,7 +79,7 @@ class SubjectController extends Controller */ public function destroy(Subject $subject): RedirectResponse { - $subject->delete(); + $this->subjectService->delete($subject); return redirect()->route('subjects.index'); } diff --git a/app/Http/Controllers/TeacherController.php b/app/Http/Controllers/TeacherController.php new file mode 100644 index 0000000..d6b5f66 --- /dev/null +++ b/app/Http/Controllers/TeacherController.php @@ -0,0 +1,86 @@ + $this->teacherService->getAll(), + ]); + } + + /** + * Show the form for creating a new resource. + */ + public function create(): View + { + return view('teachers.create'); + } + + /** + * Store a newly created resource in storage. + */ + public function store(TeacherPostRequest $request): RedirectResponse + { + return redirect()->route( + 'teachers.show', + $this->teacherService->create($request->validated()) + ); + } + + /** + * Display the specified resource. + */ + public function show(Teacher $teacher): View + { + return view('teachers.show', [ + 'teacher' => $teacher, + ]); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Teacher $teacher): View + { + return view('teachers.edit', [ + 'teacher' => $teacher, + ]); + } + + /** + * Update the specified resource in storage. + */ + public function update(TeacherPostRequest $request, Teacher $teacher): RedirectResponse + { + return redirect()->route( + 'teachers.show', + $this->teacherService->update($teacher, $request->validated()) + ); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Teacher $teacher): RedirectResponse + { + $this->teacherService->delete($teacher); + + return redirect()->route('teachers.index'); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php deleted file mode 100644 index 4eb8213..0000000 --- a/app/Http/Controllers/UserController.php +++ /dev/null @@ -1,64 +0,0 @@ -|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|max:255', + 'last_name' => 'required|max:255', + 'middle_name' => 'required|max:255', + 'birthday' => 'required|date', + 'grade_id' => 'required|exists:grades,id', + 'login' => 'required|max:255|lowercase|unique:users,login', + 'password' => 'required|max:255', + ]; + } +} diff --git a/app/Http/Requests/TeacherPostRequest.php b/app/Http/Requests/TeacherPostRequest.php new file mode 100644 index 0000000..bfe249e --- /dev/null +++ b/app/Http/Requests/TeacherPostRequest.php @@ -0,0 +1,35 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|max:255', + 'last_name' => 'required|max:255', + 'middle_name' => 'required|max:255', + 'birthday' => 'required|date', + 'login' => 'required|max:255|lowercase|unique:users,login', + 'password' => 'required|max:255', + ]; + } +} diff --git a/app/Models/Admin.php b/app/Models/Admin.php index 7ff762e..5bd9156 100644 --- a/app/Models/Admin.php +++ b/app/Models/Admin.php @@ -10,12 +10,6 @@ class Admin extends Model { use HasFactory; - protected $fillable = [ - 'name', - 'last_name', - 'middle_name', - ]; - public function user(): MorphOne { return $this->morphOne(User::class, 'userable'); diff --git a/app/Models/Student.php b/app/Models/Student.php index 28efe00..d810df1 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -5,7 +5,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; - +use Illuminate\Database\Eloquent\Relations\MorphOne; +use Illuminate\Database\Eloquent\Builder; class Student extends Model { use HasFactory; @@ -21,4 +22,22 @@ class Student extends Model { return $this->belongsTo(Grade::class); } + + public function user(): MorphOne + { + return $this->morphOne(User::class, 'userable'); + } + + public function scopeFilter(Builder $query): void + { + $name = request('name'); + $query->when($name, function (Builder $query, $name){ + $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]); + }); + } } diff --git a/app/Models/Teacher.php b/app/Models/Teacher.php index b5f057b..9b2ee1c 100644 --- a/app/Models/Teacher.php +++ b/app/Models/Teacher.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphOne; - +use Illuminate\Database\Eloquent\Builder; class Teacher extends Model { use HasFactory; @@ -32,4 +32,17 @@ class Teacher extends Model { return $this->morphOne(User::class, 'userable'); } + + public function scopeFilter(Builder $query): void + { + $name = request('name'); + $query->when($name, function (Builder $query, $name){ + $query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]); + $query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]); + }); + } } diff --git a/app/Providers/ModelServiceProvider.php b/app/Providers/ModelServiceProvider.php new file mode 100644 index 0000000..cfbd547 --- /dev/null +++ b/app/Providers/ModelServiceProvider.php @@ -0,0 +1,55 @@ +app->when(StudentController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new StudentService(); + }); + + $this->app->when(TeacherController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new TeacherService(); + }); + + $this->app->when(GradeController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new GradeService(); + }); + + $this->app->when(SubjectController::class) + ->needs(ServiceInterface::class) + ->give(function () { + return new SubjectService(); + }); + } + + /** + * Bootstrap services. + */ + public function boot(): void + { + // + } +} diff --git a/app/Services/GradeService.php b/app/Services/GradeService.php new file mode 100644 index 0000000..b59e22f --- /dev/null +++ b/app/Services/GradeService.php @@ -0,0 +1,32 @@ +paginate(10)->withQueryString(); + } + + public function create(array $data): Grade + { + return Grade::create($data); + } + + public function update($model, array $data): Grade + { + $model->update($data); + + return $model; + } + + public function delete($model): void + { + $model->delete(); + } +} diff --git a/app/Services/ServiceInterface.php b/app/Services/ServiceInterface.php new file mode 100644 index 0000000..bcc6856 --- /dev/null +++ b/app/Services/ServiceInterface.php @@ -0,0 +1,14 @@ +paginate(10)->withQueryString(); + } + + public function create(array $data): User + { + $user = User::create($data['credentials']); + $student = Student::create($data['user_data']); + $student->user()->save($user); + + return $student; + } + + public function update($model, array $data): User + { + $model->update($data); + + return $model; + } + + public function delete($model): void + { + $model->destroy(); + } +} diff --git a/app/Services/SubjectService.php b/app/Services/SubjectService.php new file mode 100644 index 0000000..2d22384 --- /dev/null +++ b/app/Services/SubjectService.php @@ -0,0 +1,32 @@ +paginate(10)->withQueryString(); + } + + public function create(array $data): Subject + { + return Subject::create($data); + } + + public function update($model, array $data): Subject + { + $model->update($data); + + return $model; + } + + public function delete($model): void + { + $model->delete(); + } +} diff --git a/app/Services/TeacherService.php b/app/Services/TeacherService.php new file mode 100644 index 0000000..2d1eabb --- /dev/null +++ b/app/Services/TeacherService.php @@ -0,0 +1,36 @@ +paginate(10)->withQueryString(); + } + + public function create(array $data): User + { + $user = User::create($data['credentials']); + $teacher = Teacher::create($data['user_data']); + $teacher->user()->save($user); + + return $teacher; + } + + public function update($model, array $data): User + { + $model->update($data); + + return $model; + } + + public function delete($model): void + { + $model->destroy(); + } +} diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 38b258d..132764c 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -2,4 +2,5 @@ return [ App\Providers\AppServiceProvider::class, + App\Providers\ModelServiceProvider::class, ]; diff --git a/resources/views/includes/header.blade.php b/resources/views/includes/header.blade.php index d049236..9e59fab 100644 --- a/resources/views/includes/header.blade.php +++ b/resources/views/includes/header.blade.php @@ -1,9 +1,10 @@
diff --git a/resources/views/students/create.blade.php b/resources/views/students/create.blade.php new file mode 100644 index 0000000..db31db1 --- /dev/null +++ b/resources/views/students/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('students.form', ['route' => route('students.store'), 'method' => 'POST']) +@endsection diff --git a/resources/views/students/edit.blade.php b/resources/views/students/edit.blade.php new file mode 100644 index 0000000..eb50242 --- /dev/null +++ b/resources/views/students/edit.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('students.form', ['route' => route('students.update', $user), 'method' => 'PUT']) +@endsection diff --git a/resources/views/students/form.blade.php b/resources/views/students/form.blade.php new file mode 100644 index 0000000..83a6602 --- /dev/null +++ b/resources/views/students/form.blade.php @@ -0,0 +1,40 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+
diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php new file mode 100644 index 0000000..1942b51 --- /dev/null +++ b/resources/views/students/index.blade.php @@ -0,0 +1,66 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{__('Новый ученик')}}
+ +
+
+
{{__('Поиск ученика')}}
+
+
+
+ + +
+
+ +
+
+
+
+ @if (count($students)) +
+
{{__('Ученики')}}
+
+ + + + + + + @foreach ($students as $student) + + + + + + @endforeach + +
Ученик 
+ + + + +
+ @csrf + @method('DELETE') + +
+
+
+ +
+ @endif +
+
+@endsection + diff --git a/resources/views/students/show.blade.php b/resources/views/students/show.blade.php new file mode 100644 index 0000000..bca92ed --- /dev/null +++ b/resources/views/students/show.blade.php @@ -0,0 +1,26 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{ ('Ученик') }}
+
+
+
ФИО: {{ $user->last_name }} {{ $user->name }} {{ $user->middle_name }}
+
+
+
Класс: {{ $user->class->name }}
+
+
+
Дата рождения: {{ $user->birthday }}
+
+
+ +
+
+
+@endsection + diff --git a/resources/views/teachers/create.blade.php b/resources/views/teachers/create.blade.php new file mode 100644 index 0000000..98dc911 --- /dev/null +++ b/resources/views/teachers/create.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('teachers.form', ['route' => route('teachers.store'), 'method' => 'POST']) +@endsection diff --git a/resources/views/teachers/edit.blade.php b/resources/views/teachers/edit.blade.php new file mode 100644 index 0000000..7b8428a --- /dev/null +++ b/resources/views/teachers/edit.blade.php @@ -0,0 +1,5 @@ +@extends('layouts.app') + +@section('content') + @include('teachers.form', ['route' => route('teachers.update', $user), 'method' => 'PUT']) +@endsection diff --git a/resources/views/teachers/form.blade.php b/resources/views/teachers/form.blade.php new file mode 100644 index 0000000..f30889b --- /dev/null +++ b/resources/views/teachers/form.blade.php @@ -0,0 +1,37 @@ +
+
+
+
+
+ @csrf + @method($method) +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+
diff --git a/resources/views/teachers/index.blade.php b/resources/views/teachers/index.blade.php new file mode 100644 index 0000000..11278eb --- /dev/null +++ b/resources/views/teachers/index.blade.php @@ -0,0 +1,66 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{__('Новый учитель')}}
+ +
+
+
{{__('Поиск учителя')}}
+
+
+
+ + +
+
+ +
+
+
+
+ @if (count($teachers)) +
+
{{__('Учителя')}}
+
+ + + + + + + @foreach ($teachers as $teacher) + + + + + + @endforeach + +
Ученик 
+ + + + +
+ @csrf + @method('DELETE') + +
+
+
+ +
+ @endif +
+
+@endsection + diff --git a/resources/views/teachers/show.blade.php b/resources/views/teachers/show.blade.php new file mode 100644 index 0000000..4015e7c --- /dev/null +++ b/resources/views/teachers/show.blade.php @@ -0,0 +1,23 @@ +@extends('layouts.app') + +@section('content') +
+
+
+
{{ ('Учитель') }}
+
+
+
ФИО: {{ $user->last_name }} {{ $user->name }} {{ $user->middle_name }}
+
+
+
Дата рождения: {{ $user->birthday }}
+
+
+ +
+
+
+@endsection + diff --git a/routes/web.php b/routes/web.php index d953592..6758792 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,8 +1,9 @@