Add notes to category #4002

This commit is contained in:
James Cole
2020-10-28 06:32:37 +01:00
parent 3aa835a985
commit ca3d836c83
7 changed files with 92 additions and 9 deletions

View File

@@ -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;
}
}

View File

@@ -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.
*/