REST API: allow override of creation and update dates
Note that if they're not provided, default behaviour will apply: creation and update dates will be autogenerated, and not empty. Fixes #1223
This commit is contained in:
parent
bea062149e
commit
b06fc28aa3
4 changed files with 18 additions and 8 deletions
|
@ -94,7 +94,7 @@ public static function formatLink($bookmark, $indexUrl)
|
||||||
*
|
*
|
||||||
* @return Bookmark instance.
|
* @return Bookmark instance.
|
||||||
*/
|
*/
|
||||||
public static function buildLinkFromRequest($input, $defaultPrivate)
|
public static function buildBookmarkFromRequest($input, $defaultPrivate): Bookmark
|
||||||
{
|
{
|
||||||
$bookmark = new Bookmark();
|
$bookmark = new Bookmark();
|
||||||
$url = ! empty($input['url']) ? cleanup_url($input['url']) : '';
|
$url = ! empty($input['url']) ? cleanup_url($input['url']) : '';
|
||||||
|
@ -110,6 +110,15 @@ public static function buildLinkFromRequest($input, $defaultPrivate)
|
||||||
$bookmark->setTags(! empty($input['tags']) ? $input['tags'] : []);
|
$bookmark->setTags(! empty($input['tags']) ? $input['tags'] : []);
|
||||||
$bookmark->setPrivate($private);
|
$bookmark->setPrivate($private);
|
||||||
|
|
||||||
|
$created = \DateTime::createFromFormat(\DateTime::ATOM, $input['created'] ?? '');
|
||||||
|
if ($created instanceof \DateTimeInterface) {
|
||||||
|
$bookmark->setCreated($created);
|
||||||
|
}
|
||||||
|
$updated = \DateTime::createFromFormat(\DateTime::ATOM, $input['updated'] ?? '');
|
||||||
|
if ($updated instanceof \DateTimeInterface) {
|
||||||
|
$bookmark->setUpdated($updated);
|
||||||
|
}
|
||||||
|
|
||||||
return $bookmark;
|
return $bookmark;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use Shaarli\Bookmark\BookmarkServiceInterface;
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||||
use Shaarli\Config\ConfigManager;
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\History;
|
||||||
use Slim\Container;
|
use Slim\Container;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,7 +32,7 @@ abstract class ApiController
|
||||||
protected $bookmarkService;
|
protected $bookmarkService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var HistoryController
|
* @var History
|
||||||
*/
|
*/
|
||||||
protected $history;
|
protected $history;
|
||||||
|
|
||||||
|
|
|
@ -116,7 +116,7 @@ public function getLink($request, $response, $args)
|
||||||
public function postLink($request, $response)
|
public function postLink($request, $response)
|
||||||
{
|
{
|
||||||
$data = $request->getParsedBody();
|
$data = $request->getParsedBody();
|
||||||
$bookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
$bookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
||||||
// duplicate by URL, return 409 Conflict
|
// duplicate by URL, return 409 Conflict
|
||||||
if (! empty($bookmark->getUrl())
|
if (! empty($bookmark->getUrl())
|
||||||
&& ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
|
&& ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
|
||||||
|
@ -155,7 +155,7 @@ public function putLink($request, $response, $args)
|
||||||
$index = index_url($this->ci['environment']);
|
$index = index_url($this->ci['environment']);
|
||||||
$data = $request->getParsedBody();
|
$data = $request->getParsedBody();
|
||||||
|
|
||||||
$requestBookmark = ApiUtils::buildLinkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
$requestBookmark = ApiUtils::buildBookmarkFromRequest($data, $this->conf->get('privacy.default_private_links'));
|
||||||
// duplicate URL on a different link, return 409 Conflict
|
// duplicate URL on a different link, return 409 Conflict
|
||||||
if (! empty($requestBookmark->getUrl())
|
if (! empty($requestBookmark->getUrl())
|
||||||
&& ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
|
&& ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
|
||||||
|
|
|
@ -160,6 +160,8 @@ public function testPostLinkFull()
|
||||||
'description' => 'shaare description',
|
'description' => 'shaare description',
|
||||||
'tags' => ['one', 'two'],
|
'tags' => ['one', 'two'],
|
||||||
'private' => true,
|
'private' => true,
|
||||||
|
'created' => '2015-05-05T12:30:00+03:00',
|
||||||
|
'updated' => '2016-06-05T14:32:10+03:00',
|
||||||
];
|
];
|
||||||
$env = Environment::mock([
|
$env = Environment::mock([
|
||||||
'REQUEST_METHOD' => 'POST',
|
'REQUEST_METHOD' => 'POST',
|
||||||
|
@ -181,10 +183,8 @@ public function testPostLinkFull()
|
||||||
$this->assertEquals($link['description'], $data['description']);
|
$this->assertEquals($link['description'], $data['description']);
|
||||||
$this->assertEquals($link['tags'], $data['tags']);
|
$this->assertEquals($link['tags'], $data['tags']);
|
||||||
$this->assertEquals(true, $data['private']);
|
$this->assertEquals(true, $data['private']);
|
||||||
$this->assertTrue(
|
$this->assertSame($link['created'], $data['created']);
|
||||||
new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
|
$this->assertSame($link['updated'], $data['updated']);
|
||||||
);
|
|
||||||
$this->assertEquals('', $data['updated']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue