Expand API for attachments.

This commit is contained in:
James Cole
2018-06-24 06:51:22 +02:00
parent 32b6ded008
commit ad6a9a7df7
28 changed files with 1290 additions and 59 deletions

View File

@@ -25,6 +25,8 @@ namespace FireflyIII\Repositories\Attachment;
use Carbon\Carbon;
use Crypt;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AttachmentFactory;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Note;
@@ -179,11 +181,30 @@ class AttachmentRepository implements AttachmentRepositoryInterface
/**
* @param User $user
*/
public function setUser(User $user)
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
*
* @return Attachment
* @throws FireflyException
*/
public function store(array $data): Attachment
{
/** @var AttachmentFactory $factory */
$factory = app(AttachmentFactory::class);
$factory->setUser($this->user);
$result = $factory->create($data);
if (null === $result) {
throw new FireflyException('Could not store attachment.');
}
return $result;
}
/**
* @param Attachment $attachment
* @param array $data
@@ -193,8 +214,13 @@ class AttachmentRepository implements AttachmentRepositoryInterface
public function update(Attachment $attachment, array $data): Attachment
{
$attachment->title = $data['title'];
// update filename, if present and different:
if (isset($data['filename']) && '' !== $data['filename'] && $data['filename'] !== $attachment->filename) {
$attachment->filename = $data['filename'];
}
$attachment->save();
$this->updateNote($attachment, $data['notes']);
$this->updateNote($attachment, $data['notes'] ?? '');
return $attachment;
}
@@ -207,7 +233,7 @@ class AttachmentRepository implements AttachmentRepositoryInterface
*/
public function updateNote(Attachment $attachment, string $note): bool
{
if (0 === \strlen($note)) {
if ('' === $note) {
$dbNote = $attachment->notes()->first();
if (null !== $dbNote) {
$dbNote->delete();

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Attachment;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Attachment;
use FireflyIII\User;
use Illuminate\Support\Collection;
@@ -95,6 +96,14 @@ interface AttachmentRepositoryInterface
*/
public function setUser(User $user);
/**
* @param array $data
*
* @return Attachment
* @throws FireflyException
*/
public function store(array $data): Attachment;
/**
* @param Attachment $attachment
* @param array $attachmentData