mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-17 17:57:09 +00:00
Add notes to category #4002
This commit is contained in:
@@ -28,6 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\CategoryFactory;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RecurrenceTransactionMeta;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Services\Internal\Destroy\CategoryDestroyService;
|
||||
@@ -202,7 +203,7 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -241,10 +242,28 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
if (null === $category) {
|
||||
throw new FireflyException(sprintf('400003: Could not store new category with name "%s"', $data['name']));
|
||||
}
|
||||
|
||||
if (array_key_exists('notes', $data) && '' === $data['notes']) {
|
||||
$this->removeNotes($category);
|
||||
}
|
||||
if (array_key_exists('notes', $data) && '' !== $data['notes']) {
|
||||
$this->updateNotes($category, $data['notes']);
|
||||
}
|
||||
|
||||
return $category;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
*/
|
||||
public function removeNotes(Category $category): void
|
||||
{
|
||||
$category->notes()->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
* @param array $data
|
||||
@@ -383,4 +402,31 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function updateNotes(Category $category, string $notes): void
|
||||
{
|
||||
$dbNote = $category->notes()->first();
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note;
|
||||
$dbNote->noteable()->associate($category);
|
||||
}
|
||||
$dbNote->text = trim($notes);
|
||||
$dbNote->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getNoteText(Category $category): ?string
|
||||
{
|
||||
$dbNote = $category->notes()->first();
|
||||
if (null === $dbNote) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $dbNote->text;
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,27 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface CategoryRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Remove notes.
|
||||
*
|
||||
* @param Category $category
|
||||
*/
|
||||
public function removeNotes(Category $category): void;
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
* @param string $notes
|
||||
*/
|
||||
public function updateNotes(Category $category, string $notes): void;
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getNoteText(Category $category): ?string;
|
||||
|
||||
/**
|
||||
* Delete all categories.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user