From 3a624ec3a19b568f7f6808ff07285c689176a9c0 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Thu, 21 Sep 2023 14:10:51 +0700 Subject: [PATCH] Fixed return values of CollectionService Our service layer should not expose the Entities, it should return the DTOs. This is to allow us to make internal refactors without having to modify the entire stack. --- ghost/collections/src/CollectionsService.ts | 18 +++++++++++++----- ghost/posts-service/lib/PostsService.js | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ghost/collections/src/CollectionsService.ts b/ghost/collections/src/CollectionsService.ts index 2889b9cf1a..1242356d33 100644 --- a/ghost/collections/src/CollectionsService.ts +++ b/ghost/collections/src/CollectionsService.ts @@ -560,12 +560,20 @@ export class CollectionsService { }); } - async getById(id: string, options?: {transaction: Knex.Transaction}): Promise { - return await this.collectionsRepository.getById(id, options); + async getById(id: string, options?: {transaction: Knex.Transaction}): Promise { + const collection = await this.collectionsRepository.getById(id, options); + if (!collection) { + return null; + } + return this.toDTO(collection); } - async getBySlug(slug: string, options?: {transaction: Knex.Transaction}): Promise { - return await this.collectionsRepository.getBySlug(slug, options); + async getBySlug(slug: string, options?: {transaction: Knex.Transaction}): Promise { + const collection = await this.collectionsRepository.getBySlug(slug, options); + if (!collection) { + return null; + } + return this.toDTO(collection); } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -605,7 +613,7 @@ export class CollectionsService { } async destroy(id: string): Promise { - const collection = await this.getById(id); + const collection = await this.collectionsRepository.getById(id); if (collection) { if (collection.deletable === false) { diff --git a/ghost/posts-service/lib/PostsService.js b/ghost/posts-service/lib/PostsService.js index 97e5ffa841..3c758133cd 100644 --- a/ghost/posts-service/lib/PostsService.js +++ b/ghost/posts-service/lib/PostsService.js @@ -56,7 +56,7 @@ class PostsService { }); } - const postIds = collection.posts; + const postIds = collection.posts.map(post => post.id); if (postIds.length !== 0) { options.filter = `id:[${postIds.join(',')}]+type:post`;