extend collection requests & hard-coded-public routes

This commit is contained in:
Julian Krauser 2024-11-04 11:46:28 +01:00
parent bd52f5c996
commit 28afdfd83e
21 changed files with 387 additions and 23 deletions

View file

@ -4,4 +4,25 @@
import { factories } from '@strapi/strapi' import { factories } from '@strapi/strapi'
export default factories.createCoreController('api::article.article'); export default factories.createCoreController('api::article.article', ({ strapi }) => ({
async distinctYears(ctx) {
try {
const years = await strapi.service('api::article.article').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::article.article').findByYear(year, parseInt(page), parseInt(pageSize));
ctx.body = result;
} catch (err) {
ctx.throw(500, err);
}
},
}));

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::article.article'); export default factories.createCoreRouter('api::article.article', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
},
});

View file

@ -0,0 +1,20 @@
export default {
routes: [
{
method: 'GET',
path: '/custom/articles/distinct-years',
handler: 'api::article.article.distinctYears',
config: {
auth: false,
},
},
{
method: 'GET',
path: '/custom/articles/year/:year',
handler: 'api::article.article.findByYear',
config: {
auth: false,
},
},
]
};

View file

@ -3,5 +3,57 @@
*/ */
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
import { parseISO, getYear } from 'date-fns';
export default factories.createCoreService('api::article.article'); export default factories.createCoreService('api::article.article', ({strapi}) => ({
async getDistinctYears() {
const articles = await strapi.query("api::article.article").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::article.article").findMany({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
limit: pageSize,
offset: (page - 1) * pageSize,
orderBy: { date: 'desc' },
});
const totalArticles = await strapi.query("api::article.article").count({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
});
return {
data: articles,
meta: {
pagination: {
page,
pageSize,
pageCount: Math.ceil(totalArticles / pageSize),
total: totalArticles,
},
},
};
},
}));

View file

@ -20,7 +20,12 @@
"type": "uid", "type": "uid",
"required": true "required": true
}, },
"imageItem": { "image_item": {
"type": "boolean",
"default": false,
"required": true
},
"date_list": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"required": true "required": true

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::collection-lookup.collection-lookup'); export default factories.createCoreRouter('api::collection-lookup.collection-lookup', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
}
});

View file

@ -4,4 +4,25 @@
import { factories } from '@strapi/strapi' import { factories } from '@strapi/strapi'
export default factories.createCoreController('api::event.event'); export default factories.createCoreController('api::event.event', ({ strapi }) => ({
async distinctYears(ctx) {
try {
const years = await strapi.service('api::event.event').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::event.event').findByYear(year, parseInt(page), parseInt(pageSize));
ctx.body = result;
} catch (err) {
ctx.throw(500, err);
}
},
}));

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::event.event'); export default factories.createCoreRouter('api::event.event', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
}
});

View file

@ -0,0 +1,20 @@
export default {
routes: [
{
method: 'GET',
path: '/custom/events/distinct-years',
handler: 'api::event.event.distinctYears',
config: {
auth: false,
},
},
{
method: 'GET',
path: '/custom/events/year/:year',
handler: 'api::event.event.findByYear',
config: {
auth: false,
},
},
]
};

View file

@ -3,5 +3,57 @@
*/ */
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
import { parseISO, getYear } from 'date-fns';
export default factories.createCoreService('api::event.event'); export default factories.createCoreService('api::event.event', ({strapi}) => ({
async getDistinctYears() {
const articles = await strapi.query("api::event.event").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::event.event").findMany({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
limit: pageSize,
offset: (page - 1) * pageSize,
orderBy: { date: 'desc' },
});
const totalArticles = await strapi.query("api::event.event").count({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
});
return {
data: articles,
meta: {
pagination: {
page,
pageSize,
pageCount: Math.ceil(totalArticles / pageSize),
total: totalArticles,
},
},
};
},
}));

View file

@ -4,4 +4,10 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::global.global'); export default factories.createCoreRouter('api::global.global', {
config: {
find: {
auth: false
},
}
});

View file

@ -4,4 +4,10 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::homepage.homepage'); export default factories.createCoreRouter('api::homepage.homepage', {
config: {
find: {
auth: false
},
}
});

View file

@ -4,4 +4,25 @@
import { factories } from '@strapi/strapi' import { factories } from '@strapi/strapi'
export default factories.createCoreController('api::operation.operation'); export default factories.createCoreController('api::operation.operation', ({ strapi }) => ({
async distinctYears(ctx) {
try {
const years = await strapi.service('api::operation.operation').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::operation.operation').findByYear(year, parseInt(page), parseInt(pageSize));
ctx.body = result;
} catch (err) {
ctx.throw(500, err);
}
},
}));

View file

@ -0,0 +1,20 @@
export default {
routes: [
{
method: 'GET',
path: '/custom/operations/distinct-years',
handler: 'api::operation.operation.distinctYears',
config: {
auth: false,
},
},
{
method: 'GET',
path: '/custom/operations/year/:year',
handler: 'api::operation.operation.findByYear',
config: {
auth: false,
},
},
]
};

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::operation.operation'); export default factories.createCoreRouter('api::operation.operation', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
}
});

View file

@ -3,5 +3,57 @@
*/ */
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
import { parseISO, getYear } from 'date-fns';
export default factories.createCoreService('api::operation.operation'); export default factories.createCoreService('api::operation.operation', ({strapi}) => ({
async getDistinctYears() {
const articles = await strapi.query("api::operation.operation").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::operation.operation").findMany({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
limit: pageSize,
offset: (page - 1) * pageSize,
orderBy: { date: 'desc' },
});
const totalArticles = await strapi.query("api::operation.operation").count({
filters: {
date: {
$gte: startOfYear,
$lte: endOfYear,
},
},
});
return {
data: articles,
meta: {
pagination: {
page,
pageSize,
pageCount: Math.ceil(totalArticles / pageSize),
total: totalArticles,
},
},
};
},
}));

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::page.page'); export default factories.createCoreRouter('api::page.page', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
}
});

View file

@ -4,4 +4,13 @@
import { factories } from '@strapi/strapi'; import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::vehicle.vehicle'); export default factories.createCoreRouter('api::vehicle.vehicle', {
config: {
find: {
auth: false
},
findOne: {
auth: false
},
}
});

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": "2024-11-03T14:29:07.198Z" "x-generation-date": "2024-11-04T10:41:27.532Z"
}, },
"x-strapi-config": { "x-strapi-config": {
"plugins": [ "plugins": [
@ -6443,7 +6443,8 @@
"required": [ "required": [
"reference", "reference",
"collection", "collection",
"imageItem" "image_item",
"date_list"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
@ -6453,7 +6454,10 @@
"collection": { "collection": {
"type": "string" "type": "string"
}, },
"imageItem": { "image_item": {
"type": "boolean"
},
"date_list": {
"type": "boolean" "type": "boolean"
}, },
"locale": { "locale": {
@ -6517,7 +6521,8 @@
"required": [ "required": [
"reference", "reference",
"collection", "collection",
"imageItem" "image_item",
"date_list"
], ],
"properties": { "properties": {
"id": { "id": {
@ -6532,7 +6537,10 @@
"collection": { "collection": {
"type": "string" "type": "string"
}, },
"imageItem": { "image_item": {
"type": "boolean"
},
"date_list": {
"type": "boolean" "type": "boolean"
}, },
"createdAt": { "createdAt": {
@ -6841,7 +6849,10 @@
"collection": { "collection": {
"type": "string" "type": "string"
}, },
"imageItem": { "image_item": {
"type": "boolean"
},
"date_list": {
"type": "boolean" "type": "boolean"
}, },
"createdAt": { "createdAt": {

File diff suppressed because one or more lines are too long

View file

@ -421,7 +421,10 @@ export interface ApiCollectionLookupCollectionLookup
createdAt: Schema.Attribute.DateTime; createdAt: Schema.Attribute.DateTime;
createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
Schema.Attribute.Private; Schema.Attribute.Private;
imageItem: Schema.Attribute.Boolean & date_list: Schema.Attribute.Boolean &
Schema.Attribute.Required &
Schema.Attribute.DefaultTo<false>;
image_item: Schema.Attribute.Boolean &
Schema.Attribute.Required & Schema.Attribute.Required &
Schema.Attribute.DefaultTo<false>; Schema.Attribute.DefaultTo<false>;
locale: Schema.Attribute.String & Schema.Attribute.Private; locale: Schema.Attribute.String & Schema.Attribute.Private;