Skip to content

Commit

Permalink
Fix: posts api query pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Feb 21, 2024
1 parent 3426b18 commit 9d48f92
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions admin/src/api/post/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({
publicationState: ctx.query.publicationState,
});

const posts = await strapi.entityService.findMany("api::post.post", {
let filterObject = {
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 @@ -91,9 +89,26 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({
},
image: true,
},
});
};

if (ctx.query.pagination) {
filterObject.start = ctx.query.pagination.start || 0;
filterObject.limit = ctx.query.pagination.limit || 10;
}

const posts = await strapi.entityService.findMany(
"api::post.post",
filterObject
);

if (ctx.query.filters) {
ctx.query.filters.is_featured = true;
} else {
ctx.queryfilters = {
is_featured: true,
};
}

ctx.query.filters.is_featured = true;
const featuredPosts = await strapi.entityService.findMany(
"api::post.post",
{
Expand Down Expand Up @@ -145,11 +160,9 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({
},

async findPaginate(ctx) {
const posts = await strapi.entityService.findMany("api::post.post", {
let filterObject = {
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 @@ -160,7 +173,17 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({
},
image: true,
},
});
};

if (ctx.query.pagination) {
filterObject.start = ctx.query.pagination.start || 0;
filterObject.limit = ctx.query.pagination.limit || 10;
}

const posts = await strapi.entityService.findMany(
"api::post.post",
filterObject
);

return this.transformResponse(posts);
},
Expand Down

0 comments on commit 9d48f92

Please sign in to comment.