init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
<?php
namespace App\Services\Cadastro\Subgrupo;
use Illuminate\Validation\Rule;
use DB;
use App\Services\Service;
use App\Models\Cadastro\Subgrupo\Subgrupo;
class SubgrupoService extends Service
{
public static function datatable()
{
$subgrupos = DB::table('cadastro_subgrupo')
->select(
'cadastro_subgrupo.id',
'cadastro_subgrupo.nome',
'cadastro_grupo.nome AS grupo'
)
->join(
'cadastro_grupo',
'cadastro_grupo.id',
'cadastro_subgrupo.grupo_id'
)
->whereNull('cadastro_subgrupo.deleted_at');
return $subgrupos;
}
public static function create()
{
$subgrupo = new Subgrupo;
$subgrupo->fill(array_fill_keys($subgrupo->getFillable(), null));
return [
'form' => [
'grupos' => self::getGrupos(),
],
'model' => $subgrupo->makeHidden(['id'])
];
}
public static function getGrupos()
{
return DB::table('cadastro_grupo')
->select(
'id',
'nome'
)
->whereNull('deleted_at')
->get();
}
public static function store()
{
self::validate();
return Subgrupo::create(request()->all())->fresh();
}
public static function edit($id)
{
$subgrupo = Subgrupo::findOrFail($id);
return [
'form' => [
'grupos' => self::getGrupos(),
],
'model' => $subgrupo->makeHidden(['id'])
];
}
public static function update($id)
{
self::validate();
$subgrupo = tap(Subgrupo::findOrFail($id))->update(request()->all());
return $subgrupo->fresh();
}
public static function validate()
{
$rule = [
'nome' => [
'string',
'required',
'between:3,100',
Rule::unique('\App\Models\Cadastro\Subgrupo\Subgrupo')->where(function ($query) {
return $query
->where('id', '<>', request()->route('subgrupo') ?? null)
->where('nome', request()->get('nome'))
->where('grupo_id', request()->get('grupo_id'))
->whereNull('deleted_at');
}),
],
'grupo_id' => [
'required',
'integer'
]
];
$messages = [
'nome.unique' => 'O nome do Subgrupo já foi inserido anteriormente.',
];
request()->validate($rule, $messages);
}
public static function destroy($id)
{
Subgrupo::findOrFail($id)->delete();
return null;
}
}