Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize api response #144

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 10 additions & 1 deletion 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 @@ -277,7 +278,7 @@ async function generatePreview(event) {
for (const element of embeds) {
let string = "";
if (["mp4", "webm"].some((v) => element.attributes.url.value.includes(v))) {
string = `<video width="50%" style="margin:auto" autoplay loop muted>
string = `<video style="margin:auto" autoplay loop muted>
<source src="${element.attributes.url.value}" type="video/mp4">
Your browser does not support the video tag.
</video>`;
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
Loading