From bbbd7d2975daf2c76ef8b9f3b94cc5012198c223 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 14 Jan 2025 09:12:11 +0100 Subject: [PATCH] same endpoints for all list types --- src/api/vehicle/controllers/vehicle.ts | 25 ++++++++++- src/api/vehicle/routes/extend-vehicle.ts | 14 ++++++ src/api/vehicle/services/vehicle.ts | 45 ++++++++++++++++++- .../1.0.0/full_documentation.json | 2 +- types/generated/contentTypes.d.ts | 1 + 5 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 src/api/vehicle/routes/extend-vehicle.ts diff --git a/src/api/vehicle/controllers/vehicle.ts b/src/api/vehicle/controllers/vehicle.ts index 270c0d4..0657dd8 100644 --- a/src/api/vehicle/controllers/vehicle.ts +++ b/src/api/vehicle/controllers/vehicle.ts @@ -2,6 +2,27 @@ * 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); + } + }, +})); diff --git a/src/api/vehicle/routes/extend-vehicle.ts b/src/api/vehicle/routes/extend-vehicle.ts new file mode 100644 index 0000000..bb0b05d --- /dev/null +++ b/src/api/vehicle/routes/extend-vehicle.ts @@ -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", + }, + ], +}; diff --git a/src/api/vehicle/services/vehicle.ts b/src/api/vehicle/services/vehicle.ts index 6b9d33c..c1175ab 100644 --- a/src/api/vehicle/services/vehicle.ts +++ b/src/api/vehicle/services/vehicle.ts @@ -2,6 +2,47 @@ * 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, + }, + }; + }, +})); diff --git a/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/src/extensions/documentation/documentation/1.0.0/full_documentation.json index ba5fdab..9789a15 100644 --- a/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "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": { "plugins": [ diff --git a/types/generated/contentTypes.d.ts b/types/generated/contentTypes.d.ts index 9be65b1..0ca5e01 100644 --- a/types/generated/contentTypes.d.ts +++ b/types/generated/contentTypes.d.ts @@ -707,6 +707,7 @@ export interface PluginReviewWorkflowsWorkflow extends Struct.CollectionTypeSche Schema.Attribute.Private; name: Schema.Attribute.String & Schema.Attribute.Required & Schema.Attribute.Unique; publishedAt: Schema.Attribute.DateTime; + stageRequiredToPublish: Schema.Attribute.Relation<"oneToOne", "plugin::review-workflows.workflow-stage">; stages: Schema.Attribute.Relation<"oneToMany", "plugin::review-workflows.workflow-stage">; updatedAt: Schema.Attribute.DateTime; updatedBy: Schema.Attribute.Relation<"oneToOne", "admin::user"> & Schema.Attribute.Private;