Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
Router::post('/updateInfo/', [new \Up\Controllers\UserController(), 'updateInfoAction']);

//fetch-api
Router::post('/remove/',[new \Up\Controllers\AdminController(), 'removeAction']);
Router::post('/remove/', [new \Up\Controllers\AdminController(), 'removeProductAction']);
Router::post('/removeUser/', [new \Up\Controllers\AdminController(), 'removeUserAction']);
Router::post('/migrations/execute/', [new \Up\Controllers\AdminController(), 'executeAction']);
Router::post('/database/delete/', [new \Up\Controllers\AdminController(), 'dbAction']);
Router::post('/update/product/', [new \Up\Controllers\AdminController(), 'updateProductAction']);
Expand Down
38 changes: 38 additions & 0 deletions public/assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,44 @@ async function removeItem(id, title)
}
}

async function removeUser(id, fullName)
{
const shouldRemove = confirm(`Are you sure you want to delete this product: ${fullName}`);
if (!shouldRemove)
{
return;
}
const removeParams = {
id: id,
};

try {
const response = await fetch('/removeUser/',
{
method: 'POST',
headers:{
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(removeParams)
}
);
const responseJson = await response.json();
if (responseJson.result !== 'Y')
{
console.log('error while deleting user :(');
}
const userItem = document.querySelector(`[data-id="${id}"]`);
if (userItem)
{
userItem.remove();
}
}
catch (error)
{
console.log('error while deleting user:' + error);
}
}

document.addEventListener('DOMContentLoaded', function() {
const buttons = document.querySelectorAll('.account__sideBarBtn');
const containers = document.querySelectorAll('.account__main');
Expand Down
30 changes: 29 additions & 1 deletion src/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function loginAction(): string
/**
* @throws Exception
*/
public function removeAction(): void
public function removeProductAction(): void
{
header('Content-Type: application/json');
$input = file_get_contents('php://input');
Expand All @@ -96,6 +96,34 @@ public function removeAction(): void
}
}

/**
* @throws Exception
*/
public function removeUserAction(): void
{
header('Content-Type: application/json');
$input = file_get_contents('php://input');
$data = Json::decode($input);

if (isset($data['id']))
{
$id = $data['id'];
$result = UserService::deleteUserByID($id);

echo Json::encode([
'result' => $result > 0 ? 'Y' : 'N',
]);
}
else
{
echo Json::encode([
'result' => 'N',
'error' => 'Id not provided',
]);
}
}


public function dbAction(): void
{
header('Content-Type: application/json');
Expand Down
13 changes: 13 additions & 0 deletions src/Services/Repository/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Up\Services\Repository;

use Core\DB\DbConnection;
use Core\Http\Request;
use Exception;
use RuntimeException;
use Up\Models\User;
use Up\Services\SecurityService;

Expand Down Expand Up @@ -221,4 +223,15 @@ public static function updateUserPassword(): bool

return SecurityService::safeUpdateQuery($table, $data, $condition, $params);
}

/**
* @throws Exception
*/
public static function deleteUserByID(int $id): void
{
if (!SecurityService::safeDeleteQuery('`USER`','`USER`.`ID` = ?', [$id]))
{
throw new RuntimeException('Error delete user: ' . DbConnection::get()->error);
}
}
}
4 changes: 2 additions & 2 deletions src/Views/default/components/admin-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ul class="admin__usersInfoList">
<li class="account__userInfoItem">
<h4 class="account__ordersInfoTitle">User code</h4>
<p class="account__ordersInfoSubtitle">#<?=$user->id?></p>
<p class="account__ordersInfoSubtitle" data-id="<?=$user->id?>">#<?=$user->id?></p>
</li>
<li class="account__userInfoItem">
<h4 class="account__ordersInfoTitle">User Name</h4>
Expand All @@ -27,7 +27,7 @@
<p class="account__ordersInfoSubtitle"><?=$user->address?></p>
</li>
<li class="account__userInfoItem admin__userDelete">
<button id="dangerBtn">
<button id="dangerBtn" onclick="removeUser(<?=$user->id?>, '<?=$user->name . ' ' . $user->surname?>')">
<img src="/assets/images/common/bin.svg" alt="delete user" class="deleteImg">
</button>
</li>
Expand Down