From 1c3a6f89f57fc43e990d83ba88dcd43ae75c6c7f Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Sat, 22 Aug 2020 12:23:27 +0700 Subject: [PATCH] use transaction on menu order --- src/Controllers/Users/MenuController.php | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Controllers/Users/MenuController.php b/src/Controllers/Users/MenuController.php index 9b7bab0..04b796b 100644 --- a/src/Controllers/Users/MenuController.php +++ b/src/Controllers/Users/MenuController.php @@ -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')); }