work version archive
This commit is contained in:
@@ -1,102 +1,122 @@
|
||||
import { fetchGraphQL } from './graphql-client.js';
|
||||
import type { ProfileArticle } from '../types/graphql.js';
|
||||
|
||||
//кэширование
|
||||
import { cache } from '@lib/cache/manager.js';
|
||||
import { CACHE_TTL } from '@lib/cache/cache-ttl';
|
||||
|
||||
export interface AnewsPost {
|
||||
title: string;
|
||||
uri: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
//последние статьи
|
||||
export async function getLatestPosts(first = 12, after = null) {
|
||||
const query = `
|
||||
query GetLatestProfileArticles($first: Int!, $after: String) {
|
||||
profileArticles(
|
||||
first: $first,
|
||||
after: $after,
|
||||
where: {orderby: { field: DATE, order: DESC }}
|
||||
) {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
edges {
|
||||
cursor
|
||||
node {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
uri
|
||||
date
|
||||
featuredImage {
|
||||
node {
|
||||
sourceUrl(size: LARGE)
|
||||
altText
|
||||
}
|
||||
// Создаем уникальный ключ для кэша
|
||||
const cacheKey = `latest-posts:${first}:${after || 'first-page'}`;
|
||||
|
||||
return await cache.wrap(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const query = `
|
||||
query GetLatestProfileArticles($first: Int!, $after: String) {
|
||||
profileArticles(
|
||||
first: $first,
|
||||
after: $after,
|
||||
where: {orderby: { field: DATE, order: DESC }}
|
||||
) {
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
author {
|
||||
edges {
|
||||
cursor
|
||||
node {
|
||||
id
|
||||
name
|
||||
firstName
|
||||
lastName
|
||||
avatar {
|
||||
url
|
||||
databaseId
|
||||
title
|
||||
uri
|
||||
date
|
||||
featuredImage {
|
||||
node {
|
||||
sourceUrl(size: LARGE)
|
||||
altText
|
||||
}
|
||||
}
|
||||
author {
|
||||
node {
|
||||
id
|
||||
name
|
||||
firstName
|
||||
lastName
|
||||
avatar {
|
||||
url
|
||||
}
|
||||
uri
|
||||
}
|
||||
}
|
||||
categories {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
databaseId
|
||||
}
|
||||
}
|
||||
tags {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
}
|
||||
}
|
||||
uri
|
||||
}
|
||||
}
|
||||
categories {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
databaseId
|
||||
}
|
||||
}
|
||||
tags {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
`;
|
||||
|
||||
const data = await fetchGraphQL(query, { first, after });
|
||||
|
||||
// Преобразуем edges в nodes
|
||||
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
|
||||
|
||||
return {
|
||||
posts,
|
||||
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
|
||||
};
|
||||
const data = await fetchGraphQL(query, { first, after });
|
||||
|
||||
// Преобразуем edges в nodes
|
||||
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
|
||||
|
||||
return {
|
||||
posts,
|
||||
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
|
||||
};
|
||||
},
|
||||
{ ttl: CACHE_TTL.POSTS } // из конфигурации
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
//последние новости
|
||||
//последние новости (кэшированная версия)
|
||||
export async function getLatestAnews(count = 12): Promise<AnewsPost[]> {
|
||||
const query = `
|
||||
query GetAnews($count: Int!) {
|
||||
aNews(first: $count, where: {orderby: {field: DATE, order: DESC}}) {
|
||||
nodes {
|
||||
title
|
||||
uri
|
||||
date
|
||||
const cacheKey = `latest-anews:${count}`;
|
||||
|
||||
return await cache.wrap(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const query = `
|
||||
query GetAnews($count: Int!) {
|
||||
aNews(first: $count, where: {orderby: {field: DATE, order: DESC}}) {
|
||||
nodes {
|
||||
title
|
||||
uri
|
||||
date
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
`;
|
||||
|
||||
const data = await fetchGraphQL(query, { count });
|
||||
return data.aNews?.nodes || []; // Исправлено: aNews вместо anews
|
||||
const data = await fetchGraphQL(query, { count });
|
||||
return data.aNews?.nodes || [];
|
||||
},
|
||||
{ ttl: CACHE_TTL.POSTS }
|
||||
);
|
||||
}
|
||||
|
||||
// Получить ProfileArticle по databaseId
|
||||
@@ -182,16 +202,6 @@ export async function getTagBySlug(slug) {
|
||||
slug
|
||||
description
|
||||
count
|
||||
seo {
|
||||
title
|
||||
metaDesc
|
||||
canonical
|
||||
opengraphTitle
|
||||
opengraphDescription
|
||||
opengraphImage {
|
||||
sourceUrl
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
@@ -208,13 +218,17 @@ export async function getTagBySlug(slug) {
|
||||
|
||||
|
||||
/**
|
||||
* Получить посты тега с пагинацией (offset-based)
|
||||
* Получить тег с постами по slug с пагинацией
|
||||
*/
|
||||
export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
|
||||
export async function getTagWithPostsPaginated(
|
||||
tagSlug: string,
|
||||
perPage: number = 12,
|
||||
page: number = 1
|
||||
) {
|
||||
const offset = (page - 1) * perPage;
|
||||
|
||||
const query = `
|
||||
query GetTagPostsPaginated($slug: ID!, $first: Int!, $offset: Int!) {
|
||||
query GetTagWithPostsPaginated($slug: ID!, $first: Int!, $offset: Int!) {
|
||||
tag(id: $slug, idType: SLUG) {
|
||||
id
|
||||
databaseId
|
||||
@@ -226,12 +240,20 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
|
||||
title
|
||||
metaDesc
|
||||
canonical
|
||||
opengraphTitle
|
||||
opengraphDescription
|
||||
opengraphImage {
|
||||
sourceUrl
|
||||
}
|
||||
}
|
||||
posts(
|
||||
first: $first
|
||||
offset: $offset
|
||||
where: {
|
||||
orderby: { field: DATE, order: DESC }
|
||||
orderby: {
|
||||
field: DATE,
|
||||
order: DESC
|
||||
}
|
||||
}
|
||||
) {
|
||||
nodes {
|
||||
@@ -332,7 +354,7 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching tag posts:', error);
|
||||
console.error('Error fetching tag with posts:', error);
|
||||
return {
|
||||
tag: null,
|
||||
posts: [],
|
||||
@@ -345,6 +367,9 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Получить общее количество постов для всех тегов
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user