Skip to content

Commit

Permalink
use transaction on menu order
Browse files Browse the repository at this point in the history
  • Loading branch information
agungsugiarto committed Aug 22, 2020
1 parent c0ed311 commit 1c3a6f8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Controllers/Users/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,29 @@ public function new()
$data = $this->request->getJSON();
$menu = new MenuEntity();

$i = 1;
foreach ($data as $item) {
if (isset($item->parent_id)) {
$menu->parent_id = $item->parent_id;
$menu->sequence = $i++;
} else {
$menu->parent_id = 0;
$menu->sequence = $i++;
try {
$this->db->transBegin();

$i = 1;
foreach ($data as $item) {
if (isset($item->parent_id)) {
$menu->parent_id = $item->parent_id;
$menu->sequence = $i++;
} else {
$menu->parent_id = 0;
$menu->sequence = $i++;
}

$this->menu->update($item->id, $menu);
}
$result = $this->menu->update($item->id, $menu);
}
} catch (\Exception $e) {
$this->db->transRollback();

if (!$result) {
return $this->fail($result, lang('boilerplate.menu.msg.msg_fail_order'));
return $this->fail(lang('boilerplate.menu.msg.msg_fail_order'));
}

$this->db->transCommit();

return $this->respondUpdated($result, lang('boilerplate.menu.msg.msg_update'));
}

Expand Down

0 comments on commit 1c3a6f8

Please sign in to comment.