same endpoints for all list types

This commit is contained in:
Julian Krauser 2025-01-14 09:12:11 +01:00
parent 8c70fa1c50
commit bbbd7d2975
5 changed files with 82 additions and 5 deletions

View file

@ -2,6 +2,27 @@
* vehicle controller * vehicle controller
*/ */
import { factories } from '@strapi/strapi' import { factories } from "@strapi/strapi";
export default factories.createCoreController('api::vehicle.vehicle'); export default factories.createCoreController("api::vehicle.vehicle", ({ strapi }) => ({
async distinctYears(ctx) {
try {
const years = await strapi.service("api::vehicle.vehicle").getDistinctYears();
ctx.body = years;
} catch (err) {
ctx.throw(500, err);
}
},
async findByYear(ctx) {
try {
const { year } = ctx.params;
const { page = "1", pageSize = "10" } = ctx.query as { page?: string; pageSize?: string };
const result = await strapi.service("api::vehicle.vehicle").findByYear(year, parseInt(page), parseInt(pageSize));
ctx.body = result;
} catch (err) {
ctx.throw(500, err);
}
},
}));

View file

@ -0,0 +1,14 @@
export default {
routes: [
{
method: "GET",
path: "/custom/vehicle/distinct-years",
handler: "api::vehicle.vehicle.distinctYears",
},
{
method: "GET",
path: "/custom/vehicle/year/:year",
handler: "api::vehicle.vehicle.findByYear",
},
],
};

View file

@ -2,6 +2,47 @@
* vehicle service * vehicle service
*/ */
import { factories } from '@strapi/strapi'; import { factories } from "@strapi/strapi";
import { parseISO, getYear } from "date-fns";
export default factories.createCoreService('api::vehicle.vehicle'); export default factories.createCoreService("api::vehicle.vehicle", ({ strapi }) => ({
async getDistinctYears() {
const articles = await strapi.query("api::vehicle.vehicle").findMany({
select: ["date"],
});
const years = articles
.map((article) => getYear(parseISO(article.date + "")))
.filter((year, index, self) => self.indexOf(year) === index)
.sort((a, b) => b - a);
return years;
},
async findByYear(year: string, page = 1, pageSize = 10) {
const startOfYear = new Date(`${year}-01-01T00:00:00.000Z`);
const endOfYear = new Date(`${year}-12-31T23:59:59.999Z`);
const articles = await strapi.query("api::vehicle.vehicle").findPage({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
publishedAt: {
$ne: null,
},
},
limit: pageSize,
page: page,
orderBy: { date: "desc" },
});
return {
data: articles.results,
meta: {
pagination: articles.pagination,
},
};
},
}));

View file

@ -14,7 +14,7 @@
"name": "Apache 2.0", "name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html" "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}, },
"x-generation-date": "2025-01-05T16:42:52.145Z" "x-generation-date": "2025-01-14T08:03:38.856Z"
}, },
"x-strapi-config": { "x-strapi-config": {
"plugins": [ "plugins": [

View file

@ -707,6 +707,7 @@ export interface PluginReviewWorkflowsWorkflow extends Struct.CollectionTypeSche
Schema.Attribute.Private; Schema.Attribute.Private;
name: Schema.Attribute.String & Schema.Attribute.Required & Schema.Attribute.Unique; name: Schema.Attribute.String & Schema.Attribute.Required & Schema.Attribute.Unique;
publishedAt: Schema.Attribute.DateTime; publishedAt: Schema.Attribute.DateTime;
stageRequiredToPublish: Schema.Attribute.Relation<"oneToOne", "plugin::review-workflows.workflow-stage">;
stages: Schema.Attribute.Relation<"oneToMany", "plugin::review-workflows.workflow-stage">; stages: Schema.Attribute.Relation<"oneToMany", "plugin::review-workflows.workflow-stage">;
updatedAt: Schema.Attribute.DateTime; updatedAt: Schema.Attribute.DateTime;
updatedBy: Schema.Attribute.Relation<"oneToOne", "admin::user"> & Schema.Attribute.Private; updatedBy: Schema.Attribute.Relation<"oneToOne", "admin::user"> & Schema.Attribute.Private;