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.
This commit is contained in:
parent
361fea9977
commit
25e0cb12c0
@ -1,6 +1,8 @@
|
||||
import {Collection} from './Collection';
|
||||
|
||||
export interface CollectionRepository {
|
||||
save(collection: Collection): Promise<void>
|
||||
getById(id: string): Promise<Collection | null>
|
||||
getBySlug(slug: string): Promise<Collection | null>
|
||||
getAll(options: any): Promise<Collection[]>
|
||||
}
|
||||
|
@ -5,4 +5,8 @@ export class CollectionsRepositoryInMemory extends InMemoryRepository<string, Co
|
||||
protected toPrimitive(entity: Collection): object {
|
||||
return entity.toJSON();
|
||||
}
|
||||
|
||||
async getBySlug(slug: string): Promise<Collection | null> {
|
||||
return this.store.find(item => item.slug === slug) || null;
|
||||
}
|
||||
}
|
||||
|
@ -228,6 +228,10 @@ export class CollectionsService {
|
||||
return await this.collectionsRepository.getById(id);
|
||||
}
|
||||
|
||||
async getBySlug(slug: string): Promise<Collection | null> {
|
||||
return await this.collectionsRepository.getBySlug(slug);
|
||||
}
|
||||
|
||||
async getAll(options?: QueryOptions): Promise<{data: CollectionDTO[], meta: any}> {
|
||||
const collections = await this.collectionsRepository.getAll(options);
|
||||
|
||||
|
@ -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',
|
||||
|
@ -13,7 +13,7 @@ type Order<T> = {
|
||||
type OrderOption<T extends Entity<any>> = Order<T>[];
|
||||
|
||||
export abstract class InMemoryRepository<IDType, T extends Entity<IDType>> {
|
||||
private store: T[] = [];
|
||||
protected store: T[] = [];
|
||||
private ids: Map<IDType, true> = new Map();
|
||||
|
||||
constructor() {}
|
||||
|
Loading…
Reference in New Issue
Block a user