From f53252669eb7140604df69f7081b7473c98ee409 Mon Sep 17 00:00:00 2001 From: yutaro Date: Thu, 13 Dec 2018 12:54:00 +0900 Subject: [PATCH] =?UTF-8?q?#2=20delete=E6=A9=9F=E8=83=BD=E3=81=AE=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E6=96=87=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4=EF=BC=8E?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=A6=E7=90=86=E3=81=AE=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=EF=BC=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/js/function.js | 36 +++++++++++++++++++++++++++------ app/php/Class/Crud.php | 46 +++++++++++++++++++++++++++++------------- app/php/delete.php | 21 +++++++++++++------ app/php/update.php | 2 +- index.php | 3 +-- 5 files changed, 79 insertions(+), 29 deletions(-) diff --git a/app/js/function.js b/app/js/function.js index 451a5d4..9e05301 100644 --- a/app/js/function.js +++ b/app/js/function.js @@ -2,6 +2,7 @@ $('#createButton').on('click', function () { $('#success-alert').hide().html(''); $('#update-error-alert').hide().html(''); $('#create-error-alert').hide().html(''); + $('#delete-error-alert').hide().html(''); let fd = new FormData($('#createForm').get(0)); @@ -43,11 +44,11 @@ $('#createButton').on('click', function () { $('#create-error-alert').html(errorText).show(); } - console.log(data); + // console.log(data); }) .fail(() => { $('#create-error-alert').html('通信に失敗しました.').show(); - console.log('create fail...'); + // console.log('create fail...'); }) }); @@ -57,6 +58,7 @@ $(document).on('click', '.updateButton', function () { $('#success-alert').hide().html(''); $('#update-error-alert').hide().html(''); $('#create-error-alert').hide().html(''); + $('#delete-error-alert').hide().html(''); updateId = $(this).parent().attr("id"); let title = $('#' + updateId + ' .card-title').html(); @@ -118,6 +120,7 @@ $(document).on('click', '.deleteButton', function () { $('#success-alert').hide().html(''); $('#update-error-alert').hide().html(''); $('#create-error-alert').hide().html(''); + $('#delete-error-alert').hide().html(''); deleteId = $(this).parent().attr('id'); }); @@ -130,10 +133,31 @@ $('#modal-delete-button').on('click', function () { id: deleteId } }) - .done((data) => { - console.log('ajax success: ' + data); - $('#deleteModal').modal('hide'); - $('#' + deleteId).parent().remove(); + .done((res) => { + // console.log('ajax success: ' + data); + let data = JSON.parse(res); + + if (data['err'].length === 0) { + $('#deleteModal').modal('hide'); + $('#' + deleteId).parent().remove(); + + let position = $('#success-alert').html('削除に成功しました.').show().offset().top; + + $('html, body').animate({ + scrollTop: position + }, { + queue: false + }); + } else { + let errorText = ''; + + Object.keys(data['err']).forEach(function (key) { + errorText += (data['err'][key] + '
'); + }); + + $('#delete-error-alert').html(errorText).show(); + + } }) .fail(() => { diff --git a/app/php/Class/Crud.php b/app/php/Class/Crud.php index 4cb6f88..d5501a6 100644 --- a/app/php/Class/Crud.php +++ b/app/php/Class/Crud.php @@ -44,16 +44,26 @@ public function Create($title, $comment, $createdAt) public function Read() { - $sql = 'SELECT * FROM `bbs` ORDER BY `id` DESC'; + $contents = []; - $stmt = $this->dbh->prepare($sql); + $this->dbh->beginTransaction(); - $stmt->execute(); + try { + $sql = 'SELECT * FROM `bbs` ORDER BY `id` DESC'; - $contents = []; + $stmt = $this->dbh->prepare($sql); + + $stmt->execute(); - while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { - $contents[] = ['id' => $result['id'], 'title' => $result['title'], 'comment' => $result['comment'], 'created_at' => $result['created_at']]; + while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { + $contents[] = ['id' => $result['id'], 'title' => $result['title'], 'comment' => $result['comment'], 'created_at' => $result['created_at']]; + } + + $this->dbh->commit(); + + } catch (PDOException $e) { + error_log($e->getMessage()); + $this->dbh->rollBack(); } return $contents; @@ -62,7 +72,8 @@ public function Read() public function Update($id, $title, $comment) { $res = 0; - $error = 'update query fail...'; + + $this->dbh->beginTransaction(); try{ $sql = 'UPDATE `bbs` SET `title`=?, `comment`=? WHERE `id`=?'; @@ -73,19 +84,23 @@ public function Update($id, $title, $comment) $stmt->execute($data); - $res = $stmt->rowCount(); + $res = $id; + + $this->dbh->commit(); } catch (PDOException $e) { - $error = $e->getMessage(); + error_log($e->getMessage()); + $this->dbh->rollBack(); } - return empty($res) ? $error : $res; + return $res; } public function Delete($id) { $res = 0; - $error = 'delete query fail...'; + + $this->dbh->beginTransaction(); try { $sql = 'DELETE FROM `bbs` WHERE `id` = :id'; @@ -96,12 +111,15 @@ public function Delete($id) $stmt->execute(); - $res = $stmt->rowCount(); + $res = $id; + + $this->dbh->commit(); } catch (PDOException $e) { - $error = $e->getMessage(); + error_log($e->getMessage()); + $this->dbh->rollBack(); } - return empty($res) ? $error : $res; + return $res; } } \ No newline at end of file diff --git a/app/php/delete.php b/app/php/delete.php index d0d8747..e91c6ea 100644 --- a/app/php/delete.php +++ b/app/php/delete.php @@ -10,15 +10,24 @@ header('Content-type: text/plain; charset= UTF-8'); +$response = []; + if (isset($_POST['id'])) { - $id = $_POST['id']; + $errors = []; $crud = new Crud(); - $result = $crud->delete($id); - $res = ($result === 1) ? 'delete success: ' . $result : 'delete fail'; + $result = $crud->delete($_POST['id']); + + if ($result === 0) $errors[] = '削除に失敗しました.'; + + $data = ['id' => $_POST['id']]; + + $response = ['err' => $errors, 'data' => $data]; - echo json_encode($res); } else { - echo "Fail to ajax request"; -} \ No newline at end of file + $response = ['err' => ['要素が足りません.'], 'data' => []]; + +} + +echo json_encode($response); diff --git a/app/php/update.php b/app/php/update.php index f7dadcb..413a6db 100644 --- a/app/php/update.php +++ b/app/php/update.php @@ -40,7 +40,7 @@ $data = ['id' => $_POST['id'], 'title' => $_POST['title'], 'comment' => $_POST['comment']]; - if ($result !== 1) $errors[] = '更新に失敗しました.'; + if ($result === 0) $errors[] = '更新に失敗しました.'; $response = ['err' => $errors, 'data' => $data]; diff --git a/index.php b/index.php index 7c422cd..a934760 100644 --- a/index.php +++ b/index.php @@ -104,8 +104,7 @@