...
 
Commits (9)
......@@ -84,6 +84,7 @@ class NodeController {
$nodeViewModel->subgroup = $node->getSubgroup();
$nodeViewModel->name = $node->getName();
$nodeViewModel->target = $node->getTarget();
$nodeViewModel->searchable = $node->isSearchable();
switch ($nodeViewModel->icon) {
case 'poison':
$nodeViewModel->targetIcon = 'fa-user';
......@@ -141,6 +142,46 @@ class NodeController {
* - down-stair
*/
$node = $this->transformPostData($postData, $difficulty, $user, $missionId);
$this->entityManager->persist($node);
$this->entityManager->flush();
$nodeId = $node->getId();
$i = 0;
foreach ($postData['note-type'] as $noteType) {
$noteText = $postData['note-text'][$i++];
if (trim($noteText) === '') {
continue;
}
$nodeNote = new NodeNote();
$nodeNote->setNodeId($nodeId);
$nodeNote->setType($noteType);
$nodeNote->setText($noteText);
$this->entityManager->persist($nodeNote);
$this->entityManager->flush();
}
return $node;
}
private function transformPostData(array $postData, string $difficulty, User $user, int $missionId): Node {
/*
* - Sabotage uses "action" instead of "target"
* - Distraction uses "action" instead of "target"
* - Agency Pickup uses "pickup-type" instead of "target"
* - "large" -> Large Pickup
* - "small" -> Stash
* - Stairwell uses "stairwell-direction" as its icon
* - up-stair
* - up-down-stair
* - down-stair
*/
list($type, $subgroup) = explode('|', $postData['subgroup']);
$node = new Node();
$node->setDifficulty($difficulty);
......@@ -191,10 +232,19 @@ class NodeController {
}
$node->setApproved(UserRole::hasAccess($user->getRolesAsInts(), [UserRole::TRUSTED_EDITOR]));
return $node;
}
public function editNode(int $nodeId, int $missionId, string $difficulty, array $postData, User $user): Node {
$node = $this->transformPostData($postData, $difficulty, $user, $missionId);
$node->setId($nodeId);
$node = $this->entityManager->merge($node);
$this->entityManager->persist($node);
$this->entityManager->flush();
$nodeId = $node->getId();
$this->entityManager->getConnection()->executeUpdate("DELETE FROM `node_notes` WHERE `node_id` = " . $nodeId);
$i = 0;
foreach ($postData['note-type'] as $noteType) {
......
......@@ -21,4 +21,5 @@ class NodeWithNotesViewModel {
public $notes;
public $approved;
public $image;
public $searchable;
}
\ No newline at end of file
......@@ -166,11 +166,11 @@ $klein->respond('GET', '/games/[:game]/[:location]/[:missionSlug]/[:difficulty]'
/* @var $icon \DataAccess\Models\Icon */
foreach ($icons as $icon) {
if (!key_exists($predeterminedItem->getGroup(), $sortedIcons)) {
$sortedIcons[$predeterminedItem->getGroup()] = [];
if (!key_exists($icon->getGroup(), $sortedIcons)) {
$sortedIcons[$icon->getGroup()] = [];
}
$sortedIcons[$predeterminedItem->getGroup()][] = $icon;
$sortedIcons[$icon->getGroup()][] = $icon;
}
$viewModel->predeterminedItems = $sortedPredeterminedItems;
......@@ -192,7 +192,11 @@ $klein->respond('POST', '/api/nodes', function (\Klein\Request $request, \Klein\
$user = \BusinessLogic\Session\Session::read('userContext');
$node = $applicationContext->get(\Controllers\NodeController::class)->createNode(intval($_POST['mission-id']), $_POST['difficulty'], $_POST, $user);
$response->code(201);
return json_encode(transformNode($node, $applicationContext));
});
function transformNode(\DataAccess\Models\Node $node, \DI\Container $applicationContext): \Controllers\ViewModels\NodeWithNotesViewModel {
$nodeViewModel = new \Controllers\ViewModels\NodeWithNotesViewModel();
$nodeViewModel->id = $node->getId();
$nodeViewModel->missionId = $node->getMissionId();
......@@ -208,6 +212,7 @@ $klein->respond('POST', '/api/nodes', function (\Klein\Request $request, \Klein\
$nodeViewModel->group = $node->getGroup();
$nodeViewModel->approved = $node->getApproved();
$nodeViewModel->image = $node->getImage();
$nodeViewModel->searchable = $node->isSearchable();
switch ($nodeViewModel->icon) {
case 'poison':
$nodeViewModel->targetIcon = 'fa-user';
......@@ -234,9 +239,9 @@ $klein->respond('POST', '/api/nodes', function (\Klein\Request $request, \Klein\
$nodeViewModel->notes[] = $innerViewModel;
}
$response->code(201);
return json_encode($nodeViewModel);
});
return $nodeViewModel;
}
$klein->respond('POST', '/api/ledges', function (\Klein\Request $request, \Klein\Response $response) use ($applicationContext) {
if (!userIsLoggedIn()) {
......@@ -286,7 +291,27 @@ $klein->respond('POST', '/api/nodes/move', function (\Klein\Request $request, \K
return json_encode(['message' => 'OK']);
});
$klein->respond('GET', '/api/nodes/delete/[:nodeId]', function(\Klein\Request $request, \Klein\Response $response) use ($applicationContext) {
$klein->respond('POST', '/api/nodes/edit/[:nodeId]', function(\Klein\Request $request, \Klein\Response $response) use ($applicationContext) {
if (!userIsLoggedIn()) {
print json_encode(['message' => 'You must be logged in to modify nodes!']);
return $response->code(401);
}
/* @var $user \DataAccess\Models\User */
$user = \BusinessLogic\Session\Session::read('userContext');
$roles = $user->getRolesAsInts();
if (!\BusinessLogic\UserRole::hasAccess($roles, [\BusinessLogic\UserRole::TRUSTED_EDITOR])) {
print json_encode(['message' => 'You do not have permission to delete nodes!']);
return $response->code(403);
}
$node = $applicationContext->get(\Controllers\NodeController::class)->editNode(intval($request->nodeId), intval($_POST['mission-id']), $_POST['difficulty'], $_POST, $user);
$response->code(200);
return json_encode(transformNode($node, $applicationContext));
});
$klein->respond('GET', '/api/nodes/delete/[:nodeId]', function(\Klein\Request $request, \Klein\Response $response) use ($applicationContext) {
if (!userIsLoggedIn()) {
print json_encode(['message' => 'You must be logged in to modify nodes!']);
return $response->code(401);
......
This diff is collapsed.