diff --git a/docs/swagger/paths/v1_short-urls_{shortCode}.json b/docs/swagger/paths/v1_short-urls_{shortCode}.json index 546e1920..6cfa3c97 100644 --- a/docs/swagger/paths/v1_short-urls_{shortCode}.json +++ b/docs/swagger/paths/v1_short-urls_{shortCode}.json @@ -150,8 +150,33 @@ } ], "responses": { - "204": { - "description": "The short code has been properly updated." + "200": { + "description": "The short URL has been properly updated.", + "content": { + "application/json": { + "schema": { + "$ref": "../definitions/ShortUrl.json" + } + } + }, + "examples": { + "application/json": { + "shortCode": "12Kb3", + "shortUrl": "https://doma.in/12Kb3", + "longUrl": "https://shlink.io", + "dateCreated": "2016-05-01T20:34:16+02:00", + "visitsCount": 1029, + "tags": [ + "shlink" + ], + "meta": { + "validSince": "2017-01-21T00:00:00+02:00", + "validUntil": null, + "maxVisits": 100 + }, + "domain": null + } + } }, "400": { "description": "Provided meta arguments are invalid.", diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index dc960fb4..7891b2a0 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -59,7 +59,7 @@ return [ Service\UrlShortener::class, 'config.url_shortener.domain', ], - Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class], + Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'], Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class], Action\ShortUrl\ResolveShortUrlAction::class => [ Service\ShortUrl\ShortUrlResolver::class, diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php index d7d283c7..672d3963 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlAction.php @@ -4,12 +4,13 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Action\ShortUrl; -use Laminas\Diactoros\Response\EmptyResponse; +use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Core\Model\ShortUrlEdit; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; +use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware; @@ -19,10 +20,12 @@ class EditShortUrlAction extends AbstractRestAction protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT]; private ShortUrlServiceInterface $shortUrlService; + private ShortUrlDataTransformer $transformer; - public function __construct(ShortUrlServiceInterface $shortUrlService) + public function __construct(ShortUrlServiceInterface $shortUrlService, array $domainConfig) { $this->shortUrlService = $shortUrlService; + $this->transformer = new ShortUrlDataTransformer($domainConfig); } public function handle(ServerRequestInterface $request): ResponseInterface @@ -31,7 +34,8 @@ class EditShortUrlAction extends AbstractRestAction $identifier = ShortUrlIdentifier::fromApiRequest($request); $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request); - $this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey); - return new EmptyResponse(); + $shortUrl = $this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey); + + return new JsonResponse($this->transformer->transform($shortUrl)); } } diff --git a/module/Rest/test-api/Action/EditShortUrlTest.php b/module/Rest/test-api/Action/EditShortUrlTest.php index 84612b0f..6652c1a4 100644 --- a/module/Rest/test-api/Action/EditShortUrlTest.php +++ b/module/Rest/test-api/Action/EditShortUrlTest.php @@ -41,8 +41,8 @@ class EditShortUrlTest extends ApiTestCase ]); $metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode); - self::assertEquals(self::STATUS_NO_CONTENT, $editWithProvidedMeta->getStatusCode()); - self::assertEquals(self::STATUS_NO_CONTENT, $editWithResetMeta->getStatusCode()); + self::assertEquals(self::STATUS_OK, $editWithProvidedMeta->getStatusCode()); + self::assertEquals(self::STATUS_OK, $editWithResetMeta->getStatusCode()); self::assertEquals($resetMeta, $metaAfterResetting); self::assertArraySubset($meta, $metaAfterEditing); } @@ -93,7 +93,7 @@ class EditShortUrlTest extends ApiTestCase public function provideLongUrls(): iterable { - yield 'valid URL' => ['https://shlink.io', self::STATUS_NO_CONTENT, null]; + yield 'valid URL' => ['https://shlink.io', self::STATUS_OK, null]; yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL']; } @@ -155,7 +155,7 @@ class EditShortUrlTest extends ApiTestCase ]]); $editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url)); - self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode()); + self::assertEquals(self::STATUS_OK, $editResp->getStatusCode()); self::assertEquals($domain, $editedShortUrl['domain']); self::assertEquals($expectedUrl, $editedShortUrl['longUrl']); self::assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null); diff --git a/module/Rest/test-api/Action/ResolveShortUrlTest.php b/module/Rest/test-api/Action/ResolveShortUrlTest.php index d256a2ad..ca99f058 100644 --- a/module/Rest/test-api/Action/ResolveShortUrlTest.php +++ b/module/Rest/test-api/Action/ResolveShortUrlTest.php @@ -29,7 +29,7 @@ class ResolveShortUrlTest extends ApiTestCase $visitResp = $this->callShortUrl($shortCode); $fetchResp = $this->callApiWithKey(self::METHOD_GET, $url); - self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode()); + self::assertEquals(self::STATUS_OK, $editResp->getStatusCode()); self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode()); self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode()); } diff --git a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php b/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php index 7a7d19ee..be70eec8 100644 --- a/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php +++ b/module/Rest/test/Action/ShortUrl/EditShortUrlActionTest.php @@ -25,7 +25,7 @@ class EditShortUrlActionTest extends TestCase public function setUp(): void { $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); - $this->action = new EditShortUrlAction($this->shortUrlService->reveal()); + $this->action = new EditShortUrlAction($this->shortUrlService->reveal(), []); } /** @test */ @@ -54,7 +54,7 @@ class EditShortUrlActionTest extends TestCase $resp = $this->action->handle($request); - self::assertEquals(204, $resp->getStatusCode()); + self::assertEquals(200, $resp->getStatusCode()); $updateMeta->shouldHaveBeenCalled(); } }