import { IPodcasts } from './podcasts.types'; import { directus } from '@/plugins/axios/'; import { IParamsDirectus } from '../baseTypes'; import { POST_STATUS } from '@/utils/constants'; const postPreviewFields = 'title,description,imageFile,category.*,published_at,slug,imagePreview,image,readTime'; async function getPost({ slug, categorySlug, }: IPodcasts.GetPostsParams): Promise { const params: IParamsDirectus = { fields: '*.*.*.*', filter: { status: POST_STATUS.published, }, }; if (slug) params.filter = { ...params.filter, slug, }; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get('podcasts', { params, }); const [post] = posts.data; return post; } async function getPostsPreview({ start, categorySlug, limit, }: IPodcasts.GetPostsParams): Promise { const params: IParamsDirectus = { sort: '-date', fields: postPreviewFields, filter: { status: POST_STATUS.published, }, }; if (start) params.offset = start; if (limit) params.limit = limit; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: posts } = await directus.get( 'podcasts', { params, }, ); return posts.data; } async function getCategories(): Promise { const params: IParamsDirectus = { fields: '*.*', }; const { data: categories } = await directus.get( 'podcast-categories', { params }, ); return categories.data; } async function getPostsCount(categorySlug?: string): Promise { const params: IParamsDirectus = { meta: '*', limit: 0, filter: { status: POST_STATUS.published, }, }; if (categorySlug) params.filter = { ...params.filter, ...{ category: { slug: categorySlug, }, }, }; const { data: postsCount } = await directus.get('podcasts', { params, }); return Number(postsCount?.meta?.filter_count); } export const PodcastsGateway = { getPost, getPostsPreview, getPostsCount, getCategories, };