Add ability to patch an entry with more fields

Like when we create an entry, we can now patch an entry with new fields:
- content
- language
- preview_picture
- published_at
- authors
This commit is contained in:
Jeremy Benoist 2017-06-02 20:52:49 +02:00
parent a687c8d915
commit 645291e8fe
No known key found for this signature in database
GPG Key ID: BCA73962457ACC3C
3 changed files with 47 additions and 4 deletions

View File

@ -374,6 +374,11 @@ class EntryRestController extends WallabagRestController
* {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
* {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
* {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
* {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"},
* {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"},
* {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
* {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"},
* {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
* } * }
* ) * )
* *
@ -385,11 +390,39 @@ class EntryRestController extends WallabagRestController
$this->validateUserAccess($entry->getUser()->getId()); $this->validateUserAccess($entry->getUser()->getId());
$title = $request->request->get('title'); $title = $request->request->get('title');
$tags = $request->request->get('tags', '');
$isArchived = $request->request->get('archive'); $isArchived = $request->request->get('archive');
$isStarred = $request->request->get('starred'); $isStarred = $request->request->get('starred');
$content = $request->request->get('content');
$language = $request->request->get('language');
$picture = $request->request->get('preview_picture');
$publishedAt = $request->request->get('published_at');
$authors = $request->request->get('authors', '');
if (!is_null($title)) { try {
$entry->setTitle($title); $this->get('wallabag_core.content_proxy')->updateEntry(
$entry,
$entry->getUrl(),
[
'title' => !empty($title) ? $title : $entry->getTitle(),
'html' => !empty($content) ? $content : $entry->getContent(),
'url' => $entry->getUrl(),
'language' => $language,
'date' => $publishedAt,
// faking the preview picture
'open_graph' => [
'og_image' => $picture,
],
'authors' => is_string($authors) ? explode(',', $authors) : [],
],
// we don't want the content to be update by fetching the url
true
);
} catch (\Exception $e) {
$this->get('logger')->error('Error while saving an entry', [
'exception' => $e,
'entry' => $entry,
]);
} }
if (!is_null($isArchived)) { if (!is_null($isArchived)) {
@ -400,7 +433,6 @@ class EntryRestController extends WallabagRestController
$entry->setStarred((bool) $isStarred); $entry->setStarred((bool) $isStarred);
} }
$tags = $request->request->get('tags', '');
if (!empty($tags)) { if (!empty($tags)) {
$this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
} }

View File

@ -105,7 +105,7 @@ class ContentProxy
} }
} }
if (!empty($content['authors'])) { if (!empty($content['authors']) && is_array($content['authors'])) {
$entry->setPublishedBy($content['authors']); $entry->setPublishedBy($content['authors']);
} }

View File

@ -477,6 +477,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
'tags' => 'new tag '.uniqid(), 'tags' => 'new tag '.uniqid(),
'starred' => '1', 'starred' => '1',
'archive' => '0', 'archive' => '0',
'language' => 'de_DE',
'preview_picture' => 'http://preview.io/picture.jpg',
'authors' => 'bob,sponge',
'content' => 'awesome',
]); ]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@ -488,6 +492,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
$this->assertEquals('New awesome title', $content['title']); $this->assertEquals('New awesome title', $content['title']);
$this->assertGreaterThan($nbTags, count($content['tags'])); $this->assertGreaterThan($nbTags, count($content['tags']));
$this->assertEquals(1, $content['user_id']); $this->assertEquals(1, $content['user_id']);
$this->assertEquals('de_DE', $content['language']);
$this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
$this->assertContains('sponge', $content['published_by']);
$this->assertContains('bob', $content['published_by']);
$this->assertEquals('awesome', $content['content']);
} }
public function testPatchEntryWithoutQuotes() public function testPatchEntryWithoutQuotes()
@ -509,6 +518,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
'tags' => 'new tag '.uniqid(), 'tags' => 'new tag '.uniqid(),
'starred' => 1, 'starred' => 1,
'archive' => 0, 'archive' => 0,
'authors' => ['bob', 'sponge'],
]); ]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@ -519,6 +529,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
$this->assertEquals($entry->getUrl(), $content['url']); $this->assertEquals($entry->getUrl(), $content['url']);
$this->assertEquals('New awesome title', $content['title']); $this->assertEquals('New awesome title', $content['title']);
$this->assertGreaterThan($nbTags, count($content['tags'])); $this->assertGreaterThan($nbTags, count($content['tags']));
$this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
} }
public function testGetTagsEntry() public function testGetTagsEntry()