Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Favorites feature #125

Open
wants to merge 13 commits into
base: viewmodes
Choose a base branch
from
Prev Previous commit
Next Next commit
Add priority column to favorites
When inserting a new favorite, the new favorite gets the priority + 1 of
the favorite with the currently highest priority. If no favorite is
stored for the kind the new favorite gets the priority 0.

When deleting a favorite, all favorites with higher priority are
decremented by 1.
jrauh01 committed Mar 5, 2025
commit 8e17003291c5716f412032c2e0a815f0ba7e2725
51 changes: 51 additions & 0 deletions application/controllers/FavoriteController.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,10 @@
use Icinga\Module\Kubernetes\Common\Database;
use Icinga\Module\Kubernetes\Forms\FavorForm;
use Icinga\Module\Kubernetes\Forms\UnfavorForm;
use Icinga\Module\Kubernetes\Model\Favorite;
use Icinga\Module\Kubernetes\Web\Controller;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
use Throwable;

class FavoriteController extends Controller
@@ -29,12 +32,24 @@ public function favorAction(): void
$username = Auth::getInstance()->getUser()->getUsername();

try {
$highestPriorityFavorite = Favorite::on($db)
->columns('priority')
->filter(
Filter::all(
Filter::equal('kind', $kind),
Filter::equal('username', $username)
)
)
->orderBy('priority', SORT_DESC)
->first();

$db->insert(
'favorite',
[
'resource_uuid' => $uuid,
'kind' => $kind,
'username' => $username,
'priority' => ($highestPriorityFavorite?->priority ?? -1) + 1,
]
);
} catch (Throwable $e) {
@@ -62,13 +77,49 @@ public function unfavorAction(): void
$uuid = $this->params->get('uuid');
$username = Auth::getInstance()->getUser()->getUsername();
try {
$transactionStarted = false;
if (! $db->inTransaction()) {
$transactionStarted = true;
$db->beginTransaction();
}

$favoriteToDelete = Favorite::on($db)
->filter(Filter::all(
Filter::equal('resource_uuid', $uuid),
Filter::equal('username', $username)
))
->first();

$db->delete(
'favorite',
[
'resource_uuid = ?' => $uuid,
'username = ?' => $username,
]
);

$affectedFavorites = Favorite::on($db)
->columns(['resource_uuid', 'username'])
->filter(
Filter::all(
Filter::equal('kind', $favoriteToDelete->kind),
Filter::equal('username', $username),
Filter::greaterThan('priority', $favoriteToDelete->priority)
)
)
->orderBy('priority', SORT_ASC);

foreach ($affectedFavorites as $favorite) {
$db->update(
'favorite',
['priority' => new Expression('priority - 1')],
['resource_uuid = ?' => $favorite->resource_uuid, 'username = ?' => $favorite->username]
);
}

if ($transactionStarted) {
$db->commitTransaction();
}
} catch (Throwable $e) {
Logger::error($e);
Logger::error($e->getTraceAsString());
2 changes: 2 additions & 0 deletions library/Kubernetes/Model/Favorite.php
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ public function getColumnDefinitions(): array
'resource_uuid' => $this->translate('Resource UUID'),
'kind' => $this->translate('Resource Kind'),
'username' => $this->translate('Username'),
'priority' => $this->translate('Priority'),
];
}

@@ -65,6 +66,7 @@ public function getColumns(): array
'resource_uuid',
'kind',
'username',
'priority',
];
}