StudentService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Student;
  4. use App\Models\StudentContract;
  5. use App\Models\User;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Http\UploadedFile;
  8. use Illuminate\Support\Facades\Storage;
  9. class StudentService
  10. {
  11. public function getAll(User $user): Collection
  12. {
  13. $unitId = $this->resolveUnitId($user);
  14. return Student::where('unit_id', $unitId)
  15. ->orderBy('created_at', 'desc')
  16. ->get();
  17. }
  18. public function getFranchisorActive(): Collection
  19. {
  20. return Student::with('unit')
  21. ->where('status', 'active')
  22. ->orderBy('name')
  23. ->get();
  24. }
  25. public function getFranchisorStudentDetail(int $id): ?array
  26. {
  27. $student = Student::with('unit')->find($id);
  28. if (!$student) {
  29. return null;
  30. }
  31. $contract = StudentContract::where('student_id', $id)
  32. ->where('status', 'active')
  33. ->first();
  34. return [
  35. 'id' => $student->id,
  36. 'name' => $student->name,
  37. 'phone' => $student->phone,
  38. 'unit' => $student->unit ? ['fantasy_name' => $student->unit->fantasy_name] : null,
  39. 'protocol' => $contract?->protocol,
  40. ];
  41. }
  42. public function getFranchisorSummary(): array
  43. {
  44. $total = Student::count();
  45. $active = Student::where('status', 'active')->count();
  46. return ['total' => $total, 'active' => $active];
  47. }
  48. public function findById(int $id): ?Student
  49. {
  50. return Student::find($id);
  51. }
  52. public function create(User $user, array $data): Student
  53. {
  54. $unitId = $this->resolveUnitId($user);
  55. $data = $this->handlePhoto($data);
  56. return Student::create(array_merge($data, ['unit_id' => $unitId]));
  57. }
  58. public function update(int $id, array $data): ?Student
  59. {
  60. $model = $this->findById($id);
  61. if (!$model) {
  62. return null;
  63. }
  64. $data = $this->handlePhoto($data, $model->photo_url);
  65. $model->update($data);
  66. return $model->fresh();
  67. }
  68. public function delete(int $id): bool
  69. {
  70. $model = $this->findById($id);
  71. if (!$model) {
  72. return false;
  73. }
  74. if ($model->photo_url) {
  75. Storage::delete($model->photo_url);
  76. }
  77. return $model->delete();
  78. }
  79. private function handlePhoto(array $data, ?string $oldPhotoPath = null): array
  80. {
  81. if (!isset($data['avatar'])) {
  82. return $data;
  83. }
  84. if ($data['avatar'] instanceof UploadedFile) {
  85. if ($oldPhotoPath) {
  86. Storage::delete($oldPhotoPath);
  87. }
  88. $data['photo_url'] = $data['avatar']->store('students/photos');
  89. } elseif (is_null($data['avatar'])) {
  90. if ($oldPhotoPath) {
  91. Storage::delete($oldPhotoPath);
  92. }
  93. $data['photo_url'] = null;
  94. }
  95. unset($data['avatar']);
  96. return $data;
  97. }
  98. private function resolveUnitId(User $user): int
  99. {
  100. $unit = $user->units()->first();
  101. abort_if(!$unit, 403, 'Usuário sem unidade associada.');
  102. return $unit->id;
  103. }
  104. }