From 61ba02ccd95d09b640d0d78b9edf82c980cee7ae Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sun, 10 Oct 2021 19:01:00 +0200 Subject: [PATCH] Fix finding ticket by id with user Add findTicket by id only. --- composer.lock | 1 - lib/Controller/Errors.php | 4 +-- lib/Db/TicketMapper.php | 38 ++++++++++++++++++---- lib/Exceptions/TicketNotFoundException.php | 14 ++++++++ lib/Service/TicketService.php | 17 ++++++++-- src/App.vue | 11 ++----- 6 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 lib/Exceptions/TicketNotFoundException.php diff --git a/composer.lock b/composer.lock index 1fdc435..880f52b 100644 --- a/composer.lock +++ b/composer.lock @@ -27,7 +27,6 @@ "phpunit/php-code-coverage": "^7.0", "phpunit/phpunit": "^8.5" }, - "default-branch": true, "type": "library", "extra": { "branch-alias": { diff --git a/lib/Controller/Errors.php b/lib/Controller/Errors.php index d118e8c..7fcb2d6 100644 --- a/lib/Controller/Errors.php +++ b/lib/Controller/Errors.php @@ -3,7 +3,7 @@ namespace OCA\UPschooling\Controller; use Closure; -use OCA\UPschooling\Service\NoteNotFound; +use OCA\UPschooling\Exceptions\TicketNotFoundException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -11,7 +11,7 @@ trait Errors { protected function handleNotFound(Closure $callback): DataResponse { try { return new DataResponse($callback()); - } catch (NoteNotFound $e) { + } catch (TicketNotFoundException $e) { $message = ['message' => $e->getMessage()]; return new DataResponse($message, Http::STATUS_NOT_FOUND); } diff --git a/lib/Db/TicketMapper.php b/lib/Db/TicketMapper.php index 98a0a06..b7c1fc4 100644 --- a/lib/Db/TicketMapper.php +++ b/lib/Db/TicketMapper.php @@ -17,19 +17,40 @@ class TicketMapper extends QBMapper /** * @param int $id - * @param string $userId * @return Entity|MatrixTicket * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException * @throws DoesNotExistException */ - public function find(int $id, MatrixUser $matrixUser): MatrixTicket + public function findTicket(int $id): MatrixTicket { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from('upschooling_tickets') - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->in($matrixUser->getMatrixUser(), ['matrix_assisted_user', 'matrix_helper_user'])); + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); + return $this->findEntity($qb); + } + + /** + * @param int $id + * @param string $userId + * @return Entity|MatrixTicket + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException + * @throws DoesNotExistException + */ + public function findForUser(int $id, MatrixUser $matrixUser): MatrixTicket + { + /* @var $qb IQueryBuilder */ + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from('upschooling_tickets') + ->where( + $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)), + $qb->expr()->orX( + $qb->expr()->eq('matrix_assisted_user', $qb->createNamedParameter($matrixUser->getMatrixUser(), IQueryBuilder::PARAM_STR)), + $qb->expr()->eq('matrix_helper_user', $qb->createNamedParameter($matrixUser->getMatrixUser(), IQueryBuilder::PARAM_STR)) + ) + ); return $this->findEntity($qb); } @@ -37,13 +58,18 @@ class TicketMapper extends QBMapper * @param string $userId * @return array */ - public function findAll(MatrixUser $matrixUser): array + public function findAllForUser(MatrixUser $matrixUser): array { /* @var $qb IQueryBuilder */ $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from('upschooling_tickets') - ->where($qb->expr()->in($matrixUser->getMatrixUser(), ['matrix_assisted_user', 'matrix_helper_user'])); + ->where( + $qb->expr()->orX( + $qb->expr()->eq('matrix_assisted_user', $qb->createNamedParameter($matrixUser->getMatrixUser(), IQueryBuilder::PARAM_STR)), + $qb->expr()->eq('matrix_helper_user', $qb->createNamedParameter($matrixUser->getMatrixUser(), IQueryBuilder::PARAM_STR)) + ) + ); return $this->findEntities($qb); } } diff --git a/lib/Exceptions/TicketNotFoundException.php b/lib/Exceptions/TicketNotFoundException.php new file mode 100644 index 0000000..22bfe1d --- /dev/null +++ b/lib/Exceptions/TicketNotFoundException.php @@ -0,0 +1,14 @@ +ticketMapper->findAll($this->getOrCreateUser($userId)); + $dbTickets = $this->ticketMapper->findAllForUser($this->getOrCreateUser($userId)); return array_map(function ($ticket) { return $this->resolveTicket($ticket); }, $dbTickets); } + /** + * @throws MatrixRequestException + * @throws MatrixHttpLibException + * @throws MultipleObjectsReturnedException + * @throws \OCP\DB\Exception + * @throws TicketNotFoundException + * @throws MatrixException + */ public function find($id, $userId): array { - return $this->resolveTicket($this->ticketMapper->find($id, $this->getOrCreateUser($userId))); + try { + return $this->resolveTicket($this->ticketMapper->findForUser($id, $this->getOrCreateUser($userId))); + } catch (DoesNotExistException $e) { + throw new TicketNotFoundException($id); + } } public function create($title, $content, $userId): array { diff --git a/src/App.vue b/src/App.vue index efff633..8d83029 100644 --- a/src/App.vue +++ b/src/App.vue @@ -61,15 +61,10 @@ export default { this.tickets.push(response.data) } }).catch(console.error) - axios.post( - 'api/v1/tickets', - { title: 'Zweites Ticket', content: 'Zweites Beispiel' }, + axios.get( + 'api/v1/tickets/1', { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } }, - ).then((response) => { - if (this.ticketsFetched === false) { - this.tickets.push(response.data) - } - }).catch(console.error) + ).catch(console.error) }, fetchTickets() { axios.get(