From f6022eadba3c82ea7f6ad22d9ac6d348fa914ff9 Mon Sep 17 00:00:00 2001 From: cp-sumi-k Date: Wed, 14 Feb 2024 14:56:23 +0530 Subject: [PATCH] Optimize api response --- .gitignore | 1 + .../src/api/cta/content-types/cta/schema.json | 4 +- .../api/post/content-types/post/lifecycles.js | 9 ++ .../api/post/content-types/post/schema.json | 7 +- admin/src/api/post/controllers/post.js | 83 +++++++++++-------- 5 files changed, 66 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 9061e7fa..705f5e08 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ node_modules .strapi .cache build +admin/types #images public/uploads diff --git a/admin/src/api/cta/content-types/cta/schema.json b/admin/src/api/cta/content-types/cta/schema.json index 784c44f9..a1696316 100644 --- a/admin/src/api/cta/content-types/cta/schema.json +++ b/admin/src/api/cta/content-types/cta/schema.json @@ -25,7 +25,9 @@ "type": "media", "multiple": false, "required": false, - "allowedTypes": ["images"] + "allowedTypes": [ + "images" + ] }, "posts": { "type": "relation", diff --git a/admin/src/api/post/content-types/post/lifecycles.js b/admin/src/api/post/content-types/post/lifecycles.js index c52602d5..48da3fb9 100644 --- a/admin/src/api/post/content-types/post/lifecycles.js +++ b/admin/src/api/post/content-types/post/lifecycles.js @@ -138,6 +138,7 @@ async function modifyContentAndSetErrorMsg(event) { await generateTOC(result, event); await generateNewToc(result, event); await generatePreview(event); + event.params.data.reading_time = getReadingTime(event.params.data.content); } } @@ -553,3 +554,11 @@ const getImg = async (page, uri) => { }); return img; }; + +function getReadingTime(content) { + if (!content) return 0; + const numberOfWords = content + .replace(/<\/?[^>]+(>|$)/g, "") + .split(/\s/g).length; + return Math.ceil(numberOfWords / 265); +} diff --git a/admin/src/api/post/content-types/post/schema.json b/admin/src/api/post/content-types/post/schema.json index 5a833a35..f7ef9780 100644 --- a/admin/src/api/post/content-types/post/schema.json +++ b/admin/src/api/post/content-types/post/schema.json @@ -60,7 +60,9 @@ "type": "media", "multiple": false, "required": false, - "allowedTypes": ["images"] + "allowedTypes": [ + "images" + ] }, "summary": { "type": "text", @@ -104,6 +106,9 @@ "type": "boolean", "default": true, "required": true + }, + "reading_time": { + "type": "integer" } } } diff --git a/admin/src/api/post/controllers/post.js b/admin/src/api/post/controllers/post.js index 951a5e67..268308e3 100644 --- a/admin/src/api/post/controllers/post.js +++ b/admin/src/api/post/controllers/post.js @@ -30,48 +30,56 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({ }, }); - if (!entity.is_resource) { + if (entity) { entity.recommandedPosts = []; - } else { - const recommandedPosts = await strapi.db - .query("api::post.post") - .findMany({ - where: { - $and: [ - { - slug: { $ne: entity.slug }, - }, - { - is_resource: entity.is_resource, - }, - ], - }, - sort: { published_on: "desc" }, - populate: { - author: { - populate: { - image: true, + if (entity.is_resource) { + const recommandedPosts = await strapi.db + .query("api::post.post") + .findMany({ + where: { + $and: [ + { + slug: { $ne: entity.slug }, + }, + { + is_resource: entity.is_resource, + }, + ], + }, + sort: { published_on: "desc" }, + populate: { + author: { + populate: { + image: true, + }, }, + image: true, }, - image: true, - }, - }); + }); - entity.recommandedPosts = recommandedPosts - .filter((post) => { - return entity.tags - .map((t) => t.name) - .some((r) => post.tags.map((t) => t.name).includes(r)); - }) - .slice(0, 3); + entity.recommandedPosts = recommandedPosts + .filter((post) => { + return entity.tags + .map((t) => t.name) + .some((r) => post.tags.map((t) => t.name).includes(r)); + }) + .slice(0, 3); + } } return this.transformResponse(entity); }, async find(ctx) { - let posts = await strapi.entityService.findMany("api::post.post", { + const count = await strapi + .query("api::post.post") + .count({ where: { is_resource: ctx.query.filters.is_resource } }); + + const posts = await strapi.entityService.findMany("api::post.post", { filters: ctx.query.filters, + fields: ctx.query.fields, + start: ctx.query.pagination.start, + limit: ctx.query.pagination.limit, publicationState: ctx.query.publicationState, sort: { published_on: "desc" }, populate: { @@ -84,12 +92,17 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({ }, }); - return this.transformResponse(posts); + return this.transformResponse({ + posts: posts, + count: count, + }); }, + async getBlogByTagName(ctx) { const { tag } = ctx.params; - - let posts = await strapi.db.query("api::post.post").findMany({ + let posts = await strapi.entityService.findMany("api::post.post", { + fields: ctx.query.fields, + publicationState: ctx.query.publicationState, sort: { published_on: "desc" }, populate: { author: { @@ -97,10 +110,8 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({ image: true, }, }, - tags: true, image: true, }, - publicationState: "live", }); posts = posts.filter((post) => {