From 4900f1420126b00902c254b313d1428d11ff5dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Tesoura=20J=C3=BAnior?= Date: Tue, 15 Jun 2021 01:28:23 -0300 Subject: [PATCH] Feat/vic add eloquent status messages (#4) (#5) * feat(controller): add CRUD eloquent status check * feat(controller): add soft delete eloquent status check * feat(service): add CRUD eloquent status check * feat(service): add CRUD eloquent status check * feat(lang): update en message dictionary * feat(lang): update es message dictionary * feat(lang): update pt message dictionary * release 7.0.1 --- CHANGELOG.md | 7 ++++ .../Traits/CRUDMethodsController.php | 26 +++++++++++--- .../Traits/SoftDeleteMethodsController.php | 12 +++++-- src/Services/Traits/CRUDMethodsService.php | 35 ++++++++++++++++--- .../Traits/SoftDeleteMethodsService.php | 25 +++++++++++-- src/resources/lang/en/message.php | 6 ++++ src/resources/lang/es/message.php | 6 ++++ src/resources/lang/pt/message.php | 6 ++++ 8 files changed, 110 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32108ae..59c80d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,3 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [7.0.0] - 2021-05-09 ### Added - First project release + +## [7.0.1] - 2021-06-15 +### Added +- Fix base query paramns dates validation +- Add controllers methods success and fail messages +- Improve CRUD methods services +- Add new dictionaries words diff --git a/src/Http/Controllers/Traits/CRUDMethodsController.php b/src/Http/Controllers/Traits/CRUDMethodsController.php index 4c1da9f..e710ba9 100644 --- a/src/Http/Controllers/Traits/CRUDMethodsController.php +++ b/src/Http/Controllers/Traits/CRUDMethodsController.php @@ -22,7 +22,14 @@ public function store(Request $request) ->setRequest($request) ->store(); - return new $this->resource($model); + if (!$model) { + return response()->json([ + 'message' => trans('core::message.store_failed') + ], 400); + } + + return (new $this->resource($model)) + ->additional(['message' => trans('core::message.stored')]); } public function show(Request $request) @@ -44,17 +51,28 @@ public function update(Request $request) ->setRequest($request) ->update($id); - return new $this->resource($model, 200); + if (!$model) { + return response()->json([ + 'message' => trans('core::message.update_failed') + ], 400); + } + + return (new $this->resource($model)) + ->additional(['message' => trans('core::message.updated')]); } public function destroy(Request $request) { $id = Arr::last(func_get_args()); - $this->service->destroy($id); + if (!$this->service->destroy($id)) { + return response()->json([ + 'message' => trans('core::message.delete_failed') + ], 400); + } return response()->json([ 'message' => trans('core::message.deleted') ]); } -} +} \ No newline at end of file diff --git a/src/Http/Controllers/Traits/SoftDeleteMethodsController.php b/src/Http/Controllers/Traits/SoftDeleteMethodsController.php index 4e82155..6ed6ea8 100644 --- a/src/Http/Controllers/Traits/SoftDeleteMethodsController.php +++ b/src/Http/Controllers/Traits/SoftDeleteMethodsController.php @@ -11,7 +11,11 @@ public function forceDestroy(Request $request) { $id = Arr::last(func_get_args()); - $this->service->forceDestroy($id); + if (!$this->service->forceDestroy($id)) { + return response()->json([ + 'message' => trans('core::message.delete_failed') + ], 400); + } return response()->json([ 'message' => trans('core::message.deleted') @@ -22,7 +26,11 @@ public function restore(Request $request) { $id = Arr::last(func_get_args()); - $this->service->restore($id); + if (!$this->service->restore($id)) { + return response()->json([ + 'message' => trans('core::message.restore_failed') + ], 400); + } return response()->json([ 'message' => trans('core::message.restored') diff --git a/src/Services/Traits/CRUDMethodsService.php b/src/Services/Traits/CRUDMethodsService.php index f65c5a9..9c78eca 100644 --- a/src/Services/Traits/CRUDMethodsService.php +++ b/src/Services/Traits/CRUDMethodsService.php @@ -2,6 +2,8 @@ namespace Txsoura\Core\Services\Traits; +use Exception; +use Illuminate\Support\Facades\Log; use Txsoura\Core\Http\Requests\QueryParamsRequest; trait CRUDMethodsService @@ -19,7 +21,13 @@ public function store() { $this->request = resolve($this->storeRequest); - return $this->model()::create($this->request->validated()); + try { + return $this->model()::create($this->request->validated()); + } catch (Exception $e) { + Log::error($e->getMessage()); + + return false; + } } public function show($id) @@ -33,14 +41,31 @@ public function update($id) { $this->request = resolve($this->updateRequest); - $model = $this->repository->findOrFail($id); - $model->update($this->request->validated()); - return $model; + $model = $this->model()::findOrFail($id); + + try { + $model->update($this->request->validated()); + + return $model; + } catch (Exception $e) { + Log::error($e->getMessage()); + + return false; + } } public function destroy($id) { $model = $this->model()::findOrFail($id); - $model->delete(); + + try { + $model->delete(); + + return true; + } catch (Exception $e) { + Log::error($e->getMessage()); + + return false; + } } } diff --git a/src/Services/Traits/SoftDeleteMethodsService.php b/src/Services/Traits/SoftDeleteMethodsService.php index a62aa26..769f62b 100644 --- a/src/Services/Traits/SoftDeleteMethodsService.php +++ b/src/Services/Traits/SoftDeleteMethodsService.php @@ -2,6 +2,9 @@ namespace Txsoura\Core\Services\Traits; +use Exception; +use Illuminate\Support\Facades\Log; + trait SoftDeleteMethodsService { public function forceDestroy($id) @@ -9,7 +12,16 @@ public function forceDestroy($id) $model = $this->model()::withTrashed() ->whereId($id) ->firstOrFail(); - $model->forceDelete(); + + try { + $model->forceDelete(); + + return true; + } catch (Exception $e) { + Log::error($e->getMessage()); + + return false; + } } public function restore($id) @@ -17,6 +29,15 @@ public function restore($id) $model = $this->model()::withTrased() ->whereId($id) ->firstOrFail(); - $model->restore(); + + try { + $model->restore(); + + return true; + } catch (Exception $e) { + Log::error($e->getMessage()); + + return false; + } } } diff --git a/src/resources/lang/en/message.php b/src/resources/lang/en/message.php index 2b76393..3da54dd 100644 --- a/src/resources/lang/en/message.php +++ b/src/resources/lang/en/message.php @@ -5,7 +5,13 @@ 'entry_not_found' => 'Entry for :model not found', 'relation_not_found' => 'Relation not found', 'not_found' => 'Not found', + 'stored' => 'Stored successfully', + 'updated' => 'Updated successfully', 'deleted' => 'Deleted successfully', 'restored' => 'Restored successfully', + 'store_failed' => 'Store failed', + 'update_failed' => 'Update failed', + 'delete_failed' => 'Delete failed', + 'restore_failed' => 'Restore failed', 'invalid_signature' => 'Invalid signature', ]; diff --git a/src/resources/lang/es/message.php b/src/resources/lang/es/message.php index 6b36215..5a62d8f 100644 --- a/src/resources/lang/es/message.php +++ b/src/resources/lang/es/message.php @@ -5,7 +5,13 @@ 'entry_not_found' => 'Entrada de :model no encontrada', 'relation_not_found' => 'Relación no encontrada', 'not_found' => 'No encontrado', + 'stored' => 'Almacenado exitosamente', + 'updated' => 'Actualizado con éxito', 'deleted' => 'Borrado exitosamente', 'restored' => 'Restaurado exitosamente', + 'store_failed' => 'La tienda falló', + 'update_failed' => 'Actualización fallida', + 'delete_failed' => 'Eliminación fallida', + 'restore_failed' => 'Restauración fallida', 'invalid_signature' => 'Firma inválida', ]; diff --git a/src/resources/lang/pt/message.php b/src/resources/lang/pt/message.php index 642f900..4126701 100644 --- a/src/resources/lang/pt/message.php +++ b/src/resources/lang/pt/message.php @@ -5,7 +5,13 @@ 'entry_not_found' => 'Entrada para :model não encontrado', 'relation_not_found' => 'Relação não encontrada', 'not_found' => 'Não encontrado', + 'stored' => 'Armazenado com sucesso', + 'updated' => 'Atualizado com sucesso', 'deleted' => 'Apagado com successo', 'restored' => 'Restaurado com successo', + 'store_failed' => 'Armazenado falhou', + 'update_failed' => 'Atualização falhou', + 'delete_failed' => 'Exclusão falhou', + 'restore_failed' => 'Restauração falhou', 'invalid_signature' => 'Assinatura inválida', ];