refactor: use relations when fetching model data

This commit is contained in:
2025-11-06 15:55:47 +01:00
parent 6eadf3a944
commit 7d386fadfd
3 changed files with 9 additions and 54 deletions

View File

@@ -13,10 +13,10 @@ class CompanyController extends Controller
{
public function all_simple()
{
$companies = Company::all()->makeHidden(['created_at', 'updated_at']);
$companies = Company::all();
$companies->each(function ($company) {
$company->contact = User::find($company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
$company->contact = User::find($company->contact);
});
return response()->json($companies);
@@ -41,7 +41,7 @@ class CompanyController extends Controller
], 400);
}
$company->contact = User::find($company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
$company->contact = User::find($company->contact);
return response()->json($company);
}

View File

@@ -19,12 +19,7 @@ class InternshipController extends Controller
abort(403, 'Unauthorized');
}
$internships = Internship::all()->makeHidden(['created_at', 'updated_at']);
$internships->each(function ($internship) {
$this->expandInternship($internship, true, true);
});
$internships = Internship::all();
return response()->json($internships);
}
@@ -33,21 +28,17 @@ class InternshipController extends Controller
$user = auth()->user();
if ($user->role === 'STUDENT') {
$internships = Internship::whereUserId($user->id)->get()->makeHidden(['created_at', 'updated_at']);
$internships = Internship::whereUserId($user->id)->get();
} elseif ($user->role === 'EMPLOYER') {
$company = Company::whereContact($user->id)->first();
if (!$company) {
return response()->json(['message' => 'No company associated with this user.'], 404);
}
$internships = Internship::whereCompanyId($company->id)->get()->makeHidden(['created_at', 'updated_at']);
$internships = Internship::whereCompanyId($company->id)->get();
} else {
abort(403, 'Unauthorized');
}
$internships->each(function ($internship) use ($user) {
$this->expandInternship($internship, $user->role === "EMPLOYER", true);
});
return response()->json($internships);
}
@@ -63,8 +54,6 @@ class InternshipController extends Controller
], 400);
}
$this->expandInternship($internship, false, false);
if ($user->role !== 'ADMIN' && $internship->user_id !== $user->id && $user->id !== $internship->company->contact) {
abort(403, 'Unauthorized');
}
@@ -310,29 +299,4 @@ class InternshipController extends Controller
], 400));
}
}
private function expandInternship(Internship $internship, bool $expand_user, bool $expand_dates)
{
if ($expand_user) {
$internship->user = User::find($internship->user_id)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
unset($internship->user_id);
}
$internship->company = Company::find($internship->company_id)->makeHidden(['created_at', 'updated_at']);
unset($internship->company_id);
$internship->contact = User::find($internship->company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
unset($internship->company->contact);
$internship->status = InternshipStatus::whereInternshipId($internship->id)->orderByDesc('changed')->get()->first()->makeHidden(['created_at', 'updated_at', 'id']);
$internship->status->modified_by = User::find($internship->status->modified_by)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
if ($expand_dates) {
$internship->start = Carbon::parse($internship->start)->format('d.m.Y');
$internship->end = Carbon::parse($internship->end)->format('d.m.Y');
}
$internship->agreement = $internship->agreement !== null;
$internship->report = $internship->report !== null;
}
}

View File

@@ -14,7 +14,7 @@ class InternshipStatusController extends Controller
public function get(int $id)
{
$user = auth()->user();
$internship_statuses = InternshipStatus::whereInternshipId($id)->orderByDesc('changed')->get()->makeHidden(['created_at', 'updated_at', 'id']);
$internship_statuses = InternshipStatus::whereInternshipId($id)->orderByDesc('changed')->get();
if (!$internship_statuses) {
return response()->json([
@@ -27,10 +27,6 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$internship_statuses->each(function ($internship_status) {
$internship_status->modified_by = User::find($internship_status->modified_by)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
});
return response()->json($internship_statuses);
}
@@ -49,7 +45,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$currentStatus = $this->currentInternshipStatus($internship);
$currentStatus = $internship->status;
$nextPossibleStatuses = $this->possibleNewStatuses($currentStatus->status, $user->role, $internship->report_confirmed);
return response()->json($nextPossibleStatuses);
@@ -113,7 +109,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$internshipStatus = $this->currentInternshipStatus($internship);
$internshipStatus = $internship->status;
$newStatusValidator = 'in:' . implode(',', $this->possibleNewStatuses($internshipStatus->status, $user->role, $internship->report_confirmed));
$request->validate([
@@ -172,9 +168,4 @@ class InternshipStatusController extends Controller
throw new \InvalidArgumentException('Unknown status');
}
}
private function currentInternshipStatus(Internship $internship)
{
return InternshipStatus::whereInternshipId($internship->id)->orderByDesc('changed')->firstOrFail();
}
}