Handle members, clear entity manager on flush

This commit is contained in:
2025-03-13 23:01:59 +01:00
parent 31a091cb52
commit 8452650613
6 changed files with 58 additions and 6 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ class Daemon
$routes = new RouteCollection();
$routes->addController(new Http\Controller\SlotsController($em->getRepository(Slot::class), $this->messageBus));
$routes->addController(new Http\Controller\GroupsController($em->getRepository(Group::class)));
$routes->addController(new Http\Controller\GroupsController($em->getRepository(Group::class), $em->getRepository(Slot::class)));
$this->server = new HttpServer(
new Http\Middleware\LoggingMiddleware($httpLogger ?? $logger),
+7
View File
@@ -32,4 +32,11 @@ class GroupRepository extends EntityRepository
->getQuery()
->getResult();
}
public function flush(...$groups): void
{
foreach ($groups as $group) $this->getEntityManager()->persist($group);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
}
}
+13
View File
@@ -53,11 +53,13 @@ class Slot implements IteratorAggregate, JsonSerializable
public function jsonSerialize(): mixed
{
$groupProps = $this->group ? $this->group->getProperties() : [];
return [
'_id' => $this->id,
'_slot' => $this->slotId,
'_name' => $this->name,
'_group' => $this->group?->getGroupId(),
...$groupProps,
...$this->props,
];
}
@@ -72,4 +74,15 @@ class Slot implements IteratorAggregate, JsonSerializable
$this->props = $properties;
return $this;
}
public function setGroup(?Group $group): self
{
$this->group = $group;
return $this;
}
public function getGroup(): ?Group
{
return $this->group;
}
}
+1
View File
@@ -85,5 +85,6 @@ class SlotRepository extends EntityRepository
{
foreach ($slots as $slot) $this->getEntityManager()->persist($slot);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
}
}
+30 -3
View File
@@ -8,12 +8,14 @@ use React\Http\Message\Response;
use SlotDb\SlotDb\Data\Group\Group;
use SlotDb\SlotDb\Data\Group\GroupRepository;
use SlotDb\SlotDb\Data\Slot\Slot;
use SlotDb\SlotDb\Data\Slot\SlotRepository;
class GroupsController extends Controller
{
public function __construct(
private readonly GroupRepository $groups
private readonly GroupRepository $groups,
private readonly SlotRepository $slots,
)
{
@@ -69,12 +71,37 @@ class GroupsController extends Controller
#[Route(path:"/api/slotdb/v1/group/:group", methods:["POST"])]
public function updateGroup(ServerRequestInterface $request, string $group)
{
$data = $this->groups->findGroup($group);
$groupObj = $this->groups->findGroup($group);
$groupProps = $groupObj->getProperties();
$posted = json_decode($request->getBody());
foreach ((array)$posted as $prop=>$op) {
if (str_starts_with($prop, "_")) {
if ($prop == '_members') {
foreach ((array)$op as $slot=>$state) {
$slotObj = $this->slots->findSlot($slot);
if ($state == true) {
$slotObj->setGroup($groupObj);
} elseif ($slotObj->getGroup() == $groupObj) {
$slotObj->setGroup(null);
}
}
}
} else {
if (is_array($op)) {
} else {
if ($op === null) {
unset($groupProps[$prop]);
} else {
$groupProps[$prop] = $op;
}
}
}
}
$groupObj->setProperties($groupProps);
$this->slots->flush();
return Response::json(true);
}
+6 -2
View File
@@ -94,9 +94,13 @@ class SlotsController extends Controller
$posted = json_decode($request->getBody());
foreach ((array)$posted as $prop=>$op) {
if (is_array($op)) {
} else {
$slotProps[$prop] = $op;
if ($op === null) {
unset($slotProps[$prop]);
} else {
$slotProps[$prop] = $op;
}
}
}