Skip to content

Commit

Permalink
Optimize api response
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Feb 14, 2024
1 parent 9a22baf commit f6022ea
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ node_modules
.strapi
.cache
build
admin/types

#images
public/uploads
4 changes: 3 additions & 1 deletion admin/src/api/cta/content-types/cta/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": ["images"]
"allowedTypes": [
"images"
]
},
"posts": {
"type": "relation",
Expand Down
9 changes: 9 additions & 0 deletions admin/src/api/post/content-types/post/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
7 changes: 6 additions & 1 deletion admin/src/api/post/content-types/post/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": ["images"]
"allowedTypes": [
"images"
]
},
"summary": {
"type": "text",
Expand Down Expand Up @@ -104,6 +106,9 @@
"type": "boolean",
"default": true,
"required": true
},
"reading_time": {
"type": "integer"
}
}
}
83 changes: 47 additions & 36 deletions admin/src/api/post/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -84,23 +92,26 @@ 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: {
populate: {
image: true,
},
},
tags: true,
image: true,
},
publicationState: "live",
});

posts = posts.filter((post) => {
Expand Down

0 comments on commit f6022ea

Please sign in to comment.