Skip to content

Commit

Permalink
Feat/vic add eloquent status messages (#4) (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
txsoura authored Jun 15, 2021
1 parent 541c68e commit 4900f14
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 22 additions & 4 deletions src/Http/Controllers/Traits/CRUDMethodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
]);
}
}
}
12 changes: 10 additions & 2 deletions src/Http/Controllers/Traits/SoftDeleteMethodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
35 changes: 30 additions & 5 deletions src/Services/Traits/CRUDMethodsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Txsoura\Core\Services\Traits;

use Exception;
use Illuminate\Support\Facades\Log;
use Txsoura\Core\Http\Requests\QueryParamsRequest;

trait CRUDMethodsService
Expand All @@ -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)
Expand All @@ -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;
}
}
}
25 changes: 23 additions & 2 deletions src/Services/Traits/SoftDeleteMethodsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@

namespace Txsoura\Core\Services\Traits;

use Exception;
use Illuminate\Support\Facades\Log;

trait SoftDeleteMethodsService
{
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)
{
$model = $this->model()::withTrased()
->whereId($id)
->firstOrFail();
$model->restore();

try {
$model->restore();

return true;
} catch (Exception $e) {
Log::error($e->getMessage());

return false;
}
}
}
6 changes: 6 additions & 0 deletions src/resources/lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
6 changes: 6 additions & 0 deletions src/resources/lang/es/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
6 changes: 6 additions & 0 deletions src/resources/lang/pt/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

0 comments on commit 4900f14

Please sign in to comment.