mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-06 14:12:15 +00:00
Expand API for attachments.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user