mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Handled tag conflict from rename tag action
This commit is contained in:
@@ -8,8 +8,8 @@ use function sprintf;
|
|||||||
|
|
||||||
class TagConflictException extends RuntimeException
|
class TagConflictException extends RuntimeException
|
||||||
{
|
{
|
||||||
public static function fromExistingTag(string $tag): self
|
public static function fromExistingTag(string $oldName, string $newName): self
|
||||||
{
|
{
|
||||||
return new self(sprintf('Tag with name %s already exists', $tag));
|
return new self(sprintf('You cannot rename tag %s to %s, because it already exists', $oldName, $newName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ class TagService implements TagServiceInterface
|
|||||||
|
|
||||||
$newNameExists = $newName !== $oldName && $repo->count(['name' => $newName]) > 0;
|
$newNameExists = $newName !== $oldName && $repo->count(['name' => $newName]) > 0;
|
||||||
if ($newNameExists) {
|
if ($newNameExists) {
|
||||||
throw TagConflictException::fromExistingTag($newName);
|
throw TagConflictException::fromExistingTag($oldName, $newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tag->rename($newName);
|
$tag->rename($newName);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\TagConflictException;
|
||||||
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
|
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
|
||||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||||
@@ -58,6 +59,15 @@ class UpdateTagAction extends AbstractRestAction
|
|||||||
'error' => RestUtils::NOT_FOUND_ERROR,
|
'error' => RestUtils::NOT_FOUND_ERROR,
|
||||||
'message' => sprintf('It was not possible to find a tag with name %s', $body['oldName']),
|
'message' => sprintf('It was not possible to find a tag with name %s', $body['oldName']),
|
||||||
], self::STATUS_NOT_FOUND);
|
], self::STATUS_NOT_FOUND);
|
||||||
|
} catch (TagConflictException $e) {
|
||||||
|
return new JsonResponse([
|
||||||
|
'error' => 'TAG_CONFLICT',
|
||||||
|
'message' => sprintf(
|
||||||
|
'You cannot rename tag %s to %s, because it already exists',
|
||||||
|
$body['oldName'],
|
||||||
|
$body['newName']
|
||||||
|
),
|
||||||
|
], self::STATUS_CONFLICT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
module/Rest/test-api/Action/UpdateTagActionTest.php
Normal file
35
module/Rest/test-api/Action/UpdateTagActionTest.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||||
|
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||||
|
|
||||||
|
class UpdateTagActionTest extends ApiTestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
public function errorIsThrownWhenTryingToRenameTagToAnotherTagName(): void
|
||||||
|
{
|
||||||
|
$resp = $this->callApiWithKey(self::METHOD_PUT, '/tags', [RequestOptions::JSON => [
|
||||||
|
'oldName' => 'foo',
|
||||||
|
'newName' => 'bar',
|
||||||
|
]]);
|
||||||
|
$payload = $this->getJsonResponsePayload($resp);
|
||||||
|
|
||||||
|
$this->assertEquals(self::STATUS_CONFLICT, $resp->getStatusCode());
|
||||||
|
$this->assertEquals('TAG_CONFLICT', $payload['error']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tagIsProperlyRenamedWhenRenamingToItself(): void
|
||||||
|
{
|
||||||
|
$resp = $this->callApiWithKey(self::METHOD_PUT, '/tags', [RequestOptions::JSON => [
|
||||||
|
'oldName' => 'foo',
|
||||||
|
'newName' => 'foo',
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$this->assertEquals(self::STATUS_NO_CONTENT, $resp->getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user