%PDF- %PDF-
| Direktori : /home2/rezakur1/public_html/portfolio/portfolio-rezakurnia/src/services/ |
| Current File : //home2/rezakur1/public_html/portfolio/portfolio-rezakurnia/src/services/articles.service.ts |
import { Debug } from "astro:components";
import { apiGet } from "../lib/api/client";
import { ENDPOINTS } from "../lib/api/endpoints";
import type {
Article,
ArticleApi,
} from "../lib/types/articles.type";
/*
|--------------------------------------------------------------------------
| MAPPER (biar gak duplikat logic)
|--------------------------------------------------------------------------
*/
function mapArticle(item: ArticleApi): Article {
return {
slug: item.slug,
title: item.title,
excerpt: item.excerpt,
content: item.content ?? [],
body: item.body ?? [],
lens5w1h: item.lens5w1h ?? {},
tags: item.tags ?? [],
date: item.date,
readTime: item.readTime,
category: item.category,
coverImage: item.coverImage,
viewCount: item.viewCount,
meta_title: item.meta_title,
meta_description: item.meta_description,
};
}
/*
|--------------------------------------------------------------------------
| GET ALL (FLAT)
|--------------------------------------------------------------------------
*/
export async function getArticles(): Promise<Article[]> {
try {
const response = await apiGet<ArticleApi[]>(ENDPOINTS.ARTICLES);
console.log(response.data);
return (response.data ?? []).map(mapArticle);
} catch (error) {
console.error("getArticles error:", error);
return [];
}
}
/*
|--------------------------------------------------------------------------
| GET DETAIL BY SLUG
|--------------------------------------------------------------------------
*/
export async function getArticleBySlug(
slug: string
): Promise<Article | null> {
try {
const response = await apiGet<ArticleApi>(
`${ENDPOINTS.ARTICLES}/${slug}`
);
const item = response.data;
console.log(item);
if (!item) return null;
return mapArticle(item);
} catch (error) {
console.error("getArticleBySlug error:", error);
return null;
}
}
export type ArticleStats = {
totalArticles: number;
totalCategories: number;
lastUpdate: string | null;
};
export async function getArticleStats(): Promise<ArticleStats> {
try {
const response = await apiGet<ArticleStats>(`${ENDPOINTS.ARTICLES}/stats`);
return response.data ?? {
totalArticles: 0,
totalCategories: 0,
lastUpdate: null,
};
} catch (error) {
console.error("getArticleStats error:", error);
return {
totalArticles: 0,
totalCategories: 0,
lastUpdate: null,
};
}
}
export type ArticleCategory = {
id: number;
name: string;
description?: string;
metaTitle?: string;
metaDescription?: string;
totalArticles: number;
};
export async function getCategories(): Promise<ArticleCategory[]> {
try {
const response = await apiGet<ArticleCategory[]>(`${ENDPOINTS.ARTICLES}/categories`);
return (response.data ?? []).map((item) => ({
id: item.id,
name: item.name,
description: item.description,
metaTitle: item.metaTitle,
metaDescription: item.metaDescription,
totalArticles: item.totalArticles ?? 0,
}));
} catch (error) {
console.error("getCategories error:", error);
return [];
}
}