VENOM: Entity fixed JSON

EntityManger added return value
EntityManager added getFirst
Fixed Some Admin shit :)
This commit is contained in:
engineerTrooper 2021-01-04 22:24:32 +01:00
parent eb6770204a
commit 6523e77742
7 changed files with 61 additions and 116 deletions

View File

@ -20,6 +20,15 @@ abstract class Entity implements JsonSerializable
// Override this if you want special fields :)
public function getLoadedFieldValues(): array
{
$keyValue = [];
foreach ($this->loadedFields as $var) {
$keyValue[$var] = $this->$var;
}
return $keyValue;
}
public function getFieldsToWrite(): array
{
if ($this->fields !== null) {
@ -33,9 +42,10 @@ abstract class Entity implements JsonSerializable
unset($vars[$key]);
}
}
if (!$allLoaded) {
foreach ($vars as $key => $var) {
if (!in_array($var, $this->loadedFields)) {
if (!in_array($key, $this->loadedFields)) {
unset($vars[$key]);
}
}
@ -104,7 +114,7 @@ abstract class Entity implements JsonSerializable
public function jsonSerialize(): array
{
return $this->getFieldsToWrite();
return $this->getLoadedFieldValues();
}
public function preSave()

View File

@ -50,10 +50,10 @@ class EntityManager
return null;
}
public function saveAll()
public function saveAll(): bool
{
if (count($this->entities) === 0) {
return;
return true;
}
try {
$this->db->start();
@ -64,10 +64,12 @@ class EntityManager
} catch (Exception $ex) {
trigger_error($ex->getMessage());
$this->db->rollBack();
return false;
}
return true;
}
public function deleteEntities()
public function deleteEntities(): bool
{
try {
$this->db->start();
@ -78,7 +80,9 @@ class EntityManager
} catch (Exception $ex) {
trigger_error($ex->getMessage());
$this->db->rollBack();
return false;
}
return true;
}
public function clearAll()
@ -118,6 +122,11 @@ class EntityManager
return $this->entities;
}
public function getFirst(): ?Entity
{
return $this->entities[0] ?? null;
}
private function addEntities(array $entities)
{
foreach ($entities as $entity) {

View File

@ -22,32 +22,27 @@ class UserAPIController
public function getById($id)
{
$d = UserController::getById($id, ["id", "username", "firstname", "lastname", "email", "isActive"]);
AdminHelper::sendResponse($d);
$entityManager = EntityManager::create(User::class);
$easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]);
$easyQuery->where("id", $id);
$entityManager->load($easyQuery);
AdminHelper::sendResponse($entityManager->getFirst());
}
public function update($id)
{
$original = UserController::getById($id);
$entityManager = EntityManager::create(User::class);
$easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]);
$easyQuery->where("id", $id);
$entityManager->load($easyQuery);
/** @var User|null $original */
$original = $entityManager->getFirst();
if ($original == null) {
AdminHelper::sendStatus(false, "User not Found");
}
$args = ArgumentHandler::get();
$data = [];
$d = $original->getData();
foreach ($d as $key => $item) {
if ($args->hasPostItem($key)) {
$val = $args->getPostItem($key);
if ($val != $item) {
$data[$key] = $val;
}
}
}
parse_str(file_get_contents('php://input'), $_PUT);
var_dump(array_keys($_PUT));
//var_dump($data, $d, $_POST);
// $args->getPostItem("username")//UPDATE users SET lastname='Doe', firstname='' WHERE id=2
AdminHelper::sendStatus(UserController::update($id, $data));
/* var_dump(array_keys($_PUT));*/
AdminHelper::sendStatus($entityManager->saveAll());
}
public function delete($id)

View File

@ -1,36 +0,0 @@
<?php
namespace Modules\User\Controller;
use Venom\Core\DatabaseHandler;
use Venom\Entities\DatabaseObject;
class UserController
{
public static function getById($id, array $fields = ["*"]): ?DatabaseObject
{
if (empty($id)) {
return null;
}
$sel = DatabaseHandler::createEasySelect($fields, "users") . " WHERE id = :id";
return DatabaseHandler::get()->getOne($sel, [
':id' => $id
]);
}
public static function get(array $fields = ["*"]): array
{
return DatabaseHandler::get()->getAll(DatabaseHandler::createEasySelect($fields, "users"));
}
public static function update($id, array $values = []): bool
{
if (count($values) === 0) {
return false;
}
return DatabaseHandler::get()->execute(...DatabaseHandler::getUpdateString($values, "users", "WHERE id = :id"));
}
}

View File

@ -23,7 +23,7 @@ $venom->registerModule([
],
"1" => [
Route::GET => 'getById',
Route::POST => 'insert',
Route::POST => 'update',
Route::PUT => 'update',
Route::DELETE => 'delete'
]

View File

@ -1,65 +1,34 @@
<div class="users-edit">
<header>
<h2>User: engineertrooper</h2>
<h2>User: ${username}</h2>
</header>
<div>
<span data-link="/users" class="icon-text">
{include(includes/svg;class=back-arrow;icon=vt-arrow-back)}
</span>
</div>
<h3>User Data</h3>
<div class="spacer">
{include(includes/input;class=input-group;label=Username;name=newUserName;error=New User Name is required;default=EngineerTrooper)}
{include(includes/input;class=input-group;label=Author Name;name=newAuthorName;error=New Author Name is required;default=Dominic Seela;classes=spacer)}
{include(includes/input;class=input-group;label=E-Mail;name=newEMailAddress;error=E-Mail Address is required;default=kontakt@engineertrooper.com;classes=spacer)}
{include(includes/input;class=input-group;label=Password;name=newPassword;type=password;error=Password is required;classes=spacer)}
{include(includes/input;class=input-group;label=Password (Repeat);name=newPasswordRepeat;type=password;error=Password (Repeat) is required;classes=spacer)}
</div>
<v-table class="privileges">
<h3>Privileges</h3>
<v-table-body>
<v-table-header>
<v-cell class="name">Module</v-cell>
<v-cell class="name">Edit</v-cell>
<v-cell class="name">View</v-cell>
</v-table-header>
<v-table-row>
<v-cell class="description">Meta-Data</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditMetaData)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewMetaData)}</v-cell>
</v-table-row>
<v-table-row>
<v-cell class="description">Pages</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditPages)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewPages)}</v-cell>
</v-table-row>
<v-table-row>
<v-cell class="description">Roles</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditRoles)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewRoles)}</v-cell>
</v-table-row>
<v-table-row>
<v-cell class="description">SEO-URL</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditSeoUrl)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewSeoUrl)}</v-cell>
</v-table-row>
<v-table-row>
<v-cell class="description">Users</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditUsers)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewUsers)}</v-cell>
</v-table-row>
<v-table-row>
<v-cell class="description">VENOM-Status</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionEditVenomStatus)}</v-cell>
<v-cell>{include(includes/switch;id=${switch.id};name=permissionViewVenomStatus)}</v-cell>
</v-table-row>
</v-table-body>
</v-table>
<div class="btn-line">
<div>
<h3>User-Data</h3>
<form id="user-form" data-id="${id}">
<div class="spacer">
{include(includes/input;class=input-group;label=Username;name=username;error=Username is required;default=$username)}
{include(includes/input;class=input-group;label=Firstname;name=firstname;error=Firstname is required;default=$firstname;classes=spacer)}
{include(includes/input;class=input-group;label=Lastname;name=lastname;error=Lastname is required;default=$lastname;classes=spacer)}
{include(includes/input;class=input-group;label=E-Mail;name=newEMailAddress;error=E-Mail Address is required;default=$email;classes=spacer)}
</div>
<div class="btn-line">
{include(includes/btn;type=valid;content=Save)}
{include(includes/btn;type=primary;content=Reset)}
{include(includes/btn;type=warn;content=Delete User)}
</div>
</div>
</form>
<h3>Change Password</h3>
<form id="password-form" data-id="${id}">
{include(includes/input;class=input-group;label=Password;name=password;type=password;classes=spacer)}
{include(includes/input;class=input-group;label=Password (Repeat);name=passwordRepeat;type=password;classes=spacer)}
<div class="btn-line">
{include(includes/btn;type=valid;content=Save)}
</div>
</form>
</div>

View File

@ -17,9 +17,7 @@
{/for}
</div>
<div class="add-new">
<h3>Add new User</h3>
{include(includes/input;label=New User Name;name=newUserName;error=New User Name is required;type=text)}
{include(includes/btn;type=primary;content=Add)}
{include(includes/btn;type=primary;content=Add User)}
</div>
</div>
</div>