UserService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UnitUser;
  4. use App\Models\User;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Http\UploadedFile;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Storage;
  9. class UserService
  10. {
  11. public function authUser(): ?User
  12. {
  13. return Auth::user()->load('units');
  14. }
  15. public function getAll(): Collection
  16. {
  17. return User::with(['state', 'units', 'userType'])->orderBy('name')->get();
  18. }
  19. public function getAllByUnit(): Collection
  20. {
  21. $unitId = Auth::user()->units->first()?->id;
  22. if (!$unitId) {
  23. return User::with(['state', 'units', 'userType'])->whereNull('id')->get();
  24. }
  25. return User::with(['state', 'units', 'userType'])
  26. ->whereHas('units', fn($q) => $q->where('units.id', $unitId))
  27. ->orderBy('name')
  28. ->get();
  29. }
  30. public function findById(int $id): ?User
  31. {
  32. return User::with(['state', 'units', 'userType'])->find($id);
  33. }
  34. public function create(array $data): User
  35. {
  36. $unitId = $data['unit_id'] ?? Auth::user()->units->first()?->id;
  37. unset($data['unit_id']);
  38. $data = $this->handleAvatar($data);
  39. $user = User::create($data);
  40. if ($unitId) {
  41. UnitUser::create([
  42. 'unit_id' => $unitId,
  43. 'user_id' => $user->id,
  44. ]);
  45. }
  46. return $user->load('state');
  47. }
  48. public function update(int $id, array $data): ?User
  49. {
  50. $model = $this->findById($id);
  51. if (!$model) {
  52. return null;
  53. }
  54. $hasUnitId = array_key_exists('unit_id', $data);
  55. $unitId = $data['unit_id'] ?? null;
  56. unset($data['unit_id']);
  57. $data = $this->handleAvatar($data, $model->avatar_url);
  58. $model->update($data);
  59. if ($hasUnitId) {
  60. $model->units()->sync($unitId ? [$unitId] : []);
  61. }
  62. return $model->fresh(['state', 'units']);
  63. }
  64. public function delete(int $id): bool
  65. {
  66. $model = $this->findById($id);
  67. if (!$model) {
  68. return false;
  69. }
  70. if ($model->avatar_url) {
  71. Storage::delete($model->avatar_url);
  72. }
  73. return $model->delete();
  74. }
  75. public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
  76. {
  77. return \App\Models\UserType::orderBy('label')->get();
  78. }
  79. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  80. {
  81. if (!isset($data['avatar'])) {
  82. return $data;
  83. }
  84. if ($data['avatar'] instanceof UploadedFile) {
  85. if ($oldAvatarPath) {
  86. Storage::delete($oldAvatarPath);
  87. }
  88. $data['avatar_url'] = $data['avatar']->store('users/avatars');
  89. } elseif (is_null($data['avatar'])) {
  90. if ($oldAvatarPath) {
  91. Storage::delete($oldAvatarPath);
  92. }
  93. $data['avatar_url'] = null;
  94. }
  95. unset($data['avatar']);
  96. return $data;
  97. }
  98. }