2021-08-21 14:17:14 +00:00
|
|
|
<?php
|
|
|
|
|
2021-08-21 14:25:23 +00:00
|
|
|
namespace OCA\UPschooling\Service;
|
2021-08-21 14:17:14 +00:00
|
|
|
|
|
|
|
use Exception;
|
2021-09-18 17:28:10 +00:00
|
|
|
use OCA\UPschooling\Db\MatrixTicket;
|
|
|
|
use OCA\UPschooling\Db\TicketMapper;
|
2021-08-21 14:17:14 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
|
2021-08-22 15:03:19 +00:00
|
|
|
class TicketService {
|
2021-08-21 14:17:14 +00:00
|
|
|
|
2021-08-22 15:03:19 +00:00
|
|
|
/** @var TicketMapper */
|
2021-08-21 14:17:14 +00:00
|
|
|
private $mapper;
|
|
|
|
|
2021-09-18 17:28:10 +00:00
|
|
|
private $matrix;
|
|
|
|
|
|
|
|
public function __construct(MatrixService $matrix, TicketMapper $mapper) {
|
|
|
|
$this->matrix = $matrix;
|
2021-08-21 14:17:14 +00:00
|
|
|
$this->mapper = $mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findAll(string $userId): array {
|
|
|
|
return $this->mapper->findAll($userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleException(Exception $e): void {
|
|
|
|
if ($e instanceof DoesNotExistException ||
|
|
|
|
$e instanceof MultipleObjectsReturnedException) {
|
|
|
|
throw new NoteNotFound($e->getMessage());
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find($id, $userId) {
|
|
|
|
try {
|
|
|
|
return $this->mapper->find($id, $userId);
|
|
|
|
|
|
|
|
// in order to be able to plug in different storage backends like files
|
|
|
|
// for instance it is a good idea to turn storage related exceptions
|
|
|
|
// into service related exceptions so controllers and service users
|
|
|
|
// have to deal with only one type of exception
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($title, $content, $userId) {
|
2021-09-18 17:28:10 +00:00
|
|
|
$note = new MatrixTicket();
|
2021-08-21 14:17:14 +00:00
|
|
|
$note->setTitle($title);
|
|
|
|
$note->setContent($content);
|
|
|
|
$note->setUserId($userId);
|
|
|
|
return $this->mapper->insert($note);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($id, $title, $content, $userId) {
|
|
|
|
try {
|
|
|
|
$note = $this->mapper->find($id, $userId);
|
|
|
|
$note->setTitle($title);
|
|
|
|
$note->setContent($content);
|
|
|
|
return $this->mapper->update($note);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id, $userId) {
|
|
|
|
try {
|
|
|
|
$note = $this->mapper->find($id, $userId);
|
|
|
|
$this->mapper->delete($note);
|
|
|
|
return $note;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|