File: /var/www/api_matriculas/app/Http/Controllers/CountryController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\Country\StoreCountryRequest;
use App\Http\Requests\Country\UpdateCountryRequest;
use App\Services\CountryService;
use Exception;
class CountryController extends Controller
{
protected $countryService;
public function __construct(CountryService $countryService)
{
$this->countryService = $countryService;
}
public function list($allData = false)
{
try {
$data = $this->countryService->list($allData);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
public function store(StoreCountryRequest $request)
{
try {
$data = $this->countryService->store($request);
return genericResponse(['data' => $data, 'message' => 'Género Creado Correctamente', 'code' => 201], 201);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
public function update($id, UpdateCountryRequest $request)
{
try {
$data = $this->countryService->update($request, $id);
return genericResponse(['data' => null, 'message' => null, 'code' => 204], 204);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
public function show($id)
{
try {
$data = $this->countryService->show($id);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
public function destroy($id)
{
try {
$data = $this->countryService->delete($id);
return genericResponse(['data' => null, 'message' => 'Género Eliminado correctamente', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
}