feature test su institution
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreInstitutionCategoryRequest;
|
||||
use App\Http\Requests\UpdateInstitutionCategoryRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\Lists\InstitutionCategory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* CRUD del lookup categorie di istituzione (scrittura riservata agli Admin).
|
||||
*
|
||||
* NB gli id 2/3/4/6 sono allineati al legacy e usati dall'ETL: cancellarne uno
|
||||
* in uso è bloccato (409). Nessun soft delete su questo lookup.
|
||||
*/
|
||||
class InstitutionCategoryController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* Elenco delle categorie.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return $this->collectionResponse(InstitutionCategory::orderBy('value')->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea una categoria.
|
||||
*/
|
||||
public function store(StoreInstitutionCategoryRequest $request): JsonResponse
|
||||
{
|
||||
$category = InstitutionCategory::create($request->validated());
|
||||
|
||||
return $this->createdResponse($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dettaglio di una categoria.
|
||||
*/
|
||||
public function show(InstitutionCategory $institutionCategory): JsonResponse
|
||||
{
|
||||
return $this->okResponse($institutionCategory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiorna una categoria.
|
||||
*/
|
||||
public function update(UpdateInstitutionCategoryRequest $request, InstitutionCategory $institutionCategory): JsonResponse
|
||||
{
|
||||
$institutionCategory->update($request->validated());
|
||||
|
||||
return $this->updatedResponse($institutionCategory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina una categoria (se non assegnata ad alcuna istituzione).
|
||||
*/
|
||||
public function destroy(InstitutionCategory $institutionCategory): JsonResponse
|
||||
{
|
||||
if ($institutionCategory->isInUse()) {
|
||||
return $this->conflictResponse('Cannot delete: category assigned to at least one institution.');
|
||||
}
|
||||
|
||||
$institutionCategory->delete();
|
||||
|
||||
return $this->deletedResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indica se la categoria è in uso (bloccata per l'eliminazione).
|
||||
*/
|
||||
public function usage(InstitutionCategory $institutionCategory): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'in_use' => $institutionCategory->isInUse(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
172
backend/app/Http/Controllers/InstitutionController.php
Normal file
172
backend/app/Http/Controllers/InstitutionController.php
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\IndexInstitutionRequest;
|
||||
use App\Http\Requests\StoreInstitutionRequest;
|
||||
use App\Http\Requests\UpdateInstitutionRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\Institution;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* CRUD delle istituzioni (scrittura riservata agli Admin via tier di rotta).
|
||||
*
|
||||
* Identificate verso l'esterno dallo `uuid` (route key), non dall'id interno.
|
||||
* Soft delete: destroy = cestina, restore = ripristina, forceDestroy = elimina
|
||||
* definitivamente (e rimuove il file logo + i link via cascade FK).
|
||||
*/
|
||||
class InstitutionController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/** Sottocartella del disco `public` dove vivono i loghi. */
|
||||
private const LOGO_DIR = 'institution_logo';
|
||||
|
||||
/**
|
||||
* Elenco paginato, con filtri opzionali (ricerca, categoria, cestino).
|
||||
*/
|
||||
public function index(IndexInstitutionRequest $request): JsonResponse
|
||||
{
|
||||
$query = Institution::query()
|
||||
->with('category')
|
||||
->orderBy('name');
|
||||
|
||||
$this->applyTrashed($query, $request->input('trashed'));
|
||||
$this->applySearch($query, $request->input('search'));
|
||||
|
||||
if ($request->filled('category_id')) {
|
||||
$query->where('category_id', $request->integer('category_id'));
|
||||
}
|
||||
|
||||
$institutions = $query->paginate($request->integer('per_page') ?: 20);
|
||||
|
||||
return $this->paginatedCollectionResponse($institutions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea un'istituzione (con upload del logo).
|
||||
*/
|
||||
public function store(StoreInstitutionRequest $request): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data['logo'] = $this->storeLogo($request->file('logo'));
|
||||
|
||||
$institution = Institution::create($data);
|
||||
|
||||
return $this->createdResponse($institution->load('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dettaglio (con categoria e link).
|
||||
*/
|
||||
public function show(Institution $institution): JsonResponse
|
||||
{
|
||||
return $this->okResponse($institution->load(['category', 'links']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiorna un'istituzione. Logo opzionale: se presente sostituisce il vecchio.
|
||||
*/
|
||||
public function update(UpdateInstitutionRequest $request, Institution $institution): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
if ($request->hasFile('logo')) {
|
||||
$this->deleteLogo($institution->logo);
|
||||
$data['logo'] = $this->storeLogo($request->file('logo'));
|
||||
} else {
|
||||
unset($data['logo']);
|
||||
}
|
||||
|
||||
$institution->update($data);
|
||||
|
||||
return $this->updatedResponse($institution->load('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cestina (soft delete).
|
||||
*/
|
||||
public function destroy(Institution $institution): JsonResponse
|
||||
{
|
||||
$institution->delete();
|
||||
|
||||
return $this->deletedResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ripristina un'istituzione cestinata.
|
||||
*/
|
||||
public function restore(Institution $institution): JsonResponse
|
||||
{
|
||||
$institution->restore();
|
||||
|
||||
return $this->restoredResponse($institution->load('category'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina definitivamente (rimuove il file logo; i link cadono in cascata DB).
|
||||
*/
|
||||
public function forceDestroy(Institution $institution): JsonResponse
|
||||
{
|
||||
$this->deleteLogo($institution->logo);
|
||||
$institution->forceDelete();
|
||||
|
||||
return $this->deletedResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Salva il file logo sul disco `public` e ne restituisce il path relativo.
|
||||
*/
|
||||
private function storeLogo(UploadedFile $file): string
|
||||
{
|
||||
return $file->store(self::LOGO_DIR, 'public');
|
||||
}
|
||||
|
||||
/**
|
||||
* Rimuove un file logo dal disco `public`, se presente.
|
||||
*/
|
||||
private function deleteLogo(?string $path): void
|
||||
{
|
||||
if (filled($path)) {
|
||||
Storage::disk('public')->delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Include i soft-deleted: `with` (tutti) o `only` (solo cestinati).
|
||||
*
|
||||
* @param Builder<Institution> $query
|
||||
*/
|
||||
private function applyTrashed(Builder $query, ?string $trashed): void
|
||||
{
|
||||
match ($trashed) {
|
||||
'with' => $query->withTrashed(),
|
||||
'only' => $query->onlyTrashed(),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ricerca parziale su nome, città e sigla.
|
||||
*
|
||||
* @param Builder<Institution> $query
|
||||
*/
|
||||
private function applySearch(Builder $query, ?string $search): void
|
||||
{
|
||||
if (blank($search)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$term = '%'.$search.'%';
|
||||
|
||||
$query->where(function (Builder $q) use ($term): void {
|
||||
$q->where('name', 'like', $term)
|
||||
->orWhere('city', 'like', $term)
|
||||
->orWhere('abbreviation', 'like', $term);
|
||||
});
|
||||
}
|
||||
}
|
||||
69
backend/app/Http/Controllers/InstitutionLinkController.php
Normal file
69
backend/app/Http/Controllers/InstitutionLinkController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreInstitutionLinkRequest;
|
||||
use App\Http\Requests\UpdateInstitutionLinkRequest;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\Institution;
|
||||
use App\Models\InstitutionLink;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* Risorse web di un'istituzione (sito ufficiale, ticketing, social, ...).
|
||||
*
|
||||
* Risorsa annidata e scoped sotto `institutions/{institution}`: il link deve
|
||||
* appartenere all'istituzione del path. Nessun soft delete (solo Institution lo ha).
|
||||
*/
|
||||
class InstitutionLinkController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* Elenco dei link di un'istituzione, ordinati.
|
||||
*/
|
||||
public function index(Institution $institution): JsonResponse
|
||||
{
|
||||
$links = $institution->links()->orderBy('sort_order')->get();
|
||||
|
||||
return $this->collectionResponse($links);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiunge un link all'istituzione.
|
||||
*/
|
||||
public function store(StoreInstitutionLinkRequest $request, Institution $institution): JsonResponse
|
||||
{
|
||||
$link = $institution->links()->create($request->validated());
|
||||
|
||||
return $this->createdResponse($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dettaglio di un link.
|
||||
*/
|
||||
public function show(Institution $institution, InstitutionLink $link): JsonResponse
|
||||
{
|
||||
return $this->okResponse($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggiorna un link.
|
||||
*/
|
||||
public function update(UpdateInstitutionLinkRequest $request, Institution $institution, InstitutionLink $link): JsonResponse
|
||||
{
|
||||
$link->update($request->validated());
|
||||
|
||||
return $this->updatedResponse($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina un link.
|
||||
*/
|
||||
public function destroy(Institution $institution, InstitutionLink $link): JsonResponse
|
||||
{
|
||||
$link->delete();
|
||||
|
||||
return $this->deletedResponse();
|
||||
}
|
||||
}
|
||||
31
backend/app/Http/Requests/IndexInstitutionRequest.php
Normal file
31
backend/app/Http/Requests/IndexInstitutionRequest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Filtri di query per l'elenco istituzioni. Autorizzazione demandata ai
|
||||
* middleware di rotta (tier.app).
|
||||
*/
|
||||
class IndexInstitutionRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'search' => ['nullable', 'string', 'max:255'],
|
||||
'category_id' => ['nullable', 'integer', 'exists:institution_categories,id'],
|
||||
'trashed' => ['nullable', Rule::in(['with', 'only'])],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreInstitutionCategoryRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'value' => ['required', 'string', 'max:25', 'unique:institution_categories,value'],
|
||||
];
|
||||
}
|
||||
}
|
||||
37
backend/app/Http/Requests/StoreInstitutionLinkRequest.php
Normal file
37
backend/app/Http/Requests/StoreInstitutionLinkRequest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\InstitutionLinkType;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreInstitutionLinkRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->mergeIfMissing(['sort_order' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'url' => ['required', 'url', 'max:2000'],
|
||||
'type' => ['required', Rule::enum(InstitutionLinkType::class)],
|
||||
'label' => ['nullable', 'string', 'max:100'],
|
||||
'sort_order' => ['required', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
48
backend/app/Http/Requests/StoreInstitutionRequest.php
Normal file
48
backend/app/Http/Requests/StoreInstitutionRequest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreInstitutionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default per i campi con valore di colonna (color, is_storage_place):
|
||||
* così non si inseriscono mai NULL su colonne NOT NULL.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->mergeIfMissing([
|
||||
'color' => '#c5cae9',
|
||||
'is_storage_place' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'category_id' => ['required', 'integer', 'exists:institution_categories,id'],
|
||||
'name' => ['required', 'string', 'max:255', 'unique:institutions,name'],
|
||||
'abbreviation' => ['required', 'string', 'max:5', 'unique:institutions,abbreviation'],
|
||||
'address' => ['required', 'string', 'max:255'],
|
||||
'city' => ['required', 'string', 'max:100'],
|
||||
'lat' => ['required', 'numeric', 'between:-90,90'],
|
||||
'lon' => ['required', 'numeric', 'between:-180,180'],
|
||||
'logo' => ['required', 'image', 'max:5120'],
|
||||
'color' => ['required', 'string', 'max:50'],
|
||||
'is_storage_place' => ['required', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateInstitutionCategoryRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$id = $this->route('institution_category')->id;
|
||||
|
||||
return [
|
||||
'value' => ['required', 'string', 'max:25', Rule::unique('institution_categories', 'value')->ignore($id)],
|
||||
];
|
||||
}
|
||||
}
|
||||
37
backend/app/Http/Requests/UpdateInstitutionLinkRequest.php
Normal file
37
backend/app/Http/Requests/UpdateInstitutionLinkRequest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\InstitutionLinkType;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateInstitutionLinkRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->mergeIfMissing(['sort_order' => 0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'url' => ['required', 'url', 'max:2000'],
|
||||
'type' => ['required', Rule::enum(InstitutionLinkType::class)],
|
||||
'label' => ['nullable', 'string', 'max:100'],
|
||||
'sort_order' => ['required', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
40
backend/app/Http/Requests/UpdateInstitutionRequest.php
Normal file
40
backend/app/Http/Requests/UpdateInstitutionRequest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateInstitutionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$id = $this->route('institution')->id;
|
||||
|
||||
return [
|
||||
'category_id' => ['required', 'integer', 'exists:institution_categories,id'],
|
||||
'name' => ['required', 'string', 'max:255', Rule::unique('institutions', 'name')->ignore($id)],
|
||||
'abbreviation' => ['required', 'string', 'max:5', Rule::unique('institutions', 'abbreviation')->ignore($id)],
|
||||
'address' => ['required', 'string', 'max:255'],
|
||||
'city' => ['required', 'string', 'max:100'],
|
||||
'lat' => ['required', 'numeric', 'between:-90,90'],
|
||||
'lon' => ['required', 'numeric', 'between:-180,180'],
|
||||
// Opzionale in update: se assente si mantiene il logo esistente.
|
||||
'logo' => ['nullable', 'image', 'max:5120'],
|
||||
'color' => ['required', 'string', 'max:50'],
|
||||
'is_storage_place' => ['required', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user