From 5d6d13c95fa8cf29794c62dcc9bdbe4c7efa9c57 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya <alejandro@alejandrocelaya.com> Date: Mon, 13 Aug 2018 16:17:43 +0200 Subject: [PATCH] Updated API docs including new response structure --- docs/swagger/definitions/ShortUrl.json | 11 ++++++++- docs/swagger/paths/v1_short-codes.json | 11 +++++---- .../paths/v1_short-codes_{shortCode}.json | 19 ++++++++------- .../src/Repository/ShortUrlRepository.php | 11 +++++++-- .../Repository/ShortUrlRepositoryTest.php | 24 +++++++++++++++++++ 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/docs/swagger/definitions/ShortUrl.json b/docs/swagger/definitions/ShortUrl.json index 6b1750b8..14cd0fe5 100644 --- a/docs/swagger/definitions/ShortUrl.json +++ b/docs/swagger/definitions/ShortUrl.json @@ -5,7 +5,11 @@ "type": "string", "description": "The short code for this short URL." }, - "originalUrl": { + "shortUrl": { + "type": "string", + "description": "The short URL." + }, + "longUrl": { "type": "string", "description": "The original long URL." }, @@ -24,6 +28,11 @@ "type": "string" }, "description": "A list of tags applied to this short URL" + }, + "originalUrl": { + "deprecated": true, + "type": "string", + "description": "The original long URL. [DEPRECATED. Use longUrl instead]" } } } diff --git a/docs/swagger/paths/v1_short-codes.json b/docs/swagger/paths/v1_short-codes.json index f377e67c..48d16a16 100644 --- a/docs/swagger/paths/v1_short-codes.json +++ b/docs/swagger/paths/v1_short-codes.json @@ -44,7 +44,7 @@ "schema": { "type": "string", "enum": [ - "originalUrl", + "longUrl", "shortCode", "dateCreated", "visits" @@ -89,7 +89,8 @@ "data": [ { "shortCode": "12C18", - "originalUrl": "https://store.steampowered.com", + "shortUrl": "https://doma.in/12C18", + "longUrl": "https://store.steampowered.com", "dateCreated": "2016-08-21T20:34:16+02:00", "visitsCount": 328, "tags": [ @@ -99,7 +100,8 @@ }, { "shortCode": "12Kb3", - "originalUrl": "https://shlink.io", + "shortUrl": "https://doma.in/12Kb3", + "longUrl": "https://shlink.io", "dateCreated": "2016-05-01T20:34:16+02:00", "visitsCount": 1029, "tags": [ @@ -108,7 +110,8 @@ }, { "shortCode": "123bA", - "originalUrl": "https://www.google.com", + "shortUrl": "https://doma.in/123bA", + "longUrl": "https://www.google.com", "dateCreated": "2015-10-01T20:34:16+02:00", "visitsCount": 25, "tags": [] diff --git a/docs/swagger/paths/v1_short-codes_{shortCode}.json b/docs/swagger/paths/v1_short-codes_{shortCode}.json index d0c26622..dba85af4 100644 --- a/docs/swagger/paths/v1_short-codes_{shortCode}.json +++ b/docs/swagger/paths/v1_short-codes_{shortCode}.json @@ -23,23 +23,24 @@ ], "responses": { "200": { - "description": "The long URL behind a short code.", + "description": "The URL info behind a short code.", "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "longUrl": { - "type": "string", - "description": "The original long URL behind the short code." - } - } + "$ref": "../definitions/ShortUrl.json" } } }, "examples": { "application/json": { - "longUrl": "https://shlink.io" + "shortCode": "12Kb3", + "shortUrl": "https://doma.in/12Kb3", + "longUrl": "https://shlink.io", + "dateCreated": "2016-05-01T20:34:16+02:00", + "visitsCount": 1029, + "tags": [ + "shlink" + ] } } }, diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index 42b2c73e..6a96f45f 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -47,6 +47,13 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI protected function processOrderByForList(QueryBuilder $qb, $orderBy) { + // Map public field names to column names + $fieldNameMap = [ + 'originalUrl' => 'originalUrl', + 'longUrl' => 'originalUrl', + 'shortCode' => 'shortCode', + 'dateCreated' => 'dateCreated', + ]; $fieldName = \is_array($orderBy) ? \key($orderBy) : $orderBy; $order = \is_array($orderBy) ? $orderBy[$fieldName] : 'ASC'; @@ -59,8 +66,8 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI return \array_column($qb->getQuery()->getResult(), 0); } - if (\in_array($fieldName, ['originalUrl', 'shortCode', 'dateCreated'], true)) { - $qb->orderBy('s.' . $fieldName, $order); + if (\array_key_exists($fieldName, $fieldNameMap)) { + $qb->orderBy('s.' . $fieldNameMap[$fieldName], $order); } return $qb->getQuery()->getResult(); } diff --git a/module/Core/test-func/Repository/ShortUrlRepositoryTest.php b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php index 1a0cf1e6..a3e98ebb 100644 --- a/module/Core/test-func/Repository/ShortUrlRepositoryTest.php +++ b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php @@ -112,4 +112,28 @@ class ShortUrlRepositoryTest extends DatabaseTestCase $this->assertCount(1, $result); $this->assertSame($foo, $result[0]); } + + /** + * @test + */ + public function findListProperlyMapsFieldNamesToColumnNamesWhenOrdering() + { + $urls = ['a', 'z', 'c', 'b']; + foreach($urls as $url) { + $this->getEntityManager()->persist( + (new ShortUrl())->setShortCode($url) + ->setLongUrl($url) + ); + } + + $this->getEntityManager()->flush(); + + $result = $this->repo->findList(null, null, null, [], ['longUrl' => 'ASC']); + + $this->assertCount(\count($urls), $result); + $this->assertEquals('a', $result[0]->getLongUrl()); + $this->assertEquals('b', $result[1]->getLongUrl()); + $this->assertEquals('c', $result[2]->getLongUrl()); + $this->assertEquals('z', $result[3]->getLongUrl()); + } }