From 25e0cb12c0a592ed93ae6370d94383717f3f989a Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 16 Jun 2023 16:31:37 +0700 Subject: [PATCH] Added "getBySlug" method to Collections Service refs https://github.com/TryGhost/Team/issues/3423 - This is a convenience method that should allow fetching collections by their slug. It is a great developer experience for in case when fetching built-in collections like "featured" and "index", so can avoid an extra call to find the collection and it's ide one wants to use. --- ghost/collections/src/CollectionRepository.ts | 2 ++ .../src/CollectionsRepositoryInMemory.ts | 4 ++++ ghost/collections/src/CollectionsService.ts | 4 ++++ ghost/collections/test/collections.test.ts | 21 +++++++++++++++++-- .../src/InMemoryRepository.ts | 2 +- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ghost/collections/src/CollectionRepository.ts b/ghost/collections/src/CollectionRepository.ts index feb46b8d3a..f81dc7ea95 100644 --- a/ghost/collections/src/CollectionRepository.ts +++ b/ghost/collections/src/CollectionRepository.ts @@ -1,6 +1,8 @@ import {Collection} from './Collection'; + export interface CollectionRepository { save(collection: Collection): Promise getById(id: string): Promise + getBySlug(slug: string): Promise getAll(options: any): Promise } diff --git a/ghost/collections/src/CollectionsRepositoryInMemory.ts b/ghost/collections/src/CollectionsRepositoryInMemory.ts index 31e4eb8b08..b78275c5c1 100644 --- a/ghost/collections/src/CollectionsRepositoryInMemory.ts +++ b/ghost/collections/src/CollectionsRepositoryInMemory.ts @@ -5,4 +5,8 @@ export class CollectionsRepositoryInMemory extends InMemoryRepository { + return this.store.find(item => item.slug === slug) || null; + } } diff --git a/ghost/collections/src/CollectionsService.ts b/ghost/collections/src/CollectionsService.ts index acdd2d176a..437c7fbb68 100644 --- a/ghost/collections/src/CollectionsService.ts +++ b/ghost/collections/src/CollectionsService.ts @@ -228,6 +228,10 @@ export class CollectionsService { return await this.collectionsRepository.getById(id); } + async getBySlug(slug: string): Promise { + return await this.collectionsRepository.getBySlug(slug); + } + async getAll(options?: QueryOptions): Promise<{data: CollectionDTO[], meta: any}> { const collections = await this.collectionsRepository.getAll(options); diff --git a/ghost/collections/test/collections.test.ts b/ghost/collections/test/collections.test.ts index d046d51b3a..a06549a563 100644 --- a/ghost/collections/test/collections.test.ts +++ b/ghost/collections/test/collections.test.ts @@ -49,18 +49,35 @@ describe('CollectionsService', function () { const createdCollection = await collectionsService.getById(savedCollection.id); assert.ok(createdCollection, 'Collection should be saved'); - assert.ok(savedCollection.id, 'Collection should have an id'); + assert.ok(createdCollection.id, 'Collection should have an id'); assert.equal(createdCollection.title, 'testing collections', 'Collection title should match'); const allCollections = await collectionsService.getAll(); assert.equal(allCollections.data.length, 1, 'There should be one collection'); - await collectionsService.destroy(savedCollection.id); + await collectionsService.destroy(createdCollection.id); const deletedCollection = await collectionsService.getById(savedCollection.id); assert.equal(deletedCollection, null, 'Collection should be deleted'); }); + it('Can retrieve a collection by slug', async function () { + const savedCollection = await collectionsService.createCollection({ + title: 'slug test', + slug: 'get-me-by-slug', + type: 'manual', + filter: null + }); + + const retrievedCollection = await collectionsService.getBySlug('get-me-by-slug'); + assert.ok(retrievedCollection, 'Collection should be saved'); + assert.ok(retrievedCollection.slug, 'Collection should have a slug'); + assert.equal(savedCollection.title, 'slug test', 'Collection title should match'); + + const nonExistingCollection = await collectionsService.getBySlug('i-do-not-exist'); + assert.equal(nonExistingCollection, null, 'Collection should not exist'); + }); + it('Throws when built in collection is attempted to be deleted', async function () { const collection = await collectionsService.createCollection({ title: 'Featured Posts', diff --git a/ghost/in-memory-repository/src/InMemoryRepository.ts b/ghost/in-memory-repository/src/InMemoryRepository.ts index 5dfc4de21d..3bf9d3cd7b 100644 --- a/ghost/in-memory-repository/src/InMemoryRepository.ts +++ b/ghost/in-memory-repository/src/InMemoryRepository.ts @@ -13,7 +13,7 @@ type Order = { type OrderOption> = Order[]; export abstract class InMemoryRepository> { - private store: T[] = []; + protected store: T[] = []; private ids: Map = new Map(); constructor() {}