diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..708e5da --- /dev/null +++ b/.editorconfig @@ -0,0 +1,23 @@ +# Copyright 2017 Aviral Dasgupta +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +root = true + +[*] +charset=utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = tab +indent_size = 2 +trim_trailing_whitespace = true 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/Controller/TicketApiController.php b/lib/Controller/TicketApiController.php index bdaefb3..0f58359 100644 --- a/lib/Controller/TicketApiController.php +++ b/lib/Controller/TicketApiController.php @@ -54,11 +54,9 @@ class TicketApiController extends ApiController /** * @NoAdminRequired */ - public function update(int $id, string $title, string $content): DataResponse + public function update(int $id): DataResponse { - return $this->handleNotFound(function () use ($id, $title, $content) { - return $this->service->update($id, $title, $content, $this->userId); - }); + return new DataResponse($this->service->assign($id, $this->userId)); } public function destroy(int $id): DataResponse 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 @@ +userManager = $userManager; $this->matrix = $matrix; $this->ticketMapper = $ticketMapper; $this->userMapper = $userMapper; } public function findAll(string $userId): array { - $dbTickets = $this->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 { @@ -59,6 +82,19 @@ class TicketService { return $this->resolveTicket($this->ticketMapper->insert($ticket)); } + public function assign($id, $userId): array{ + $matrixUser = $this->getOrCreateUser($userId); + $ticket = $this->ticketMapper->findTicket($id); + $roomID = $ticket->getMatrixRoom(); + $helperName = $this->userManager->get($userId)->getDisplayName(); + $this->matrix->setProperty($roomID, "upschooling.ticket", array( + "matrixHelperUser" => $matrixUser->getMatrixUser(), + "matrixHelperName" => $helperName, + )); + $ticket->setMatrixHelperUser($matrixUser->getMatrixUser()); + return $this->resolveTicket($this->ticketMapper->update($ticket)); + } + public function update($id, $title, $content, $userId) { throw new Exception("Not implemented"); } @@ -68,6 +104,7 @@ class TicketService { } /** + * matrixTicketContent has all the syncronized data * @param MatrixTicket $ticket the database object. * @return array a JSON serializable representation of the resolved ticket, for the frontend. */ @@ -79,12 +116,17 @@ class TicketService { $title = array_get($matrixTicketContent, "title", "Untitled"); $description = array_get($matrixTicketContent, "description", ""); $lastModified = $this->matrix->getLastEventDate($ticket->getMatrixRoom()); + $matrixHelperUser = array_get($matrixTicketContent, "matrixHelperUser", "nicht zugewiesen"); + $matrixHelperName = array_get($matrixTicketContent, "matrixHelperName", $matrixHelperUser); + $expirationDate = array_get($matrixTicketContent, "expirationDate", ""); return array( 'ticketId' => $ticketId, 'status' => $ticket->getStatus(), 'lastModified' => $lastModified, 'title' => $title, 'description' => $description, + 'matrixHelperName' => $matrixHelperName, + 'expirationDate' => $expirationDate ); } catch (MatrixException | RoomNotFoundException $e) { return array( diff --git a/src/App.vue b/src/App.vue index efff633..d93efd1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -61,14 +61,16 @@ 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' } }, + ).catch(console.error) + axios.put( + 'api/v1/tickets/1', + {}, + { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } } ).then((response) => { - if (this.ticketsFetched === false) { - this.tickets.push(response.data) - } + console.error('added a helper to ticket 2') }).catch(console.error) }, fetchTickets() { diff --git a/tests/Unit/Controller/TicketApiControllerTest.php b/tests/Unit/Controller/TicketApiControllerTest.php index d9e5a76..ad7beb6 100644 --- a/tests/Unit/Controller/TicketApiControllerTest.php +++ b/tests/Unit/Controller/TicketApiControllerTest.php @@ -35,7 +35,7 @@ class TicketApiControllerTest extends TestCase $this->equalTo($this->userId)) ->will($this->returnValue($note)); - $result = $this->controller->update(3, 'title', 'content'); + $result = $this->controller->update(3); $this->assertEquals($note, $result->getData()); } @@ -48,7 +48,7 @@ class TicketApiControllerTest extends TestCase ->method('update') ->will($this->throwException(new NoteNotFound())); - $result = $this->controller->update(3, 'title', 'content'); + $result = $this->controller->update(3); $this->assertEquals(Http::STATUS_NOT_FOUND, $result->getStatus()); }*/