Skip to content

Commit

Permalink
feat: personnel index api (for docchula homepage)
Browse files Browse the repository at this point in the history
  • Loading branch information
keenthekeen committed Oct 26, 2023
1 parent 2d23f11 commit fe53e2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/Http/Controllers/PersonnelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function index(Request $request)
]);
}

public function indexApi(Request $request)
{
$yearList = Personnel::getYearList();
return response()->json([
'years' => $yearList,
'personnels' => Personnel::getYear($request->input('year', $yearList[0]))->map(function (Personnel $personnel) {
$personnel->photo_url = $personnel->photo_path ? Storage::disk('public')->url($personnel->photo_path) : null;
return $personnel;
})->setVisible(['id', 'name', 'name_en', 'position', 'position_en', 'year', 'sequence', 'photo_url']),
]);
}

public function create(Request $request)
{
return $this->edit(new Personnel(['year' => $request->input('year') ?? Helper::buddhistYear()]));
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Personnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static function getYear($year = null): Collection

public static function getYearList(): array
{
return Cache::remember('personnel-year-list', 7200, fn() => self::pluck('year')->unique()->sortDesc()->values());
return Cache::remember('personnel-year-list', 7200, fn() => self::pluck('year')->unique()->sortDesc()->values()->toArray());
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::get('personnels', [\App\Http\Controllers\PersonnelController::class, 'indexApi']);

0 comments on commit fe53e2d

Please sign in to comment.