mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Added collection editing method
refs https://github.com/TryGhost/Team/issues/3169 - The collection entity properties used to be modified, which is not a good practice for logic encapsulation. Extracted editing logic to it's own method which can also contain validation logic when wrong data is passed when editing a collection.
This commit is contained in:
parent
58a18d37ea
commit
42539b954f
3 changed files with 79 additions and 4 deletions
|
@ -12,6 +12,12 @@ const messages = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type CollectionPost = {
|
||||||
|
id: string;
|
||||||
|
featured?: boolean;
|
||||||
|
published_at?: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export class Collection {
|
export class Collection {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
|
@ -40,6 +46,37 @@ export class Collection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public edit(data: Partial<Collection>) {
|
||||||
|
if (this.type === 'automatic' && (data.filter === null || data.filter === '')) {
|
||||||
|
throw new ValidationError({
|
||||||
|
message: tpl(messages.invalidFilterProvided.message),
|
||||||
|
context: tpl(messages.invalidFilterProvided.context)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.title !== undefined) {
|
||||||
|
this.title = data.title;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.slug !== undefined) {
|
||||||
|
this.slug = data.slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.description !== undefined) {
|
||||||
|
this.description = data.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.filter !== undefined) {
|
||||||
|
this.filter = data.filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.featureImage !== undefined) {
|
||||||
|
this.featureImage = data.featureImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param post {{id: string}} - The post to add to the collection
|
* @param post {{id: string}} - The post to add to the collection
|
||||||
* @param index {number} - The index to insert the post at, use negative numbers to count from the end.
|
* @param index {number} - The index to insert the post at, use negative numbers to count from the end.
|
||||||
|
|
|
@ -251,6 +251,9 @@ export class CollectionsService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const collectionData = this.fromDTO(data);
|
||||||
|
await collection.edit(collectionData);
|
||||||
|
|
||||||
if (collection.type === 'manual' && data.posts) {
|
if (collection.type === 'manual' && data.posts) {
|
||||||
for (const post of data.posts) {
|
for (const post of data.posts) {
|
||||||
collection.addPost(post);
|
collection.addPost(post);
|
||||||
|
@ -261,10 +264,6 @@ export class CollectionsService {
|
||||||
await this.updateAutomaticCollectionItems(collection, data.filter);
|
await this.updateAutomaticCollectionItems(collection, data.filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
const collectionData = this.fromDTO(data);
|
|
||||||
|
|
||||||
Object.assign(collection, collectionData);
|
|
||||||
|
|
||||||
await this.collectionsRepository.save(collection);
|
await this.collectionsRepository.save(collection);
|
||||||
|
|
||||||
return this.toDTO(collection);
|
return this.toDTO(collection);
|
||||||
|
|
|
@ -122,6 +122,45 @@ describe('Collection', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('edit', function () {
|
||||||
|
it('Can edit Collection values', async function () {
|
||||||
|
const collection = await Collection.create({
|
||||||
|
slug: 'test-collection',
|
||||||
|
title: 'Testing edits',
|
||||||
|
type: 'automatic',
|
||||||
|
filter: 'featured:true'
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(collection.title, 'Testing edits');
|
||||||
|
|
||||||
|
collection.edit({
|
||||||
|
title: 'Edited title',
|
||||||
|
slug: 'edited-slug'
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(collection.title, 'Edited title');
|
||||||
|
assert.equal(collection.slug, 'edited-slug');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Throws when the collection filter is empty', async function () {
|
||||||
|
const collection = await Collection.create({
|
||||||
|
title: 'Testing edits',
|
||||||
|
type: 'automatic',
|
||||||
|
filter: 'featured:true'
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.rejects(async () => {
|
||||||
|
await collection.edit({
|
||||||
|
filter: null
|
||||||
|
});
|
||||||
|
}, (err: any) => {
|
||||||
|
assert.equal(err.message, 'Invalid filter provided for automatic Collection', 'Error message should match');
|
||||||
|
assert.equal(err.context, 'Automatic type of collection should always have a filter value', 'Error message should match');
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Can add posts to different positions', async function () {
|
it('Can add posts to different positions', async function () {
|
||||||
const collection = await Collection.create({
|
const collection = await Collection.create({
|
||||||
title: 'Testing adding posts'
|
title: 'Testing adding posts'
|
||||||
|
|
Loading…
Add table
Reference in a new issue