diff --git a/src/API/System/Queue.php b/src/API/System/Queue.php new file mode 100644 index 00000000..abad7026 --- /dev/null +++ b/src/API/System/Queue.php @@ -0,0 +1,95 @@ + [], + 'links' => [ + 'self' => (string)$request->getUri()->withHost('')->withPort(0)->withScheme(''), + ], + ]; + + $entities = $items = []; + + $queue = $this->cache->get('queue', []); + + foreach ($queue as $item) { + $items[] = Container::get(iState::class)::fromArray($item); + } + + if (!empty($items)) { + foreach ($this->db->find(...$items) as $item) { + $entities[$item->id] = $item; + } + } + + $items = null; + + foreach ($entities as $entity) { + $response['queue'][] = [ + 'id' => $entity->id, + 'title' => $entity->getName(), + 'played' => $entity->isWatched() ? 'Yes' : 'No', + 'via' => $entity->via ?? '??', + 'date' => makeDate($entity->updated)->format('Y-m-d H:i:s T'), + 'event' => ag($entity->getExtra($entity->via), iState::COLUMN_EXTRA_EVENT), + ]; + } + + return api_response(HTTP_STATUS::HTTP_OK, $response); + } + + /** + * @throws InvalidArgumentException + */ + #[Delete(self::URL . '/{id}[/]', name: 'system.queue.delete')] + public function deleteQueue(iRequest $request, array $args = []): iResponse + { + if (null === ($id = ag($args, 'id'))) { + return api_error('Invalid id.', HTTP_STATUS::HTTP_BAD_REQUEST); + } + + $queue = $this->cache->get('queue', []); + + if (empty($queue)) { + return api_error('Queue is empty.', HTTP_STATUS::HTTP_NOT_FOUND); + } + + if (false === array_key_exists($id, $queue)) { + return api_error(r("Record id '{id}' doesn't exists.", ['id' => $id]), HTTP_STATUS::HTTP_NOT_FOUND); + } + + $item = Container::get(iState::class)::fromArray(['id' => $id]); + + queuePush($item, remove: true); + + return api_response(HTTP_STATUS::HTTP_OK, ['item' => $item->getAll()]); + } +}