Fix finding ticket by id with user

Add findTicket by id only.
This commit is contained in:
Ben 2021-10-10 19:01:00 +02:00
parent 708136dcf7
commit 61ba02ccd9
Signed by: ben
GPG Key ID: 0F54A7ED232D3319
6 changed files with 66 additions and 19 deletions

1
composer.lock generated
View File

@ -27,7 +27,6 @@
"phpunit/php-code-coverage": "^7.0",
"phpunit/phpunit": "^8.5"
},
"default-branch": true,
"type": "library",
"extra": {
"branch-alias": {

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace OCA\UPschooling\Exceptions;
use OC\OCS\Exception;
use OC\OCS\Result;
class TicketNotFoundException extends Exception
{
public function __construct($ticketId)
{
parent::__construct(new Result(null, 404, 'Ticket with id ' . $ticketId . ' not found'));
}
}

View File

@ -11,6 +11,7 @@ use OCA\UPschooling\Db\MatrixUser;
use OCA\UPschooling\Db\TicketMapper;
use OCA\UPschooling\Db\UserMapper;
use OCA\UPschooling\Exceptions\RoomNotFoundException;
use OCA\UPschooling\Exceptions\TicketNotFoundException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
@ -32,12 +33,24 @@ class TicketService {
}
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 {

View File

@ -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(