0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Extended save method to return saved collection data

refs https://github.com/TryGhost/Team/issues/3167

- The core client (API) needs a way to pass in information without an ID when creating a new entity, handling it on service/repository layer makes the most sense.
- Used "require" syntax to import tpl/errors modules, otherwise TS compiler was complaining about type compatibility issues, this works as a temporary workaround and is tracked and an issue to improve in the future.
This commit is contained in:
Naz 2023-05-17 13:42:35 +07:00 committed by naz
parent 36eff3a481
commit 2d1eb881fc
4 changed files with 86 additions and 8 deletions

View file

@ -28,6 +28,9 @@
"typescript": "5.0.4"
},
"dependencies": {
"@tryghost/in-memory-repository": "0.0.0"
"@tryghost/errors": "^1.2.25",
"@tryghost/in-memory-repository": "0.0.0",
"@tryghost/tpl": "^0.1.25",
"bson-objectid": "^2.0.4"
}
}

View file

@ -1,11 +1,46 @@
// have to use requires until there are type definitions for these modules
const {ValidationError} = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
import ObjectID from 'bson-objectid';
import {InMemoryRepository} from '@tryghost/in-memory-repository';
import {Collection} from './Collection';
const messages = {
invalidIDProvided: 'Invalid ID provided for Collection'
};
export class CollectionsRepositoryInMemory extends InMemoryRepository<string, Collection> {
constructor() {
super();
}
async create(data: any): Promise<Collection> {
let id;
if (!data.id) {
id = new ObjectID();
} else if (typeof data.id === 'string') {
id = ObjectID.createFromHexString(data.id);
} else if (data.id instanceof ObjectID) {
id = data.id;
} else {
throw new ValidationError({
message: tpl(messages.invalidIDProvided)
});
}
return {
id: id.toHexString(),
title: data.title,
description: data.description,
type: data.type,
filter: data.filter,
feature_image: data.feature_image,
deleted: data.deleted || false
};
}
protected toPrimitive(entity: Collection): object {
return {
title: entity.title,

View file

@ -11,8 +11,10 @@ export class CollectionsService {
this.repository = deps.repository;
}
async save(collection: Collection): Promise<Collection> {
return await this.repository.save(collection);
async save(data: any): Promise<Collection> {
const collection = await this.repository.create(data);
await this.repository.save(collection);
return collection;
}
async getById(id: string): Promise<Collection | null> {

View file

@ -1,4 +1,5 @@
import assert from 'assert';
import ObjectID from 'bson-objectid';
import {CollectionsService} from '../src/index';
import {CollectionsRepositoryInMemory} from '../src/CollectionsRepositoryInMemory';
@ -13,8 +14,7 @@ describe('collections', function () {
const repository = new CollectionsRepositoryInMemory();
const collectionsService = new CollectionsService({repository});
await collectionsService.save({
id: 'test_id_1',
const savedCollection = await collectionsService.save({
title: 'testing collections',
description: 'testing collections description',
type: 'manual',
@ -23,17 +23,55 @@ describe('collections', function () {
deleted: false
});
const createdCollection = await collectionsService.getById('test_id_1');
const createdCollection = await collectionsService.getById(savedCollection.id);
assert.ok(createdCollection, 'Collection should be saved');
assert.ok(savedCollection.id, 'Collection should have an id');
assert.equal(createdCollection.title, 'testing collections', 'Collection title should match');
const allCollections = await collectionsService.getAll();
assert.equal(allCollections.length, 1, 'There should be one collection');
await collectionsService.destroy('test_id_1');
const deletedCollection = await collectionsService.getById('test_id_1');
await collectionsService.destroy(savedCollection.id);
const deletedCollection = await collectionsService.getById(savedCollection.id);
assert.equal(deletedCollection, null, 'Collection should be deleted');
});
it('Can create a collection with predefined ID', async function () {
const repository = new CollectionsRepositoryInMemory();
const collectionsService = new CollectionsService({repository});
const id = new ObjectID();
const savedCollection = await collectionsService.save({
id: id.toHexString()
});
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
});
it('Can create a collection with predefined ObjectID instance', async function () {
const repository = new CollectionsRepositoryInMemory();
const collectionsService = new CollectionsService({repository});
const id = new ObjectID();
const savedCollection = await collectionsService.save({
id: id
});
assert.equal(savedCollection.id, id.toHexString(), 'Collection should have same id');
});
it('Throws an error when trying to save a collection with an invalid ID', async function () {
const repository = new CollectionsRepositoryInMemory();
const collectionsService = new CollectionsService({repository});
try {
await collectionsService.save({
id: 12345
});
} catch (error: any) {
assert.equal(error.message, 'Invalid ID provided for Collection', 'Error message should match');
}
});
});