Skip to content

Commit

Permalink
Merge pull request #221 from uasoft-indonesia/feature/v2/soft-delete
Browse files Browse the repository at this point in the history
[badaso v2] add feature soft delete
  • Loading branch information
rizkiheryandi authored Jul 4, 2021
2 parents 062a579 + 1a0e133 commit 91c0beb
Show file tree
Hide file tree
Showing 24 changed files with 8,978 additions and 108 deletions.
7,196 changes: 7,196 additions & 0 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Commands/BadasoSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function updateWebpackMix()
if ($this->checkExist($mix_file, $search)) {
$data =
<<<'EOT'
// Badaso
mix
.js(
Expand Down
10 changes: 5 additions & 5 deletions src/ContentManager/ContentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ContentGenerator
/** @var string Data type Delete Statement */
const DELETE_STATEMENT = <<<'TXT'
$data_type = Badaso::model('DataType')->where('name', '%s')->first();
if ($data_type) {
Badaso::model('DataType')->where('name', '%s')->delete();
}
Expand All @@ -22,14 +22,14 @@ class ContentGenerator
/** @var string Menu Insert Statement */
const MENU_INSERT_STATEMENT = <<<'TXT'
$menu = Badaso::model('Menu')->where('key', config('badaso.default_menu'))->firstOrFail();
$menu_item = Badaso::model('MenuItem')
->where('menu_id', $menu->id)
->where('url', '%s')
->first();
$order = Badaso::model('MenuItem')->highestOrderMenuItem();
if (!is_null($menu_item)) {
$menu_item->fill([
'title' => '%s',
Expand Down Expand Up @@ -58,7 +58,7 @@ class ContentGenerator
/** @var string Menu Delete Statement */
const MENU_DELETE_STATEMENT = <<<'TXT'
$menuItem = Badaso::model('MenuItem')::where('url', '%s');
if ($menuItem->exists()) {
$menuItem->delete();
}
Expand Down
87 changes: 84 additions & 3 deletions src/Controllers/BadasoBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function browse(Request $request)
$slug = $this->getSlug($request);
$data_type = $this->getDataType($slug);

$data = $this->getDataList($slug, $request->all());
$only_data_soft_delete = $request->showSoftDelete == 'true';

$data = $this->getDataList($slug, $request->all(), $only_data_soft_delete);

return ApiResponse::entity($data_type, $data);
} catch (Exception $e) {
Expand Down Expand Up @@ -167,11 +169,13 @@ public function delete(Request $request)
'data.*.value' => ['required'],
]);

$is_hard_delete = $request->isHardDelete == 'true';

$slug = $this->getSlug($request);
$data_type = $this->getDataType($slug);

$data = $this->createDataFromRaw($request->input('data') ?? [], $data_type);
$this->deleteData($data, $data_type);
$this->deleteData($data, $data_type, $is_hard_delete);

activity($data_type->display_name_singular)
->causedBy(auth()->user() ?? null)
Expand All @@ -192,10 +196,87 @@ public function delete(Request $request)
}
}

public function restore(Request $request)
{
DB::beginTransaction();

try {
$request->validate([
'slug' => 'required',
'data' => [
'required',
],
'data.*.field' => ['required'],
'data.*.value' => ['required'],
]);

$slug = $this->getSlug($request);
$data_type = $this->getDataType($slug);

$data = $this->createDataFromRaw($request->input('data') ?? [], $data_type);
$this->restoreData($data, $data_type);

activity($data_type->display_name_singular)
->causedBy(auth()->user() ?? null)
->withProperties($data)
->log($data_type->display_name_singular.' has been restore');

DB::commit();

return ApiResponse::entity($data_type);
} catch (Exception $e) {
DB::rollBack();

return ApiResponse::failed($e);
}
}

public function deleteMultiple(Request $request)
{
DB::beginTransaction();

try {
$request->validate([
'slug' => 'required',
'data' => [
'required',
],
'data.*.field' => ['required'],
'data.*.value' => ['required'],
]);

$is_hard_delete = $request->isHardDelete == 'true';

$slug = $this->getSlug($request);
$data_type = $this->getDataType($slug);

$data = $this->createDataFromRaw($request->input('data') ?? [], $data_type);
$ids = $data['ids'];
$id_list = explode(',', $ids);
foreach ($id_list as $id) {
$should_delete['id'] = $id;
$this->deleteData($should_delete, $data_type, $is_hard_delete);
}

activity($data_type->display_name_singular)
->causedBy(auth()->user() ?? null)
->withProperties($data)
->log($data_type->display_name_singular.' has been bulk deleted');

DB::commit();

return ApiResponse::entity($data_type);
} catch (Exception $e) {
DB::rollBack();

return ApiResponse::failed($e);
}
}

public function restoreMultiple(Request $request)
{
DB::beginTransaction();

try {
$request->validate([
'slug' => 'required',
Expand All @@ -214,7 +295,7 @@ public function deleteMultiple(Request $request)
$id_list = explode(',', $ids);
foreach ($id_list as $id) {
$should_delete['id'] = $id;
$this->deleteData($should_delete, $data_type);
$this->restoreData($should_delete, $data_type);
}

activity($data_type->display_name_singular)
Expand Down
21 changes: 21 additions & 0 deletions src/Controllers/BadasoCRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ function ($attribute, $value, $fail) use ($request) {
'rows.*.display_name' => 'required',
'display_name_singular' => 'required',
'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'],
'is_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) {
if (isset($request->name) && $val) {
if (! Schema::hasColumn($request->name, 'deleted_at')) {
$failed(__('badaso::validation.crud.table_deleted_at_not_exists', [
'table_name' => $request->name,
]));
}
}
}],
]);

$table_name = $request->input('name');
Expand Down Expand Up @@ -188,6 +197,7 @@ function ($attribute, $value, $fail) use ($request) {
$data_type->details = $request->input('details');
$data_type->controller = $request->input('controller');
$data_type->notification = json_encode($request->input('notification'));
$data_type->is_soft_delete = $request->input('create_soft_delete');
$data_type->save();

DataRow::where('data_type_id', $data_type->id)->delete();
Expand Down Expand Up @@ -293,7 +303,17 @@ function ($attribute, $value, $fail) use ($request) {
'rows.*.display_name' => 'required',
'display_name_singular' => 'required',
'notification.*.event' => ['in:onCreate,onRead,onUpdate,onDelete'],
'create_soft_delete' => ['required', 'boolean', function ($att, $val, $failed) use ($request) {
if (isset($request->name) && $val) {
if (! Schema::hasColumn($request->name, 'deleted_at')) {
$failed(__('badaso::validation.crud.table_deleted_at_not_exists', [
'table_name' => $request->name,
]));
}
}
}],
]);

$table_name = $request->input('name');
$new_data_type = new DataType();
$new_data_type->name = $table_name;
Expand All @@ -312,6 +332,7 @@ function ($attribute, $value, $fail) use ($request) {
$new_data_type->description = $request->input('description');
$new_data_type->details = $request->input('details');
$new_data_type->notification = json_encode($request->input('notification'));
$new_data_type->is_soft_delete = $request->input('create_soft_delete');
$new_data_type->save();

$data_rows = $request->input('rows') ?? [];
Expand Down
109 changes: 91 additions & 18 deletions src/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function getContentByType($data_type, $data_row, $value)
return $return_value;
}

public function getDataList($slug, $request)
public function getDataList($slug, $request, $only_data_soft_delete = false)
{
$data_type = $this->getDataType($slug);
$data = [];
Expand All @@ -214,15 +214,15 @@ public function getDataList($slug, $request)

if ($data_type->model_name) {
if ($data_type->server_side) {
$records = GetData::serverSideWithModel($data_type, $builder_params);
$records = GetData::serverSideWithModel($data_type, $builder_params, $only_data_soft_delete);
} else {
$records = GetData::clientSideWithModel($data_type, $builder_params);
$records = GetData::clientSideWithModel($data_type, $builder_params, $only_data_soft_delete);
}
} else {
if ($data_type->server_side) {
$records = GetData::serverSideWithQueryBuilder($data_type, $builder_params);
$records = GetData::serverSideWithQueryBuilder($data_type, $builder_params, $only_data_soft_delete);
} else {
$records = GetData::clientSideWithQueryBuilder($data_type, $builder_params);
$records = GetData::clientSideWithQueryBuilder($data_type, $builder_params, $only_data_soft_delete);
}
}

Expand Down Expand Up @@ -318,8 +318,7 @@ public function updateData($data, $data_type)
'upload_image_multiple',
'upload_file',
'upload_file_multiple',
])
) {
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
if (is_array($value)) {
Expand Down Expand Up @@ -352,8 +351,7 @@ public function updateData($data, $data_type)
'upload_image_multiple',
'upload_file',
'upload_file_multiple',
])
) {
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
if (is_array($value)) {
Expand All @@ -380,7 +378,7 @@ public function updateData($data, $data_type)
];
}

public function deleteData($data, $data_type)
public function deleteData($data, $data_type, $is_hard_delete = false)
{
$data_rows = $data_type->dataRows;
$model = null;
Expand All @@ -390,35 +388,110 @@ public function deleteData($data, $data_type)
$model = $model::find($id);
if (! is_null($model)) {
foreach ($data_rows as $data_row) {
if (in_array($data_row->type, ['upload_image',
if (in_array($data_row->type, [
'upload_image',
'upload_image_multiple',
'upload_file',
'upload_file_multiple',
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
$this->handleDeleteFile($file);
}
}
}
if ($is_hard_delete) {
$model->delete();
} else {
if ($data_type->is_soft_delete) {
$model->update([
'deleted_at' => date('Y-m-d H:i:s'),
]);
} else {
$model->delete();
}
}
}
} else {
$model = DB::table($data_type->name)->where('id', $id)->first();
if (! is_null($model)) {
foreach ($data_rows as $data_row) {
if (in_array($data_row->type, [
'upload_image',
'upload_image_multiple',
'upload_file',
'upload_file_multiple',
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
$this->handleDeleteFile($file);
}
}
}
$model = DB::table($data_type->name)->where('id', $id);

if ($is_hard_delete) {
$model->delete();
} else {
if ($data_type->is_soft_delete) {
$model->update([
'deleted_at' => date('Y-m-d H:i:s'),
]);
} else {
$model->delete();
}
}
}
}
}

public function restoreData($data, $data_type)
{
$data_rows = $data_type->dataRows;
$model = null;
$id = $data['id'];
if ($data_type->model_name) {
$model = app($data_type->model_name);
$model = $model::find($id);
if (! is_null($model)) {
foreach ($data_rows as $data_row) {
if (in_array($data_row->type, [
'upload_image',
'upload_image_multiple',
'upload_file',
'upload_file_multiple', ])
) {
'upload_file_multiple',
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
$this->handleDeleteFile($file);
}
}
}
$model->delete();
$model->update([
'deleted_at' => null,
]);
}
} else {
$model = DB::table($data_type->name)->where('id', $id)->first();
if (! is_null($model)) {
foreach ($data_rows as $data_row) {
if (in_array($data_row->type, ['upload_image',
if (in_array($data_row->type, [
'upload_image',
'upload_image_multiple',
'upload_file',
'upload_file_multiple', ])
) {
'upload_file_multiple',
])) {
$files = explode(',', $model->{$data_row->field});
foreach ($files as $file) {
$this->handleDeleteFile($file);
}
}
}
DB::table($data_type->name)->where('id', $id)->delete();
$model = DB::table($data_type->name)->where('id', $id);

$model->update([
'deleted_at' => null,
]);
}
}
}
Expand Down
Loading

0 comments on commit 91c0beb

Please sign in to comment.