106 lines
3.4 KiB
PHP
106 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Breeder;
|
|
use App\Models\GeneralSetting;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class BreederController extends Controller
|
|
{
|
|
/**
|
|
* Get the geoJson datas in departments.geojson file.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getGeoJson(): \Illuminate\Http\JsonResponse
|
|
{
|
|
$departmentGeoJson = Cache::rememberForever(
|
|
'geoJsonMap',
|
|
function () {
|
|
return File::json(base_path('/public/departments.geojson'));
|
|
}
|
|
);
|
|
return response()->json([$departmentGeoJson]);
|
|
}
|
|
|
|
/**
|
|
* Get an array of total count of breeders per department.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getBreederCountPerDepartment(): \Illuminate\Http\JsonResponse
|
|
{
|
|
// TODO: mettre à jour le cache lors de l'édition/ajout/suppression d'un éleveur.
|
|
$breederCountPerDepartment = Cache::rememberForever(
|
|
'breederCountPerDepartment',
|
|
function () {
|
|
return DB::table('breeders')
|
|
->where('published', true)
|
|
->groupBy('department')
|
|
->select('department', DB::raw('count(*) as total'))
|
|
->get()->toArray();
|
|
}
|
|
);
|
|
return response()->json($breederCountPerDepartment);
|
|
}
|
|
|
|
/**
|
|
* Get all the breeders in a given department.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getDepartmentBreeders(Request $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
// TODO: checker si le departement existe.
|
|
// TODO: mettre à jour le cache lors de l'édition/ajout/suppression d'un éleveur.
|
|
$breeders = Cache::rememberForever(
|
|
sprintf('departmentBreeders%s', $request->DEPARTMENT),
|
|
function () use ($request) {
|
|
return Breeder::query()
|
|
->where('published', true)
|
|
->where('is_highlighted', true)
|
|
->where('department', \App\Enums\Department::make($request->DEPARTMENT))
|
|
->get()->toArray();
|
|
}
|
|
);
|
|
return response()->json($breeders);
|
|
}
|
|
|
|
/**
|
|
* Get all the breeders views in a given department.
|
|
*
|
|
* @param Request $request
|
|
* @return array<string>
|
|
*/
|
|
public function getDepartmentBreedersViews(Request $request): array
|
|
{
|
|
return Breeder::query()
|
|
->where('published', true)
|
|
->where('is_highlighted', true)
|
|
->where('department', \App\Enums\Department::make($request->DEPARTMENT))
|
|
->get()
|
|
->map(function ($breederModel) {
|
|
return view('fo.partials.breeder-card', [
|
|
'model' => $breederModel,
|
|
'isSlider' => true,
|
|
])->render();
|
|
})->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get all the breeders views in a given department.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getManageableTextComponent(): array
|
|
{
|
|
return GeneralSetting::query()->where('type', SettingsType::breederMap)->first()->value;
|
|
}
|
|
}
|