Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from YutaroYutaro/feature/issue_#2
Feature/issue #2
- Loading branch information
Showing
12 changed files
with
668 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
|
||
\.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
$('#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)); | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: './app/php/create.php', | ||
data: fd, | ||
processData: false, | ||
contentType: false, | ||
}) | ||
.done((res) => { | ||
let data = JSON.parse(res); | ||
|
||
if (data['err'].length === 0) { | ||
$('#bbs-body').prepend( | ||
'<div class="card mb-3">' + | ||
'<div id="' + data['data']['id'] + '" class="card-body">\n' + | ||
'<h5 class="card-title">' + data['data']['title'] + '</h5>\n' + | ||
'<p class="card-text card-comment">' + data['data']['comment'] + '</p>\n' + | ||
'<p class="card-text">\n' + | ||
'<small class="text-muted">' + data['data']['created_at'] + '</small>\n' + | ||
'</p>\n' + | ||
'<button type="button" class="btn btn-success updateButton" data-toggle="modal"\n' + | ||
'data-target="#updateModal">修正する\n' + | ||
'</button>\n' + | ||
'<button type="button" class="btn btn-danger deleteButton" data-toggle="modal"\n' + | ||
'data-target="#deleteModal">削除する\n' + | ||
'</button>\n' + | ||
'</div>' + | ||
'</div>' | ||
); | ||
} else { | ||
let errorText = ''; | ||
|
||
Object.keys(data['err']).forEach(function (key) { | ||
errorText += (data['err'][key] + '<br>'); | ||
}); | ||
|
||
$('#create-error-alert').html(errorText).show(); | ||
} | ||
|
||
// console.log(data); | ||
}) | ||
.fail(() => { | ||
$('#create-error-alert').html('通信に失敗しました.').show(); | ||
// console.log('create fail...'); | ||
}) | ||
}); | ||
|
||
let updateId; | ||
|
||
$(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(); | ||
let comment = $('#' + updateId + ' .card-comment').html(); | ||
|
||
$('#update-title').val(title); | ||
$('#update-comment').val(comment); | ||
}); | ||
|
||
$('#modal-update-button').on('click', function () { | ||
let fd = new FormData($('#updateForm').get(0)); | ||
fd.append('id', updateId); | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: './app/php/update.php', | ||
data: fd, | ||
processData: false, | ||
contentType: false, | ||
}) | ||
.done((res) => { | ||
let data = JSON.parse(res); | ||
|
||
if (data['err'].length === 0) { | ||
$('#' + data['data']['id'] + ' .card-title').html(data['data']['title']); | ||
$('#' + data['data']['id'] + ' .card-comment').html(data['data']['comment']); | ||
$('#updateModal').modal('hide'); | ||
|
||
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>'); | ||
}); | ||
|
||
$('#update-error-alert').html(errorText).show(); | ||
|
||
// console.log(data['err']); | ||
} | ||
|
||
}) | ||
.fail(() => { | ||
$('#update-error-alert').html('通信に失敗しました.').show(); | ||
// console.log('create fail...'); | ||
}) | ||
}); | ||
|
||
let deleteId; | ||
|
||
$(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'); | ||
}); | ||
|
||
$('#modal-delete-button').on('click', function () { | ||
$.ajax({ | ||
url: './app/php/delete.php', | ||
type: 'POST', | ||
data: { | ||
id: deleteId | ||
} | ||
}) | ||
.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(() => { | ||
$('#delete-error-alert').html('通信に失敗しました.').show(); | ||
// console.log('delete fail...'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: nishikawa.yutaro | ||
* Date: 2018-12-11 | ||
* Time: 17:46 | ||
*/ | ||
|
||
include __DIR__ . '/Validation.php'; | ||
|
||
class BbsValidation extends Validation | ||
{ | ||
public function IdValidation($id) | ||
{ | ||
$errors = []; | ||
|
||
return $errors; | ||
} | ||
|
||
public function TitleValidation($title) | ||
{ | ||
$errors = []; | ||
|
||
if ($this->MaxSize($title, 50)) $errors['title_length'] = 'タイトルは50文字以内で入力してください.'; | ||
if ($this->Required($title)) $errors['title_required'] = 'タイトルを入力してください.'; | ||
|
||
return $errors; | ||
} | ||
|
||
public function CommentValidation($comment) | ||
{ | ||
$errors = []; | ||
|
||
if ($this->MaxSize($comment, 100)) $errors['comment_length'] = 'コメントは100文字以内で入力してください.'; | ||
if ($this->Required($comment)) $errors['comment_required'] = 'コメントを入力してください.'; | ||
|
||
return $errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: nishikawa.yutaro | ||
* Date: 2018-12-10 | ||
* Time: 18:27 | ||
*/ | ||
|
||
include __DIR__ . '/MySql.php'; | ||
|
||
class Crud extends MySql | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function Create($title, $comment, $createdAt) | ||
{ | ||
$res = 0; | ||
|
||
$this->dbh->beginTransaction(); | ||
|
||
try { | ||
$sql = 'INSERT INTO `bbs` (`title`, `comment`, `created_at`) VALUES (?, ?, ?)'; | ||
|
||
$data = [$title, $comment, $createdAt]; | ||
|
||
$stmt = $this->dbh->prepare($sql); | ||
|
||
$stmt->execute($data); | ||
|
||
$res = $this->dbh->lastInsertId('id'); | ||
|
||
$this->dbh->commit(); | ||
|
||
} catch (PDOException $e) { | ||
error_log($e->getMessage()); | ||
$this->dbh->rollBack(); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
public function Read() | ||
{ | ||
$contents = []; | ||
|
||
$this->dbh->beginTransaction(); | ||
|
||
try { | ||
$sql = 'SELECT * FROM `bbs` ORDER BY `id` DESC'; | ||
|
||
$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']]; | ||
} | ||
|
||
$this->dbh->commit(); | ||
|
||
} catch (PDOException $e) { | ||
error_log($e->getMessage()); | ||
$this->dbh->rollBack(); | ||
} | ||
|
||
return $contents; | ||
} | ||
|
||
public function Update($id, $title, $comment) | ||
{ | ||
$res = 0; | ||
|
||
$this->dbh->beginTransaction(); | ||
|
||
try{ | ||
$sql = 'UPDATE `bbs` SET `title`=?, `comment`=? WHERE `id`=?'; | ||
|
||
$data = [$title, $comment, $id]; | ||
|
||
$stmt = $this->dbh->prepare($sql); | ||
|
||
$stmt->execute($data); | ||
|
||
$res = $id; | ||
|
||
$this->dbh->commit(); | ||
|
||
} catch (PDOException $e) { | ||
error_log($e->getMessage()); | ||
$this->dbh->rollBack(); | ||
} | ||
|
||
return $res; | ||
} | ||
|
||
public function Delete($id) | ||
{ | ||
$res = 0; | ||
|
||
$this->dbh->beginTransaction(); | ||
|
||
try { | ||
$sql = 'DELETE FROM `bbs` WHERE `id` = :id'; | ||
|
||
$stmt = $this->dbh->prepare($sql); | ||
|
||
$stmt->bindValue(':id', $id, PDO::PARAM_INT); | ||
|
||
$stmt->execute(); | ||
|
||
$res = $id; | ||
|
||
$this->dbh->commit(); | ||
|
||
} catch (PDOException $e) { | ||
error_log($e->getMessage()); | ||
$this->dbh->rollBack(); | ||
} | ||
|
||
return $res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: nishikawa.yutaro | ||
* Date: 2018-12-10 | ||
* Time: 17:52 | ||
*/ | ||
|
||
class MySql | ||
{ | ||
private $dbIni; | ||
|
||
private $dsn; | ||
private $user; | ||
private $password; | ||
|
||
protected $dbh; | ||
|
||
public function __construct() | ||
{ | ||
$this->dbIni = parse_ini_file(__DIR__ . '/configs/database.ini'); | ||
|
||
$this->dsn = $this->dbIni['dsn']; | ||
$this->user = $this->dbIni['user']; | ||
$this->password = $this->dbIni['password']; | ||
|
||
$this->dbh = new PDO($this->dsn, $this->user, $this->password); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
// TODO: Implement __destruct() method. | ||
$this->dbh = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: nishikawa.yutaro | ||
* Date: 2018-12-11 | ||
* Time: 17:35 | ||
*/ | ||
|
||
class Validation | ||
{ | ||
public function MaxSize($str, $size) { | ||
return (mb_strlen($str, 'UTF-8') > $size); | ||
} | ||
|
||
public function Required($str){ | ||
return (empty($str)); | ||
} | ||
} |
Oops, something went wrong.