ldc-crm/app/Http/Controllers/ServiceController.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Controllers;
2021-03-14 10:45:42 +01:00
use App\Http\Requests\ManageServiceRequest;
2020-12-01 17:02:10 +01:00
use App\Models\Brand;
use App\Models\Service;
class ServiceController extends Controller
{
public function index()
{
2020-12-01 17:02:10 +01:00
$services = Service::all();
return view('services.index', compact('services'));
}
2021-03-14 10:45:42 +01:00
public function manage(ManageServiceRequest $request, Service $service = null)
{
2020-12-01 17:02:10 +01:00
$brands = Brand::all();
2021-03-14 10:45:42 +01:00
$edit = $service ? true : false;
2020-12-01 17:02:10 +01:00
2021-03-14 10:45:42 +01:00
if ($request->isMethod('post')) {
2020-12-01 17:02:10 +01:00
if (!$service) {
2021-03-14 10:45:42 +01:00
$service = new Service($request->validated());
} else {
$service->fill($request->validated());
2020-12-01 17:02:10 +01:00
}
2021-03-14 10:45:42 +01:00
$brand = Brand::findOrFail($request->brand_id);
$service->brand()->associate($brand);
2020-12-01 17:02:10 +01:00
$service->save();
2021-03-14 10:45:42 +01:00
return redirect()->route('services')->with('success', $edit ? 'Modifications enregistrées !' : sprintf(
'Token du service : %s',
$service->bearer_token
));
2020-12-01 17:02:10 +01:00
}
return view('services.manage', compact('service', 'brands', 'edit'));
}
2021-03-14 10:45:42 +01:00
public function generateApiToken(Service $service)
{
2021-03-14 10:45:42 +01:00
$service->generateBearerToken();
2020-12-01 17:02:10 +01:00
$service->save();
2021-03-14 10:45:42 +01:00
return back()->with('success', "Nouveau token d'API de " . $service->name . " : " . $service->bearer_token);
2020-12-01 17:02:10 +01:00
}
}