Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b45b05d

Browse files
Finished CRUD tests
1 parent d336315 commit b45b05d

File tree

11 files changed

+84
-16
lines changed

11 files changed

+84
-16
lines changed

‎config/constants.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
define('SITENAME', 'PHP Basic MVC');
88

99
// CRUD Operations' Alert Messages
10-
define('TASK_CREATED', 'Task added successfully');
1110
define('TASK_NOT_CREATED', 'Something went wrong creating new task');
12-
define('TASK_DELETED', 'Task deleted successfully');
11+
define('TASK_NOT_UPDATED', 'Task updated successfully');
1312
define('TASK_NOT_DELETED', 'Something went wrong deleting task');

‎public/css/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
html {
22
font-family: sans-serif;
3+
}
4+
5+
form {
6+
display: inline-block;
37
}

‎public/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
require_once dirname(__DIR__) . '/vendor/autoload.php';
33

4-
App\Router::load(APPROOT . '/src/app/routes.php')->redirectTo();
4+
App\Router::load(APPROOT . '/src/app/routes.php')->redirect();

‎src/app/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setParams(string $uri) : void
9191
* ... and this can be used or not if the method doesn't receive any arguments
9292
* @return void
9393
*/
94-
public function redirectTo() : void
94+
public function redirect() : void
9595
{
9696
$controller = $this->getNamespace() . $this->params['controller'];
9797
$action = $this->capitalizeAction($this->params['action']);

‎src/controllers/Folder1/Class1.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/**
55
* ModuleClass file
66
* For example for large projects with many folders (modules)
7+
*
8+
* You can access this file through http://localhost/php-basic-mvc/folder1/class1/index
79
*/
810
class Class1
911
{

‎src/controllers/Folder1/Class2.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/**
55
* ModuleClass file
66
* For example for large projects with many folders (modules)
7+
*
8+
* You can access this file through http://localhost/php-basic-mvc/folder1/class2/123/index
79
*/
810
class Class2
911
{

‎src/controllers/Folder2/Class1.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/**
55
* ModuleClass file
66
* For example for large projects with many folders (modules)
7+
*
8+
* You can access this file through http://localhost/php-basic-mvc/folder2/class1/index
79
*/
810
class Class1
911
{

‎src/controllers/Folder2/Class2.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/**
55
* ModuleClass file
66
* For example for large projects with many folders (modules)
7+
*
8+
* You can access this file through http://localhost/php-basic-mvc/folder2/class2/123/index
79
*/
810
class Class2
911
{

‎src/controllers/Test.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,42 @@ public function __construct()
1010
$this->taskModel = new Task;
1111
}
1212

13+
// Render View
1314
public function tasks()
1415
{
15-
$tasks = $this->taskModel->selectAll();
16-
17-
view('TestDb/tasks', $tasks);
16+
view('TestDb/tasks', $this->getTasks());
1817
}
1918

19+
// CREATE new task
2020
public function addTask()
2121
{
22-
echo __METHOD__;
22+
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->taskModel->createTask($_POST['new_task']))
23+
header('location: ' . URLROOT . '/test/tasks', true, 303);
24+
else
25+
die(TASK_NOT_CREATED);
26+
}
27+
28+
// READ all tasks
29+
private function getTasks() : array
30+
{
31+
return $this->taskModel->selectAll();
32+
}
33+
34+
// UPDATE task
35+
public function markDone($params)
36+
{
37+
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->taskModel->changeTaskStatus($params['id']))
38+
header('location: ' . URLROOT . '/test/tasks', true, 303);
39+
else
40+
die(TASK_NOT_UPDATED);
2341
}
2442

25-
public function markRead($data)
43+
// DELETE task
44+
public function delete($params)
2645
{
27-
if ($this->taskModel->changeTaskStatus($data['id']))
28-
{
29-
$this->tasks();
30-
}
46+
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $this->taskModel->deleteTask($params['id']))
47+
header('location: ' . URLROOT . '/test/tasks', true, 303);
48+
else
49+
die(TASK_NOT_DELETED);
3150
}
3251
}

‎src/models/Task.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,33 @@ public function __construct()
1212
$this->db = new Database;
1313
}
1414

15+
/**
16+
* CREATE
17+
* @return boolean
18+
*/
19+
public function createTask($description) : bool
20+
{
21+
$this->db->query("INSERT INTO task (`description`,`completed`) VALUES (:task, 0)");
22+
$this->db->bind(':task', $description);
23+
if ($this->db->execute())
24+
return true;
25+
return false;
26+
}
27+
28+
/**
29+
* READ
30+
* @return array
31+
*/
1532
public function selectAll() : array
1633
{
1734
$this->db->query("SELECT * FROM task");
1835
return $this->db->resultSet();
1936
}
2037

38+
/**
39+
* UPDATE
40+
* @return boolean
41+
*/
2142
public function changeTaskStatus($id) : bool
2243
{
2344
$this->db->query("UPDATE task SET completed = 1 WHERE id = :id");
@@ -26,5 +47,17 @@ public function changeTaskStatus($id) : bool
2647
return true;
2748
return false;
2849
}
29-
50+
51+
/**
52+
* DELETE
53+
* @return boolean
54+
*/
55+
public function deleteTask($id) : bool
56+
{
57+
$this->db->query("DELETE FROM task WHERE id = :id");
58+
$this->db->bind(':id', $id);
59+
if ($this->db->execute())
60+
return true;
61+
return false;
62+
}
3063
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /