CourseWork/app/Services/TeacherService.php

80 lines
2.2 KiB
PHP
Raw Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Services;
2024-06-23 17:02:18 +04:00
use App\Models\Student;
use App\Models\Subject;
2024-04-23 14:52:49 +04:00
use App\Models\Teacher;
use App\Models\User;
use Illuminate\Pagination\LengthAwarePaginator;
2024-06-23 17:02:18 +04:00
use Illuminate\Support\Facades\Auth;
2024-04-23 14:52:49 +04:00
2024-06-16 12:20:48 +04:00
class TeacherService
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
public function create(array $data): Teacher
2024-04-23 14:52:49 +04:00
{
2024-04-27 15:10:21 +04:00
$user = User::create([
'email' => $data['email'],
'password' => $data['password'],
]);
$teacher = Teacher::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
]);
2024-04-23 14:52:49 +04:00
$teacher->user()->save($user);
return $teacher;
}
2024-06-16 12:20:48 +04:00
public function update(Teacher $teacher, array $data): Teacher
2024-04-23 14:52:49 +04:00
{
2024-06-16 12:20:48 +04:00
$teacher->user()->update([
2024-04-27 15:10:21 +04:00
'email' => $data['email'],
'password' => $data['password'],
]);
2024-06-16 12:20:48 +04:00
$teacher->update([
2024-04-27 15:10:21 +04:00
'name' => $data['name'],
'last_name' => $data['last_name'],
'middle_name' => $data['middle_name'],
'birthday' => $data['birthday'],
]);
2024-04-23 14:52:49 +04:00
2024-06-16 12:20:48 +04:00
return $teacher;
2024-04-23 14:52:49 +04:00
}
2024-06-16 12:20:48 +04:00
public function delete(Teacher $teacher): void
2024-04-23 14:52:49 +04:00
{
2024-06-16 12:20:48 +04:00
$teacher->user()->delete();
$teacher->delete();
2024-04-23 14:52:49 +04:00
}
2024-06-23 17:02:18 +04:00
public function getTeachers()
{
if (Auth::user()->userable_type == Student::class) {
return Teacher::join('grade_teacher', 'teachers.id', '=', 'grade_teacher.teacher_id')
->where('grade_id', Auth::user()->userable->grade_id)
->filter()
->paginate(5)
->withQueryString();
}
return Teacher::filter()->paginate(5)->withQueryString();
}
public function getSubjects(Teacher $teacher)
{
if (Auth::user()->userable_type == Student::class) {
return Subject::join('subject_teacher', 'subject_teacher.subject_id', '=', 'subjects.id')
->join('grade_subject', 'grade_subject.subject_id', '=', 'subjects.id')
->where('grade_subject.grade_id', Auth::user()->userable->grade_id)
->where('subject_teacher.teacher_id', $teacher->id)
->get();
}
return $teacher->subjects;
}
2024-04-23 14:52:49 +04:00
}