Change service and fix filters

This commit is contained in:
m.zargarov 2024-06-16 12:20:48 +04:00
parent 67c2e366c5
commit b33b694547
22 changed files with 93 additions and 438 deletions

View File

@ -11,11 +11,6 @@ use Illuminate\View\View;
class GradeSubjectController extends Controller class GradeSubjectController extends Controller
{ {
public function __construct(
protected ServiceInterface $service,
) {
}
public function create(Grade $grade): View public function create(Grade $grade): View
{ {
return view('grade-subject.create', [ return view('grade-subject.create', [
@ -28,7 +23,7 @@ class GradeSubjectController extends Controller
{ {
$grade->subjects()->syncWithoutDetaching($request->subject_id); $grade->subjects()->syncWithoutDetaching($request->subject_id);
return redirect()->route('grades.show', $this->service->create($request->validated(), $grade)); return redirect()->route('grades.show', $grade);
} }
public function edit(Grade $grade, Subject $subject): View public function edit(Grade $grade, Subject $subject): View
@ -42,12 +37,16 @@ class GradeSubjectController extends Controller
public function update(GradeSubjectPostRequest $request, Grade $grade, Subject $subject): RedirectResponse public function update(GradeSubjectPostRequest $request, Grade $grade, Subject $subject): RedirectResponse
{ {
return redirect()->route('grades.show', $grade->subjects()->detach($subject);
$this->service->update($grade, $request->validated(), $subject)); $grade->subjects()->attach($request->subject_id);
return redirect()->route('grades.show', $grade);
} }
public function destroy(Grade $grade, Subject $subject): RedirectResponse public function destroy(Grade $grade, Subject $subject): RedirectResponse
{ {
return redirect()->route('grades.show', $this->service->delete($grade, $subject)); $grade->subjects()->detach($subject);
return redirect()->route('grades.show', $grade);
} }
} }

View File

@ -12,24 +12,21 @@ use Illuminate\View\View;
class GradeTeacherController extends Controller class GradeTeacherController extends Controller
{ {
public function __construct(
protected ServiceInterface $service,
) {
}
public function create(Teacher $teacher, Subject $subject): View public function create(Teacher $teacher, Subject $subject): View
{ {
return view('grade-teacher.create', [ return view('grade-teacher.create', [
'teacher' => $teacher, 'teacher' => $teacher,
'subject' => $subject, 'subject' => $subject,
'grades' => $this->service->getGrades($subject), 'grades' => $subject->grades,
]); ]);
} }
public function store(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse public function store(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse
{ {
$teacher->grades()->syncWithoutDetaching($request->grade_id);
return redirect()->route('teachers.subjects.show', [ return redirect()->route('teachers.subjects.show', [
$this->service->create($request->validated(), $teacher), $teacher,
$subject, $subject,
]); ]);
} }
@ -40,22 +37,27 @@ class GradeTeacherController extends Controller
'teacher' => $teacher, 'teacher' => $teacher,
'subject' => $subject, 'subject' => $subject,
'updateGrade' => $grade, 'updateGrade' => $grade,
'grades' => $this->service->getGrades($subject), 'grades' => $subject->grades,
]); ]);
} }
public function update(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse public function update(GradeTeacherPostRequest $request, Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse
{ {
$teacher->grades()->detach($grade);
$teacher->grades()->attach($request->grade_id);
return redirect()->route('teachers.subjects.show', [ return redirect()->route('teachers.subjects.show', [
$this->service->update($teacher, $request->validated(), $grade), $teacher,
$subject, $subject,
]); ]);
} }
public function destroy(Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse public function destroy(Teacher $teacher, Subject $subject, Grade $grade): RedirectResponse
{ {
$teacher->grades()->detach($grade);
return redirect()->route('teachers.subjects.show', [ return redirect()->route('teachers.subjects.show', [
$this->service->delete($teacher, $grade), $teacher,
$subject, $subject,
]); ]);
} }

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Enums\ScoreEnum;
use App\Enums\TypeLesson; use App\Enums\TypeLesson;
use App\Http\Requests\LessonPostRequest; use App\Http\Requests\LessonPostRequest;
use App\Models\Grade; use App\Models\Grade;
@ -12,12 +13,6 @@ use Illuminate\View\View;
class LessonController extends Controller class LessonController extends Controller
{ {
public function __construct
(
protected ServiceInterface $service,
){
}
public function gradeList(): View public function gradeList(): View
{ {
return view('grade-lesson.grades-list', [ return view('grade-lesson.grades-list', [
@ -53,10 +48,15 @@ class LessonController extends Controller
*/ */
public function store(LessonPostRequest $request, Grade $grade): RedirectResponse public function store(LessonPostRequest $request, Grade $grade): RedirectResponse
{ {
$lesson = Lesson::create($request->validated());
$lesson
->students()
->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
return redirect()->route( return redirect()->route(
'grades.lessons.show', [ 'grades.lessons.show', [
$grade, $grade,
$this->service->create($request->validated()) $lesson,
] ]
); );
} }
@ -92,7 +92,7 @@ class LessonController extends Controller
return redirect()->route( return redirect()->route(
'grades.lessons.show',[ 'grades.lessons.show',[
$grade, $grade,
$this->service->update($lesson, $request->validated()) $lesson->update($request->validated()),
] ]
); );
} }
@ -102,7 +102,7 @@ class LessonController extends Controller
*/ */
public function destroy(Grade $grade, Lesson $lesson): RedirectResponse public function destroy(Grade $grade, Lesson $lesson): RedirectResponse
{ {
$this->service->delete($lesson); $lesson->delete();
return redirect()->route('grades.lessons.index', $grade); return redirect()->route('grades.lessons.index', $grade);
} }

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Enums\ScoreEnum; use App\Enums\ScoreEnum;
use App\Models\Lesson; use App\Models\Lesson;
use App\Services\ScoreService;
use App\Services\ServiceInterface; use App\Services\ServiceInterface;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -11,14 +12,14 @@ class ScoreController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $service, protected ScoreService $service,
){ ){
} }
public function show(Lesson $lesson) public function show(Lesson $lesson)
{ {
return view('scores.show', [ return view('scores.show', [
'students' => $this->service->getAll($lesson), 'students' => $lesson->students,
'lesson' => $lesson, 'lesson' => $lesson,
'scores' => ScoreEnum::cases(), 'scores' => ScoreEnum::cases(),
]); ]);

View File

@ -6,13 +6,14 @@ use App\Http\Requests\StudentPostRequest;
use App\Models\Grade; use App\Models\Grade;
use App\Models\Student; use App\Models\Student;
use App\Services\ServiceInterface; use App\Services\ServiceInterface;
use App\Services\StudentService;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\View\View; use Illuminate\View\View;
class StudentController extends Controller class StudentController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $service protected StudentService $service
) { ) {
} }
@ -22,7 +23,7 @@ class StudentController extends Controller
public function index(): View public function index(): View
{ {
return view('students.index', [ return view('students.index', [
'students' => $this->service->getAll(), 'students' => Student::filter()->paginate(5)->withQueryString(),
]); ]);
} }
@ -85,7 +86,8 @@ class StudentController extends Controller
*/ */
public function destroy(Student $student): RedirectResponse public function destroy(Student $student): RedirectResponse
{ {
$this->service->delete($student); $student->user()->delete();
$student->delete();
return redirect()->route('students.index'); return redirect()->route('students.index');
} }

View File

@ -10,18 +10,13 @@ use Illuminate\View\View;
class SubjectController extends Controller class SubjectController extends Controller
{ {
public function __construct(
protected ServiceInterface $service,
) {
}
/** /**
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function index(): View public function index(): View
{ {
return view('subjects.index', [ return view('subjects.index', [
'subjects' => $this->service->getAll(), 'subjects' => Subject::filter()->paginate(5)->withQueryString(),
]); ]);
} }
@ -40,7 +35,7 @@ class SubjectController extends Controller
{ {
return redirect()->route( return redirect()->route(
'subjects.show', 'subjects.show',
$this->service->create($request->validated()) Subject::create($request->validated()),
); );
} }
@ -71,7 +66,7 @@ class SubjectController extends Controller
{ {
return redirect()->route( return redirect()->route(
'subjects.show', 'subjects.show',
$this->service->update($subject, $request->validated()) $subject->update($request->validated())
); );
} }
@ -80,7 +75,7 @@ class SubjectController extends Controller
*/ */
public function destroy(Subject $subject): RedirectResponse public function destroy(Subject $subject): RedirectResponse
{ {
$this->service->delete($subject); $subject->delete();
return redirect()->route('subjects.index'); return redirect()->route('subjects.index');
} }

View File

@ -11,22 +11,19 @@ use Illuminate\View\View;
class SubjectTeacherController extends Controller class SubjectTeacherController extends Controller
{ {
public function __construct(
protected ServiceInterface $service,
) {
}
public function create(Teacher $teacher): View public function create(Teacher $teacher): View
{ {
return view('subject-teacher.create', [ return view('subject-teacher.create', [
'teacher' => $teacher, 'teacher' => $teacher,
'subjects' => $this->service->getAllSubjects(), 'subjects' => Subject::all(),
]); ]);
} }
public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse public function store(SubjectTeacherPostRequest $request, Teacher $teacher): RedirectResponse
{ {
return redirect()->route('teachers.show', $this->service->create($request->validated(), $teacher)); $teacher->subjects()->syncWithoutDetaching($request->subject_id);
return redirect()->route('teachers.show', $teacher);
} }
public function show(Teacher $teacher, Subject $subject): View public function show(Teacher $teacher, Subject $subject): View
@ -34,7 +31,10 @@ class SubjectTeacherController extends Controller
return view('subject-teacher.show', [ return view('subject-teacher.show', [
'teacher' => $teacher, 'teacher' => $teacher,
'subject' => $subject, 'subject' => $subject,
'grades' => $this->service->getGrades($teacher, $subject), 'grades' => $teacher
->grades()
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
->where('subject_id', $subject->id)->get(),
]); ]);
} }
@ -43,18 +43,22 @@ class SubjectTeacherController extends Controller
return view('subject-teacher.edit', [ return view('subject-teacher.edit', [
'teacher' => $teacher, 'teacher' => $teacher,
'updateSubject' => $subject, 'updateSubject' => $subject,
'subjects' => $this->service->getAllSubjects(), 'subjects' => Subject::all(),
]); ]);
} }
public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse public function update(SubjectTeacherPostRequest $request, Teacher $teacher, Subject $subject): RedirectResponse
{ {
return redirect()->route('teachers.show', $teacher->subjects()->detach($subject);
$this->service->update($teacher, $request->validated(), $subject)); $teacher->subjects()->attach($request->subject_id);
return redirect()->route('teachers.show', $teacher);
} }
public function destroy(Teacher $teacher, Subject $subject): RedirectResponse public function destroy(Teacher $teacher, Subject $subject): RedirectResponse
{ {
return redirect()->route('teachers.show', $this->service->delete($teacher, $subject)); $teacher->subjects()->detach($subject);
return redirect()->route('teachers.show', $teacher);
} }
} }

View File

@ -5,13 +5,14 @@ namespace App\Http\Controllers;
use App\Http\Requests\TeacherPostRequest; use App\Http\Requests\TeacherPostRequest;
use App\Models\Teacher; use App\Models\Teacher;
use App\Services\ServiceInterface; use App\Services\ServiceInterface;
use App\Services\TeacherService;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\View\View; use Illuminate\View\View;
class TeacherController extends Controller class TeacherController extends Controller
{ {
public function __construct( public function __construct(
protected ServiceInterface $service protected TeacherService $service
) { ) {
} }
@ -21,7 +22,7 @@ class TeacherController extends Controller
public function index(): View public function index(): View
{ {
return view('teachers.index', [ return view('teachers.index', [
'teachers' => $this->service->getAll(), 'teachers' => Teacher::filter()->paginate(5)->withQueryString(),
]); ]);
} }
@ -81,7 +82,8 @@ class TeacherController extends Controller
*/ */
public function destroy(Teacher $teacher): RedirectResponse public function destroy(Teacher $teacher): RedirectResponse
{ {
$this->service->delete($teacher); $teacher->user()->delete();
$teacher->delete();
return redirect()->route('teachers.index'); return redirect()->route('teachers.index');
} }

View File

@ -46,12 +46,12 @@ class Student extends Model
{ {
$name = request('name'); $name = request('name');
$query->when($name, function (Builder $query, $name) { $query->when($name, function (Builder $query, $name) {
$query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->whereRaw('CONCAT (name, \' \', last_name, \' \', middle_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', middle_name, \' \', last_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (last_name, \' \', name, \' \', middle_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (last_name, \' \', middle_name, \' \', name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (middle_name, \' \', name, \' \', last_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (middle_name, \' \', last_name, \' \', name) ilike ?', ["$name%"]);
}); });
} }

View File

@ -45,12 +45,12 @@ class Teacher extends Model
{ {
$name = request('name'); $name = request('name');
$query->when($name, function (Builder $query, $name) { $query->when($name, function (Builder $query, $name) {
$query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]); $query->whereRaw('CONCAT (name, \' \', last_name, \' \', middle_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (name, \' \', middle_name, \' \', last_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (last_name, \' \', name, \' \', middle_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (last_name, \' \', middle_name, \' \', name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (middle_name, \' \', name, \' \', last_name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]); $query->orWhereRaw('CONCAT (middle_name, \' \', last_name, \' \', name) ilike ?', ["$name%"]);
}); });
} }

View File

@ -1,90 +0,0 @@
<?php
namespace App\Providers;
use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController;
use App\Http\Controllers\GradeTeacherController;
use App\Http\Controllers\LessonController;
use App\Http\Controllers\ScoreController;
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\GradeSubjectService;
use App\Services\GradeTeacherService;
use App\Services\LessonService;
use App\Services\ScoreService;
use App\Services\ServiceInterface;
use App\Services\StudentService;
use App\Services\SubjectService;
use App\Services\SubjectTeacherService;
use App\Services\TeacherService;
use Illuminate\Support\ServiceProvider;
class ModelServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
$this->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(SubjectController::class)
->needs(ServiceInterface::class)
->give(function () {
return new SubjectService();
});
$this->app->when(SubjectTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new SubjectTeacherService();
});
$this->app->when(GradeSubjectController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeSubjectService();
});
$this->app->when(GradeTeacherController::class)
->needs(ServiceInterface::class)
->give(function () {
return new GradeTeacherService();
});
$this->app->when(LessonController::class)
->needs(ServiceInterface::class)
->give(function () {
return new LessonService();
});
$this->app->when(ScoreController::class)
->needs(ServiceInterface::class)
->give(function () {
return new ScoreService();
});
}
/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}

View File

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

View File

@ -1,47 +0,0 @@
<?php
namespace App\Services;
use App\Models\Subject;
use App\Models\Teacher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class GradeTeacherService implements ServiceInterface
{
public function getAll(?Teacher $teacher = null, ?Subject $subject = null): Collection
{
return $teacher
->grades()
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
->where('subject_id', $subject->id)
->get();
}
public function getGrades(Subject $subject): Collection
{
return $subject->grades;
}
public function create(array $data, ?Model $model = null): Teacher
{
$model->grades()->syncWithoutDetaching($data['grade_id']);
return $model;
}
public function update(Model $model, array $data, ?Model $grade = null): Teacher
{
$model->grades()->detach($grade->id);
$model->grades()->attach($data['grade_id']);
return $model;
}
public function delete(Model $model, ?Model $grade = null): Teacher
{
$model->grades()->detach($grade);
return $model;
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace App\Services;
use App\Enums\ScoreEnum;
use App\Models\Grade;
use App\Models\Lesson;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class LessonService implements ServiceInterface
{
public function getAll(?Grade $grade = null): Collection
{
return $grade->lessons()->filter()->get();
}
public function getGrades(): Collection
{
return Grade::all();
}
public function create(array $data): Lesson
{
$lesson = Lesson::create($data);
$lesson
->students()
->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
return $lesson;
}
public function update(Model $model, array $data): Lesson
{
$model->update($data);
return $model;
}
public function delete($model): void
{
$model->delete();
}
}

View File

@ -5,32 +5,16 @@ namespace App\Services;
use App\Models\Lesson; use App\Models\Lesson;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class ScoreService implements ServiceInterface class ScoreService
{ {
public function update(Lesson $lesson, array $data)
public function getAll(?Lesson $lesson = null)
{ {
return $lesson->students; $lesson->students->each(function ($item, $key) use ($data, $lesson) {
}
public function update(Model $model, array $data)
{
$model->students->each(function ($item, $key) use ($data, $model) {
if ($data['score' . $item->id]) { if ($data['score' . $item->id]) {
$model->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]); $lesson->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]);
} }
}); });
return $model; return $lesson;
}
public function delete(Model $model)
{
// TODO: Implement delete() method.
}
public function create(array $data)
{
// TODO: Implement create() method.
} }
} }

View File

@ -1,16 +0,0 @@
<?php
namespace App\Services;
use Illuminate\Database\Eloquent\Model;
interface ServiceInterface
{
public function getAll();
public function create(array $data);
public function update(Model $model, array $data);
public function delete(Model $model);
}

View File

@ -7,13 +7,8 @@ use App\Models\Student;
use App\Models\User; use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
class StudentService implements ServiceInterface class StudentService
{ {
public function getAll(): LengthAwarePaginator
{
return Student::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Student public function create(array $data): Student
{ {
$user = User::create([ $user = User::create([
@ -35,14 +30,14 @@ class StudentService implements ServiceInterface
return $student; return $student;
} }
public function update($model, array $data): Student public function update(Student $student, array $data): Student
{ {
$model->user()->update([ $student->user()->update([
'email' => $data['email'], 'email' => $data['email'],
'password' => $data['password'], 'password' => $data['password'],
]); ]);
$model->update([ $student->update([
'name' => $data['name'], 'name' => $data['name'],
'last_name' => $data['last_name'], 'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'], 'middle_name' => $data['middle_name'],
@ -50,7 +45,7 @@ class StudentService implements ServiceInterface
'grade_id' => $data['grade_id'], 'grade_id' => $data['grade_id'],
]); ]);
return $model; return $student;
} }
public function delete($model): void public function delete($model): void

View File

@ -1,32 +0,0 @@
<?php
namespace App\Services;
use App\Models\Subject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
class SubjectService implements ServiceInterface
{
public function getAll(): LengthAwarePaginator
{
return Subject::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Subject
{
return Subject::create($data);
}
public function update(Model $model, array $data): Subject
{
$model->update($data);
return $model;
}
public function delete(Model $model): void
{
$model->delete();
}
}

View File

@ -1,51 +0,0 @@
<?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 getGrades(Teacher $teacher, Subject $subject): Collection
{
return $teacher
->grades()
->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id')
->where('subject_id', $subject->id)->get();
}
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

@ -6,13 +6,8 @@ use App\Models\Teacher;
use App\Models\User; use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
class TeacherService implements ServiceInterface class TeacherService
{ {
public function getAll(): LengthAwarePaginator
{
return Teacher::filter()->paginate(5)->withQueryString();
}
public function create(array $data): Teacher public function create(array $data): Teacher
{ {
$user = User::create([ $user = User::create([
@ -30,26 +25,26 @@ class TeacherService implements ServiceInterface
return $teacher; return $teacher;
} }
public function update($model, array $data): Teacher public function update(Teacher $teacher, array $data): Teacher
{ {
$model->user()->update([ $teacher->user()->update([
'email' => $data['email'], 'email' => $data['email'],
'password' => $data['password'], 'password' => $data['password'],
]); ]);
$model->update([ $teacher->update([
'name' => $data['name'], 'name' => $data['name'],
'last_name' => $data['last_name'], 'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'], 'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'], 'birthday' => $data['birthday'],
]); ]);
return $model; return $teacher;
} }
public function delete($model): void public function delete(Teacher $teacher): void
{ {
$model->user()->delete(); $teacher->user()->delete();
$model->delete(); $teacher->delete();
} }
} }

View File

@ -2,6 +2,5 @@
return [ return [
App\Providers\AppServiceProvider::class, App\Providers\AppServiceProvider::class,
App\Providers\ModelServiceProvider::class,
App\Providers\TelescopeServiceProvider::class, App\Providers\TelescopeServiceProvider::class,
]; ];

View File

@ -6,6 +6,7 @@ export default defineConfig({
laravel({ laravel({
input: [ input: [
'resources/css/app.css', 'resources/css/app.css',
'resources/sass/app.scss',
'resources/js/app.js', 'resources/js/app.js',
], ],
refresh: true, refresh: true,