-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instanciar de forma automática os services dentro do controller, service e repository
- Loading branch information
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Orangecode\Repository; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
trait RepositoryTransferList | ||
{ | ||
/** @var Model */ | ||
private Model $transferList; | ||
|
||
/** @var array */ | ||
private array $keys; | ||
|
||
/** | ||
* Realiza a parametrização do transferList | ||
*/ | ||
public function transferList(string $model, array $keys = []): Repository | ||
{ | ||
$this->transferList = app()->make($model); | ||
$this->keys = $keys; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Realiza o gerenciamento do transferList | ||
*/ | ||
public function manager(Request $request): void | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
if (empty($request->list) || !isset($this->keys[0]) || !isset($this->keys[1])) { | ||
abort(500, 'Não foi possível atualizar a lista de dados.'); | ||
} | ||
foreach ($request->list as $list) { | ||
if (isset($list['active']) && $list['active']) { | ||
$this->active([ | ||
$this->keys[0] => $request[$this->keys[0]], | ||
$this->keys[1] => $list[$this->keys[1]], | ||
]); | ||
} else { | ||
$this->disabled([ | ||
$this->keys[0] => $request[$this->keys[0]], | ||
$this->keys[1] => $list[$this->keys[1]], | ||
]); | ||
} | ||
} | ||
DB::commit(); | ||
} catch (\Exception $exception) { | ||
DB::rollBack(); | ||
abort(500, $exception->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Realiza a ativação do transfer list | ||
*/ | ||
private function active(array $data): void | ||
{ | ||
$this->transferList::updateOrCreate( | ||
[ | ||
$this->keys[0] => $data[$this->keys[0]], | ||
$this->keys[1] => $data[$this->keys[1]] | ||
], | ||
$data | ||
)?->id; | ||
} | ||
|
||
/** | ||
* Realiza a desativação do transfer list | ||
*/ | ||
private function disabled(array $data): void | ||
{ | ||
$this->transferList::where($this->keys[0], $data[$this->keys[0]]) | ||
->where($this->keys[1], '=', $data[$this->keys[1]]) | ||
->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters