mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-10 12:24:50 +00:00
Update autocomplete code for #3150
This commit is contained in:
@@ -71,12 +71,11 @@ class PiggyBankController extends Controller
|
||||
$piggies = $this->piggyRepository->searchPiggyBank($data['query'], $data['limit']);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrency();
|
||||
$response = [];
|
||||
|
||||
/** @var PiggyBank $piggy */
|
||||
foreach ($piggies as $piggy) {
|
||||
$currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency;
|
||||
$piggy->objectGroup = $piggy->objectGroups->first();
|
||||
$piggy->name_with_amount
|
||||
= $response[] = [
|
||||
$response[] = [
|
||||
'id' => $piggy->id,
|
||||
'name' => $piggy->name,
|
||||
'currency_id' => $currency->id,
|
||||
@@ -104,9 +103,7 @@ class PiggyBankController extends Controller
|
||||
foreach ($piggies as $piggy) {
|
||||
$currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency;
|
||||
$currentAmount = $this->piggyRepository->getRepetition($piggy)->currentamount ?? '0';
|
||||
$piggy->objectGroup = $piggy->objectGroups->first();
|
||||
$piggy->name_with_amount
|
||||
= $response[] = [
|
||||
$response[] = [
|
||||
'id' => $piggy->id,
|
||||
'name' => $piggy->name,
|
||||
'name_with_balance' => sprintf(
|
||||
|
||||
@@ -25,6 +25,11 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
|
||||
|
||||
|
||||
use FireflyIII\Api\V1\Controllers\Controller;
|
||||
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* Class TagController
|
||||
@@ -32,4 +37,46 @@ use FireflyIII\Api\V1\Controllers\Controller;
|
||||
class TagController extends Controller
|
||||
{
|
||||
|
||||
private TagRepositoryInterface $repository;
|
||||
|
||||
/**
|
||||
* CurrencyController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$this->repository = app(TagRepositoryInterface::class);
|
||||
$this->repository->setUser($user);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AutocompleteRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function tags(AutocompleteRequest $request): JsonResponse
|
||||
{
|
||||
$data = $request->getData();
|
||||
|
||||
$result = $this->repository->searchTags($data['query'], $data['limit']);
|
||||
$array = [];
|
||||
/** @var Tag $tag */
|
||||
foreach ($result as $tag) {
|
||||
$array[] = [
|
||||
'id' => $tag->id,
|
||||
'name' => $tag->tag,
|
||||
'tag' => $tag->tag,
|
||||
];
|
||||
}
|
||||
|
||||
return response()->json($array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,27 +121,8 @@ class AutoCompleteController extends Controller
|
||||
|
||||
return response()->json($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function tags(Request $request): JsonResponse
|
||||
{
|
||||
$search = (string) $request->get('search');
|
||||
/** @var TagRepositoryInterface $repository */
|
||||
$repository = app(TagRepositoryInterface::class);
|
||||
$result = $repository->searchTags($search);
|
||||
$array = $result->toArray();
|
||||
foreach ($array as $index => $item) {
|
||||
// rename field for consistency.
|
||||
$array[$index]['name'] = $item['tag'];
|
||||
}
|
||||
|
||||
return response()->json($array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
|
||||
@@ -414,7 +414,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
||||
$search->where('piggy_banks.name', 'LIKE', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search->orderBy('piggy_banks.order', 'ASC')
|
||||
->orderBy('piggy_banks.name', 'ASC')->where('piggy_banks.active', 1);
|
||||
->orderBy('piggy_banks.name', 'ASC');
|
||||
|
||||
return $search->take($limit)->get();
|
||||
}
|
||||
|
||||
@@ -287,10 +287,11 @@ class TagRepository implements TagRepositoryInterface
|
||||
* Search the users tags.
|
||||
*
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTags(string $query): Collection
|
||||
public function searchTags(string $query, int $limit): Collection
|
||||
{
|
||||
/** @var Collection $tags */
|
||||
$tags = $this->user->tags()->orderBy('tag', 'ASC');
|
||||
@@ -299,7 +300,7 @@ class TagRepository implements TagRepositoryInterface
|
||||
$tags->where('tag', 'LIKE', $search);
|
||||
}
|
||||
|
||||
return $tags->get();
|
||||
return $tags->take($limit)->get('tags.*');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -165,10 +165,11 @@ interface TagRepositoryInterface
|
||||
* Search the users tags.
|
||||
*
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTags(string $query): Collection;
|
||||
public function searchTags(string $query, int $limit): Collection;
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
|
||||
Reference in New Issue
Block a user