From 813849e5216cb87121e0f778a734575be6a36052 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 7 May 2017 16:50:20 +0200 Subject: Add history entries for API endpoint CHANGED: datetime is now store as an object in history store file --- application/api/ApiMiddleware.php | 5 +++-- application/api/controllers/ApiController.php | 9 ++++++++- application/api/controllers/History.php | 2 +- application/api/controllers/Links.php | 5 ++++- 4 files changed, 16 insertions(+), 5 deletions(-) (limited to 'application/api') diff --git a/application/api/ApiMiddleware.php b/application/api/ApiMiddleware.php index 4120f7a9..ff209393 100644 --- a/application/api/ApiMiddleware.php +++ b/application/api/ApiMiddleware.php @@ -4,6 +4,7 @@ namespace Shaarli\Api; use Shaarli\Api\Exceptions\ApiException; use Shaarli\Api\Exceptions\ApiAuthorizationException; +use Shaarli\Config\ConfigManager; use Slim\Container; use Slim\Http\Request; use Slim\Http\Response; @@ -31,7 +32,7 @@ class ApiMiddleware protected $container; /** - * @var \ConfigManager instance. + * @var ConfigManager instance. */ protected $conf; @@ -121,7 +122,7 @@ class ApiMiddleware * * FIXME! LinkDB could use a refactoring to avoid this trick. * - * @param \ConfigManager $conf instance. + * @param ConfigManager $conf instance. */ protected function setLinkDb($conf) { diff --git a/application/api/controllers/ApiController.php b/application/api/controllers/ApiController.php index f35b923a..3be85b98 100644 --- a/application/api/controllers/ApiController.php +++ b/application/api/controllers/ApiController.php @@ -2,6 +2,7 @@ namespace Shaarli\Api\Controllers; +use Shaarli\Config\ConfigManager; use \Slim\Container; /** @@ -19,7 +20,7 @@ abstract class ApiController protected $ci; /** - * @var \ConfigManager + * @var ConfigManager */ protected $conf; @@ -28,6 +29,11 @@ abstract class ApiController */ protected $linkDb; + /** + * @var \History + */ + protected $history; + /** * @var int|null JSON style option. */ @@ -45,6 +51,7 @@ abstract class ApiController $this->ci = $ci; $this->conf = $ci->get('conf'); $this->linkDb = $ci->get('db'); + $this->history = $ci->get('history'); if ($this->conf->get('dev.debug', false)) { $this->jsonStyle = JSON_PRETTY_PRINT; } else { diff --git a/application/api/controllers/History.php b/application/api/controllers/History.php index c4ff3e5d..da034eb2 100644 --- a/application/api/controllers/History.php +++ b/application/api/controllers/History.php @@ -28,7 +28,7 @@ class History extends ApiController */ public function getHistory($request, $response) { - $history = (new \History($this->conf->get('resource.history')))->getHistory(); + $history = $this->history->getHistory(); $history = array_reverse($history); // Return history operations from the {offset}th, starting from {since}. diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php index a40e974d..eb78dd26 100644 --- a/application/api/controllers/Links.php +++ b/application/api/controllers/Links.php @@ -141,6 +141,7 @@ class Links extends ApiController $this->linkDb[$link['id']] = $link; $this->linkDb->save($this->conf->get('resource.page_cache')); + $this->history->addLink($link); $out = ApiUtils::formatLink($link, index_url($this->ci['environment'])); $redirect = $this->ci->router->relativePathFor('getLink', ['id' => $link['id']]); return $response->withAddedHeader('Location', $redirect) @@ -184,6 +185,7 @@ class Links extends ApiController $responseLink = ApiUtils::updateLink($responseLink, $requestLink); $this->linkDb[$responseLink['id']] = $responseLink; $this->linkDb->save($this->conf->get('resource.page_cache')); + $this->history->updateLink($responseLink); $out = ApiUtils::formatLink($responseLink, $index); return $response->withJson($out, 200, $this->jsonStyle); @@ -205,9 +207,10 @@ class Links extends ApiController if (! isset($this->linkDb[$args['id']])) { throw new ApiLinkNotFoundException(); } - + $link = $this->linkDb[$args['id']]; unset($this->linkDb[(int) $args['id']]); $this->linkDb->save($this->conf->get('resource.page_cache')); + $this->history->deleteLink($link); return $response->withStatus(204); } -- cgit v1.2.3