176 lines
5.8 KiB
PHP
176 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StorePersonRequest;
|
|
use App\Http\Requests\UpdatePersonRequest;
|
|
use App\Models\Person;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Exception;
|
|
|
|
class PersonController extends Controller
|
|
{
|
|
protected array $relations = ['migration', 'naturalization', 'residence', 'family', 'internment'];
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = Person::with($this->relations);
|
|
|
|
if ($search = $request->search) {
|
|
$query->where(fn($q) =>
|
|
$q->where('full_name', 'LIKE', "%$search%")
|
|
->orWhere('surname', 'LIKE', "%$search%")
|
|
->orWhere('occupation', 'LIKE', "%$search%")
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $query->paginate(10),
|
|
'message' => 'Persons retrieved successfully'
|
|
]);
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to retrieve persons', $e);
|
|
}
|
|
}
|
|
|
|
public function show(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$person = Person::with($this->relations)->find($id);
|
|
|
|
return $person
|
|
? $this->successResponse($person, 'Person retrieved successfully')
|
|
: $this->notFoundResponse('Person not found');
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to retrieve person', $e);
|
|
}
|
|
}
|
|
|
|
public function store(StorePersonRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$data = $request->only([
|
|
'surname', 'christian_name', 'date_of_birth', 'place_of_birth',
|
|
'date_of_death', 'occupation', 'additional_notes', 'reference', 'id_card_no'
|
|
]);
|
|
$data['full_name'] = trim("{$request->christian_name} {$request->surname}");
|
|
|
|
$person = Person::create($data);
|
|
|
|
foreach ($this->relations as $relation) {
|
|
if ($request->has($relation)) {
|
|
$person->$relation()->create($request->$relation);
|
|
}
|
|
}
|
|
|
|
return $this->successResponse(
|
|
$person->load($this->relations),
|
|
'Person created successfully',
|
|
201
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to create person', $e);
|
|
}
|
|
}
|
|
|
|
public function update(UpdatePersonRequest $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$person = Person::findOrFail($id);
|
|
|
|
$data = $request->only([
|
|
'surname', 'christian_name', 'date_of_birth', 'place_of_birth',
|
|
'date_of_death', 'occupation', 'additional_notes', 'reference', 'id_card_no'
|
|
]);
|
|
|
|
if ($request->hasAny(['christian_name', 'surname'])) {
|
|
$christian = $request->input('christian_name', $person->christian_name);
|
|
$surname = $request->input('surname', $person->surname);
|
|
$data['full_name'] = trim("$christian $surname");
|
|
}
|
|
|
|
$person->update($data);
|
|
|
|
foreach ($this->relations as $relation) {
|
|
if (is_array($request->$relation ?? null)) {
|
|
$person->$relation
|
|
? $person->$relation->update($request->$relation)
|
|
: $person->$relation()->create($request->$relation);
|
|
}
|
|
}
|
|
|
|
return $this->successResponse(
|
|
$person->load($this->relations),
|
|
'Person updated successfully'
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to update person', $e);
|
|
}
|
|
}
|
|
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$person = Person::with($this->relations)->findOrFail($id);
|
|
|
|
foreach ($this->relations as $relation) {
|
|
$person->$relation?->delete();
|
|
}
|
|
|
|
$person->delete();
|
|
|
|
return $this->successResponse(null, 'Person deleted successfully');
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to delete person', $e);
|
|
}
|
|
}
|
|
|
|
// Response helpers
|
|
protected function successResponse($data, string $message, int $status = 200): JsonResponse
|
|
{
|
|
return response()->json(['success' => true, 'data' => $data, 'message' => $message], $status);
|
|
}
|
|
|
|
protected function notFoundResponse(string $message): JsonResponse
|
|
{
|
|
return response()->json(['success' => false, 'message' => $message], 404);
|
|
}
|
|
|
|
protected function errorResponse(string $message, Exception $e): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $message,
|
|
'error' => $e->getMessage()
|
|
], 500);
|
|
}
|
|
public function search(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = Person::query()->with($this->relations);
|
|
|
|
if ($search = $request->input('query')) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('full_name', 'LIKE', "%$search%")
|
|
->orWhere('surname', 'LIKE', "%$search%")
|
|
->orWhere('christian_name', 'LIKE', "%$search%")
|
|
->orWhere('occupation', 'LIKE', "%$search%")
|
|
->orWhere('place_of_birth', 'LIKE', "%$search%")
|
|
->orWhere('id_card_no', 'LIKE', "%$search%");
|
|
});
|
|
}
|
|
|
|
return $this->successResponse(
|
|
$query->paginate(10),
|
|
'Search results returned successfully'
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->errorResponse('Failed to perform search', $e);
|
|
}
|
|
}
|
|
|
|
}
|