Modify Model become abstract, create database interactions and do test.
This commit is contained in:
parent
5ca179fac7
commit
a075e1c36e
153
App/Models/Post.php
Normal file
153
App/Models/Post.php
Normal file
@ -0,0 +1,153 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
class Post extends \Core\Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->createTable(
|
||||
'pengumuman',
|
||||
[
|
||||
'id int(3) NOT NULL AUTO_INCREMENT',
|
||||
'category int(3) NOT NULL',
|
||||
'created_at timestamp DEFAULT CURRENT_TIMESTAMP',
|
||||
'expired_at timestamp NOT NULL',
|
||||
'creator int(3) NOT NULL',
|
||||
'edited_at timestamp',
|
||||
'editor timestamp',
|
||||
'content varchar(255) NOT NULL',
|
||||
'PRIMARY KEY (id)'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function showAll()
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT * FROM pengumuman ORDER BY created_at";
|
||||
|
||||
if ($stmt = $db->query($sql)) {
|
||||
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function showSingle($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "SELECT * FROM pengumuman WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
if ($query->rowCount() === 1) {
|
||||
$result = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
// For tests
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function entry($fields, $vals)
|
||||
{
|
||||
if (count($fields) && count($vals) && count($fields) === count($vals)) {
|
||||
$keys = '`'.implode('`, `', $fields).'`';
|
||||
|
||||
$x = 1;
|
||||
$values = '';
|
||||
foreach ($vals as $val) {
|
||||
$values .= '?';
|
||||
if ($x < count($vals)) {
|
||||
$values .= ', ';
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "INSERT INTO pengumuman ({$keys}) VALUES ({$values})";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
$x = 1;
|
||||
|
||||
foreach ($vals as $val) {
|
||||
$query->bindValue($x, urldecode($val));
|
||||
$x++;
|
||||
}
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function update($args, $id)
|
||||
{
|
||||
if (count($args)) {
|
||||
$keys = array_keys($args);
|
||||
|
||||
$fields = [];
|
||||
foreach ($keys as $key) {
|
||||
$fields[] = $key.' = ?';
|
||||
}
|
||||
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "UPDATE pengumuman SET ".implode(', ', $fields)." WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
$x = 1;
|
||||
foreach ($args as $value) {
|
||||
$query->bindValue($x, $value);
|
||||
$x++;
|
||||
}
|
||||
$query->bindValue($x, $id);
|
||||
|
||||
if ($query->execute()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
try {
|
||||
$db = static::connectDB();
|
||||
|
||||
$sql = "DELETE FROM pengumuman WHERE id = ?";
|
||||
|
||||
$query = $db->prepare($sql);
|
||||
|
||||
if ($query->execute([$id])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ namespace Core;
|
||||
|
||||
use App\Config;
|
||||
|
||||
class Model
|
||||
abstract class Model
|
||||
{
|
||||
protected static $dsn;
|
||||
|
||||
@ -24,20 +24,9 @@ class Model
|
||||
}
|
||||
}
|
||||
|
||||
public function createTable()
|
||||
public function createTable($table, $fields = [])
|
||||
{
|
||||
$sql = "CREATE TABLE IF NOT EXISTS pengumuman (
|
||||
id int(3) NOT NULL AUTO_INCREMENT,
|
||||
category int(3) NOT NULL,
|
||||
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expire_at timestamp NOT NULL,
|
||||
creator int(3) NOT NULL,
|
||||
edited_at timestamp,
|
||||
editor int(3),
|
||||
content varchar(255) NOT NULL,
|
||||
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
$sql = "CREATE TABLE IF NOT EXISTS {$table} (".implode(',', $fields).") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
@ -48,9 +37,9 @@ class Model
|
||||
return false;
|
||||
}
|
||||
|
||||
public function dropTable()
|
||||
public function dropTable($table)
|
||||
{
|
||||
$sql = "DROP TABLE IF EXISTS pengumuman";
|
||||
$sql = "DROP TABLE IF EXISTS {$table}";
|
||||
|
||||
$db = static::connectDB();
|
||||
$query = $db->prepare($sql);
|
||||
|
31
_tests/unit/PostTest.php
Normal file
31
_tests/unit/PostTest.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
class PostTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function showDataSuccess()
|
||||
{
|
||||
$post = new Post();
|
||||
|
||||
$this->assertTrue($post->showAll());
|
||||
|
||||
$fields = ['category', 'expired_at', 'creator', 'content'];
|
||||
$values = ['3', '2017-09-02', '2', 'barbarbarbarbarbarbar!'];
|
||||
|
||||
$this->assertTrue($post->entry($fields, $values));
|
||||
|
||||
$args = ['category' => 5, 'content' => 'foofoofoofoo!'];
|
||||
|
||||
$this->assertTrue($post->update($args, 1));
|
||||
|
||||
$this->assertTrue($post->showSingle(1));
|
||||
|
||||
$this->assertTrue($post->delete(1));
|
||||
|
||||
$this->assertTrue($post->dropTable('pengumuman'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user