task-4 (many-to-many) #4

Merged
klllst merged 5 commits from feature/task-4 into develop 2024-05-07 15:09:03 +04:00
27 changed files with 360 additions and 142 deletions
Showing only changes of commit 67a02034f1 - Show all commits

View File

@ -11,7 +11,7 @@ use Illuminate\View\View;
class GradeController extends Controller
{
public function __construct(
protected ServiceInterface $gradeService,
protected ServiceInterface $service,
){}
/**
@ -20,7 +20,7 @@ class GradeController extends Controller
public function index(): View
{
return view('grades.index', [
'grades' => $this->gradeService->getAll(),
'grades' => $this->service->getAll(),
]);
}
@ -37,7 +37,7 @@ class GradeController extends Controller
*/
public function store(GradePostRequest $request): RedirectResponse
{
return redirect()->route('grades.show', $this->gradeService->create($request->validated()));
return redirect()->route('grades.show', $this->service->create($request->validated()));
}
/**
@ -65,7 +65,7 @@ class GradeController extends Controller
*/
public function update(GradePostRequest $request, Grade $grade): RedirectResponse
{
return redirect()->route('grades.show', $this->gradeService->update($grade, $request->validated()));
return redirect()->route('grades.show', $this->service->update($grade, $request->validated()));
}
/**
@ -73,7 +73,7 @@ class GradeController extends Controller
*/
public function destroy(Grade $grade): RedirectResponse
{
$this->gradeService->delete($grade);
$this->service->delete($grade);
return redirect()->route('grades.index');
}

View File

@ -12,7 +12,7 @@ use Illuminate\View\View;
class StudentController extends Controller
{
public function __construct(
protected ServiceInterface $studentService
protected ServiceInterface $service
){}
/**
@ -21,7 +21,7 @@ class StudentController extends Controller
public function index(): View
{
return view('students.index', [
'students' => $this->studentService->getAll(),
'students' => $this->service->getAll(),
]);
}
@ -42,7 +42,7 @@ class StudentController extends Controller
{
return redirect()->route(
'students.show',
$this->studentService->create($request->validated())
$this->service->create($request->validated())
);
}
@ -75,7 +75,7 @@ class StudentController extends Controller
{
return redirect()->route(
'students.show',
$this->studentService->update($student, $request->validated())
$this->service->update($student, $request->validated())
);
}
@ -84,7 +84,7 @@ class StudentController extends Controller
*/
public function destroy(Student $student): RedirectResponse
{
$this->studentService->delete($student);
$this->service->delete($student);
return redirect()->route('students.index');
}

View File

@ -11,7 +11,7 @@ use Illuminate\View\View;
class SubjectController extends Controller
{
public function __construct(
protected ServiceInterface $subjectService,
protected ServiceInterface $service,
){}
/**
@ -20,7 +20,7 @@ class SubjectController extends Controller
public function index(): View
{
return view('subjects.index', [
"subjects" => $this->subjectService->getAll(),
"subjects" => $this->service->getAll(),
]);
}
@ -39,7 +39,7 @@ class SubjectController extends Controller
{
return redirect()->route(
'subjects.show',
$this->subjectService->create($request->validated())
$this->service->create($request->validated())
);
}
@ -70,7 +70,7 @@ class SubjectController extends Controller
{
return redirect()->route(
'subjects.show',
$this->subjectService->update($subject, $request->validated())
$this->service->update($subject, $request->validated())
);
}
@ -79,7 +79,7 @@ class SubjectController extends Controller
*/
public function destroy(Subject $subject): RedirectResponse
{
$this->subjectService->delete($subject);
$this->service->delete($subject);
return redirect()->route('subjects.index');
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubjectTeacherPostRequest;
use App\Models\Subject;
use App\Models\Teacher;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class SubjectTeacherController extends Controller
{
public function __construct(
protected ServiceInterface $service,
){}
public function create(Teacher $teacher): View
{
return view('subject-teacher.create', [
'teacher' => $teacher,
'subjects' => $this->service->getAllSubjects(),
]);
}
public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse
{
return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher));
}
public function edit(Teacher $teacher, Subject $subject): View
{
return view('subject-teacher.edit', [
'teacher' => $teacher,
'updateSubject' => $subject,
'subjects' => $this->service->getAllSubjects(),
]);
}
public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse
{
return redirect()->route('teachers.show',
$this->service->update($teacher, $request->validated(), $subject));
}
public function destroy(Teacher $teacher, Subject $subject): RedirectResponse
{
return redirect()->route('teachers.show', $this->service->delete($teacher, $subject));
}
}

View File

@ -11,7 +11,7 @@ use Illuminate\View\View;
class TeacherController extends Controller
{
public function __construct(
protected ServiceInterface $teacherService
protected ServiceInterface $service
){}
/**
@ -20,7 +20,7 @@ class TeacherController extends Controller
public function index(): View
{
return view('teachers.index', [
'teachers' => $this->teacherService->getAll(),
'teachers' => $this->service->getAll(),
]);
}
@ -39,7 +39,7 @@ class TeacherController extends Controller
{
return redirect()->route(
'teachers.show',
$this->teacherService->create($request->validated())
$this->service->create($request->validated())
);
}
@ -50,6 +50,7 @@ class TeacherController extends Controller
{
return view('teachers.show', [
'teacher' => $teacher,
'subjects' => $teacher->subjects,
]);
}
@ -70,7 +71,7 @@ class TeacherController extends Controller
{
return redirect()->route(
'teachers.show',
$this->teacherService->update($teacher, $request->validated())
$this->service->update($teacher, $request->validated())
);
}
@ -79,7 +80,7 @@ class TeacherController extends Controller
*/
public function destroy(Teacher $teacher): RedirectResponse
{
$this->teacherService->delete($teacher);
$this->service->delete($teacher);
return redirect()->route('teachers.index');
}

View File

@ -22,7 +22,7 @@ class SubjectPostRequest extends FormRequest
public function rules(): array
{
return [
'name' => 'required|unique:grades|max:255',
'name' => 'required|unique:subjects|max:255',
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SubjectTeacherPostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'subject_id' => 'required|exists:subjects,id',
];
}
}

View File

@ -5,11 +5,13 @@ namespace App\Providers;
use App\Http\Controllers\GradeController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\SubjectController;
use App\Http\Controllers\SubjectTeacherController;
use App\Http\Controllers\TeacherController;
use App\Services\GradeService;
use App\Services\ServiceInterface;
use App\Services\StudentService;
use App\Services\SubjectService;
use App\Services\SubjectTeacherService;
use App\Services\TeacherService;
use Illuminate\Support\ServiceProvider;
@ -43,6 +45,12 @@ class ModelServiceProvider extends ServiceProvider
->give(function () {
return new SubjectService();
});
$this->app->when(SubjectTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new SubjectTeacherService();
});
}
/**

View File

@ -3,6 +3,7 @@
namespace App\Services;
use App\Models\Grade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class GradeService implements ServiceInterface
@ -10,7 +11,7 @@ class GradeService implements ServiceInterface
public function getAll(): LengthAwarePaginator
{
return Grade::filter()->paginate(10)->withQueryString();
return Grade::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Grade
@ -18,7 +19,7 @@ class GradeService implements ServiceInterface
return Grade::create($data);
}
public function update($model, array $data): Grade
public function update(Model $model, array $data): Grade
{
$model->update($data);

View File

@ -2,13 +2,15 @@
namespace App\Services;
use Illuminate\Database\Eloquent\Model;
interface ServiceInterface
{
public function getAll();
public function create(array $data);
public function create(array $data): Model;
public function update($model, array $data);
public function update(Model $model, array $data): Model;
public function delete($model);
public function delete(Model $model);
}

View File

@ -5,13 +5,14 @@ namespace App\Services;
use App\Models\Student;
use App\Models\Teacher;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class StudentService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
return Student::filter()->paginate(10)->withQueryString();
return Student::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Student

View File

@ -3,6 +3,7 @@
namespace App\Services;
use App\Models\Subject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class SubjectService implements ServiceInterface
@ -10,7 +11,7 @@ class SubjectService implements ServiceInterface
public function getAll(): LengthAwarePaginator
{
return Subject::filter()->paginate(10)->withQueryString();
return Subject::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Subject
@ -18,14 +19,14 @@ class SubjectService implements ServiceInterface
return Subject::create($data);
}
public function update($model, array $data): Subject
public function update(Model $model, array $data): Subject
{
$model->update($data);
return $model;
}
public function delete($model): void
public function delete(Model $model): void
{
$model->delete();
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Services;
use App\Models\Subject;
use App\Models\Teacher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class SubjectTeacherService implements ServiceInterface
{
public function getAll(Teacher $teacher = null): Collection
{
return $teacher->subjects;
}
public function getAllSubjects(): Collection
{
return Subject::all();
}
public function create(array $data, Model $model = null): Teacher
{
$model->subjects()->syncWithoutDetaching($data['subject_id']);
return $model;
}
public function update(Model $model, array $data, Model $subject = null): Teacher
{
$model->subjects()->detach($subject->id);
$model->subjects()->attach($data['subject_id']);
return $model;
}
public function delete(Model $model, Model $subject = null): Teacher
{
$model->subjects()->detach($subject);
return $model;
}
}

View File

@ -11,7 +11,7 @@ class TeacherService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
return Teacher::filter()->paginate(2)->withQueryString();
return Teacher::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Teacher

View File

@ -3,12 +3,6 @@
@section('content')
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый класс')}}</div>
<div class="card-body">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header">{{__('Поиск класса')}}</div>
<div class="card-body">
@ -24,10 +18,10 @@
</form>
</div>
</div>
@if (count($grades))
<div class="card mt-4">
<div class="card-header">{{__('Классы')}}</div>
<div class="card-body">
@if (count($grades))
<table class="table table-striped">
<thead>
<th>Название</th>
@ -64,10 +58,15 @@
@endforeach
</tbody>
</table>
@else
<p>Классы отсутствуют</p>
@endif
</div>
<div class="card-body">
<a href="{{ route('grades.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $grades->links() }}</div>
</div>
@endif
</div>
</div>
@endsection

View File

@ -1,5 +1,5 @@
@extends('layouts.app')
@section('content')
@include('students.form', ['route' => route('students.update', $user), 'method' => 'PUT'])
@include('students.form', ['route' => route('students.update', $student), 'method' => 'PUT'])
@endsection

View File

@ -34,11 +34,11 @@
</div>
<div class="mb-3">
<label for="email" class="form-label">Эл. почта:</label>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($student) ? $student->email : old('email') }}" required>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($student) ? $student->user->email : old('email') }}" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Пароль:</label>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($student) ? $student->password : old('password') }}" required>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($student) ? $student->user->password : old('password') }}" required>
</div>
<button type="submit" class="btn btn-success">Подтвердить</button>
</form>

View File

@ -3,12 +3,6 @@
@section('content')
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый ученик')}}</div>
<div class="card-body">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header">{{__('Поиск ученика')}}</div>
<div class="card-body">
@ -23,43 +17,48 @@
</form>
</div>
</div>
@if (count($students))
<div class="card mt-4">
<div class="card-header">{{__('Ученики')}}</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<th>ФИО</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($students as $student)
<tr>
<td class="table-text">
<div>
<a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->last_name }} {{ $student->name }} {{ $student->middle_name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="card-footer">{{ $students->links() }}</div>
<div class="card mt-4">
<div class="card-header">{{__('Ученики')}}</div>
<div class="card-body">
@if(count($students))
<table class="table table-striped">
<thead>
<th>ФИО</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($students as $student)
<tr>
<td class="table-text">
<div>
<a href="{{ route('students.show', $student) }}" class="btn btn-block col-8">{{ $student->last_name }} {{ $student->name }} {{ $student->middle_name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('students.edit', $student) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('students.destroy', $student) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>Ученики остутствуют</p>
@endif
</div>
@endif
<div class="card-body">
<a href="{{ route('students.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $students->links() }}</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,5 @@
@extends('layouts.app')
@section('content')
@include('subject-teacher.form', ['route' => route('teachers.subjects.store', $teacher), 'method' => 'POST'])
@endsection

View File

@ -0,0 +1,8 @@
@extends('layouts.app')
@section('content')
@include('subject-teacher.form', [
'route' => route('teachers.subjects.update',
['teacher' => $teacher, 'subject' => $updateSubject]
), 'method' => 'PUT'])
@endsection

View File

@ -0,0 +1,24 @@
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card mt-4">
<div class="card-body">
<form action="{{ $route }}" method="POST">
@csrf
@method($method)
<div class="mb-3">
<label for="subject_id" class="form-label">Предмет:</label>
<select id="subject_id" name="subject_id" class="form-select" required>
<option value="">Выберите предмет</option>
@foreach($subjects as $subject)
<option value="{{ $subject->id }}" {{ isset($updateSubject) && $updateSubject->id == $subject->id ? 'selected' : '' }}>
{{ $subject->name }}
</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-success">Подтвердить</button>
</form>
</div>
</div>
</div>
</div>

View File

@ -3,12 +3,6 @@
@section('content')
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый предмет')}}</div>
<div class="card-body">
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header">{{__('Поиск предмета')}}</div>
<div class="card-body">
@ -23,43 +17,48 @@
</form>
</div>
</div>
@if (count($subjects))
<div class="card mt-4">
<div class="card-header">{{__('Предметы')}}</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<th>Название</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($subjects as $subject)
<tr>
<td class="table-text">
<div>
<a href="{{ route('subjects.show', $subject) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('subjects.destroy', $subject) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="card-footer">{{ $subjects->links() }}</div>
<div class="card mt-4">
<div class="card-header">{{__('Предметы')}}</div>
<div class="card-body">
@if (count($subjects))
<table class="table table-striped">
<thead>
<th>Название</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($subjects as $subject)
<tr>
<td class="table-text">
<div>
<a href="{{ route('subjects.show', $subject) }}" class="btn btn-block col-8">{{ $subject->name }}</a>
</div>
</td>
<td>
<div>
<a href="{{ route('subjects.edit', $subject) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('subjects.destroy', $subject) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>Предметы отсутствуют</p>
@endif
</div>
@endif
<div class="card-body">
<a href="{{ route('subjects.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $subjects->links() }}</div>
</div>
</div>
</div>
@endsection

View File

@ -1,5 +1,5 @@
@extends('layouts.app')
@section('content')
@include('teachers.form', ['route' => route('teachers.update', $user), 'method' => 'PUT'])
@include('teachers.form', ['route' => route('teachers.update', $teacher), 'method' => 'PUT'])
@endsection

View File

@ -23,11 +23,11 @@
</div>
<div class="mb-3">
<label for="email" class="form-label">Эл. почта:</label>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($teacher) ? $teacher->email : old('email') }}" required>
<input type="text" id="email" name="email" class="form-control" value="{{ isset($teacher) ? $teacher->user->email : old('email') }}" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Пароль:</label>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($teacher) ? $teacher->password : old('password') }}" required>
<input type="text" id="password" name="password" class="form-control" value="{{ isset($teacher) ? $teacher->user->password : old('password') }}" required>
</div>
<button type="submit" class="btn btn-success">Подтвердить</button>
</form>

View File

@ -3,12 +3,6 @@
@section('content')
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">{{__('Новый учитель')}}</div>
<div class="card-body">
<a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header">{{__('Поиск учителя')}}</div>
<div class="card-body">
@ -23,10 +17,10 @@
</form>
</div>
</div>
@if (count($teachers))
<div class="card mt-4">
<div class="card-header">{{__('Учителя')}}</div>
<div class="card-body">
@if (count($teachers))
<table class="table table-striped">
<thead>
<th>ФИО</th>
@ -56,10 +50,15 @@
@endforeach
</tbody>
</table>
@else
<p>Учителя отсутствуют</p>
@endif
</div>
<div class="card-body">
<a href="{{ route('teachers.create') }}" class="btn btn-success">Добавить</a>
</div>
<div class="card-footer">{{ $teachers->links() }}</div>
</div>
@endif
</div>
</div>
@endsection

View File

@ -4,7 +4,9 @@
<div class="container col-md-6">
<div class="row justify-content-center">
<div class="card">
<div class="card-header">{{ ('Учитель') }}</div>
<div class="card-header">
{{ ('Учитель') }}
</div>
<div class="card-body">
<div class="mb-3">
<h5><strong>ФИО: </strong>{{ $teacher->fio }}</h5>
@ -17,6 +19,49 @@
<a href="{{ route('teachers.edit', $teacher) }}" class="btn btn-primary">Редактировать</a>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
{{__('Предметы учителя')}}
</div>
<div class="card-body">
@if(count($subjects))
<table class="table table-striped">
<thead>
<th>Название</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach ($subjects as $subject)
<tr>
<td class="table-text">
<div>
{{ $subject->name }}
</div>
</td>
<td>
<div>
<a href="{{ route('teachers.subjects.edit', [$teacher, $subject]) }}" class="btn btn-warning">Редактировать</a>
</div>
</td>
<td>
<form action="{{ route('teachers.subjects.destroy', [$teacher, $subject]) }}" method="POST" style="display: inline-block;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Удалить</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>У учителя отсутствуют предметы</p>
@endif
</div>
<div class="card-body">
<a href="{{ route('teachers.subjects.create', $teacher) }}" class="btn btn-success">Добавить</a>
</div>
</div>
</div>
</div>
@endsection

View File

@ -3,6 +3,7 @@
use App\Http\Controllers\GradeController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\SubjectController;
use App\Http\Controllers\SubjectTeacherController;
use App\Http\Controllers\TeacherController;
use Illuminate\Support\Facades\Route;
@ -10,7 +11,11 @@ Route::get('/', function () {
return redirect()->route('grades.index');
});
Route::resource('grades', GradeController::class);
Route::resource('subjects', SubjectController::class);
Route::resource('students', StudentController::class);
Route::resource('teachers', TeacherController::class);
Route::resources([
'grades' => GradeController::class,
'subjects' => SubjectController::class,
'students' => StudentController::class,
'teachers' => TeacherController::class,
]);
Route::resource('teachers.subjects', SubjectTeacherController::class)->except('index', 'show');