Skip to content

Commit

Permalink
trigger delete cacahe menu and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed May 27, 2020
1 parent 9a69593 commit 084159a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Controllers/Users/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ public function edit($id)
*/
public function delete($id)
{
if ($found = $this->menu->delete($id)) {
return $this->respondCreated($found, lang('boilerplate.menu.msg.msg_delete'));
if (!$this->menu->delete($id)) {
return $this->failNotFound(lang('boilerplate.menu.msg.msg_get_fail'));
}

return $this->failNotFound(lang('boilerplate.menu.msg.msg_get_fail'));
return $this->respondDeleted(['id' => $id], lang('boilerplate.menu.msg.msg_delete'));
}
}
30 changes: 20 additions & 10 deletions src/Models/MenuModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class MenuModel extends Model
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';

// trigger
protected $afterInsert = ['deleteCacheMenu'];
protected $afterUpdate = ['deleteCacheMenu'];
protected $afterDelete = ['deleteCacheMenu'];

protected $validationRules = [
'title' => 'required|min_length[10]|max_length[60]',
'parent_id' => 'required',
Expand All @@ -44,16 +49,9 @@ class MenuModel extends Model
*/
public function getMenuById($id)
{
switch ($this->db->DBDriver) {
case 'MySQLi':
// do mysqli
return $this->getMenuDriverMySQLi($id);
break;
case 'Postgre':
// do postgres
return $this->getMenuDRiverPostgre($id);
break;
}
return $this->db->DBDriver === 'Postgre'
? $this->getMenuDRiverPostgre($id)
: $this->getMenuDriverMySQLi($id);
}

/**
Expand Down Expand Up @@ -119,4 +117,16 @@ private function getMenuDRiverPostgre($id)
->get()
->getResultArray();
}

/**
* deleteCacheMenu.
*
* @return void
*/
protected function deleteCacheMenu()
{
if (cache(user()->id.'_group_menu')) {
cache()->delete(user()->id.'_group_menu');
}
}
}
2 changes: 1 addition & 1 deletion src/Views/Menu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function menu() {
}).done((data, textStatus, jqXHR) => {
Toast.fire({
icon: 'success',
title: textStatus,
title: jqXHR.statusText,
});
$('.dd').nestable('destroy');
menu();
Expand Down

0 comments on commit 084159a

Please sign in to comment.