115 lines
2.9 KiB
PHP
115 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\PainelSenha;
|
|
|
|
use App\Models\PainelSenha\Prioridade;
|
|
use App\Models\PainelSenha\PrioridadeTipo;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PrioridadeService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
return DB::table('painel_senha_prioridade')
|
|
->select([
|
|
'painel_senha_prioridade.id',
|
|
'painel_senha_prioridade_tipo.nome as tipo_nome',
|
|
'painel_senha_prioridade.nome',
|
|
'painel_senha_prioridade.grau',
|
|
])
|
|
->leftJoin('painel_senha_prioridade_tipo', 'painel_senha_prioridade_tipo.id', 'painel_senha_prioridade.tipo_id')
|
|
->get();
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData(),
|
|
];
|
|
}
|
|
|
|
public static function store($request)
|
|
{
|
|
self::validate($request);
|
|
|
|
return Prioridade::create($request->all())->fresh();
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
return Prioridade::find($id);
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData($id),
|
|
];
|
|
}
|
|
|
|
public static function update($request, $id)
|
|
{
|
|
self::validate($request);
|
|
|
|
return tap(self::show($id))->update($request->all());
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return self::show($id)->delete();
|
|
}
|
|
|
|
private static function validate($request)
|
|
{
|
|
return $request->validate([
|
|
'tipo_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists(PrioridadeTipo::class, 'id'),
|
|
],
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'min:3',
|
|
'max:100',
|
|
Rule::unique('\App\Models\PainelSenha\Prioridade')->where(function ($query) use ($request) {
|
|
return $query->where('nome', $request->nome);
|
|
})->ignore($request->prioridade),
|
|
],
|
|
'grau' => [
|
|
'required',
|
|
'integer',
|
|
],
|
|
]);
|
|
}
|
|
|
|
private static function getFormData()
|
|
{
|
|
return [
|
|
'tipos' => DB::table('painel_senha_prioridade_tipo')
|
|
->select([
|
|
'id',
|
|
'nome',
|
|
])
|
|
->orderBy('nome')
|
|
->get(),
|
|
];
|
|
}
|
|
|
|
private static function getModelData($id = null)
|
|
{
|
|
$prioridade = self::show($id);
|
|
|
|
return [
|
|
'id' => $prioridade->id ?? null,
|
|
'tipo_id' => $prioridade->tipo_id ?? null,
|
|
'nome' => $prioridade->nome ?? null,
|
|
'grau' => $prioridade->grau ?? null,
|
|
];
|
|
}
|
|
}
|