Skip to content

Commit

Permalink
#2 delete機能のエラー文を表示するように変更.エラー処理の変更.
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaroYutaro committed Dec 13, 2018
1 parent ed9aebc commit f532526
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
36 changes: 30 additions & 6 deletions app/js/function.js
Expand Up @@ -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));

Expand Down Expand Up @@ -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...');
})
});

Expand All @@ -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();
Expand Down Expand Up @@ -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');
});
Expand All @@ -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] + '<br>');
});

$('#delete-error-alert').html(errorText).show();

}

})
.fail(() => {
Expand Down
46 changes: 32 additions & 14 deletions app/php/Class/Crud.php
Expand Up @@ -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;
Expand All @@ -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`=?';
Expand All @@ -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';
Expand All @@ -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;
}
}
21 changes: 15 additions & 6 deletions app/php/delete.php
Expand Up @@ -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";
}
$response = ['err' => ['要素が足りません.'], 'data' => []];

}

echo json_encode($response);
2 changes: 1 addition & 1 deletion app/php/update.php
Expand Up @@ -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];

Expand Down
3 changes: 1 addition & 2 deletions index.php
Expand Up @@ -104,8 +104,7 @@
</button>
</div>
<div class="modal-body">
<div id="delete-error-alert" class="alert alert-danger" role="alert"
style="display: none;"></div>
<div id="delete-error-alert" class="alert alert-danger" role="alert" style="display: none;"></div>
本当に削除しますか?
</div>
<div class="modal-footer">
Expand Down

0 comments on commit f532526

Please sign in to comment.