import type { BlogArticleResponseDto, CreateBlogArticleDto } from '@/api/generated/models' import { texts } from '@/texts' import { createServiceError, errorResult, handleServiceError, type ServiceCallOptions, type ServiceResult, shouldBubbleErrorToParent, successResult, } from '@/services/errorHandler' import { unwrapApiData } from '@/services/apiResponse' import { getAdminBlogArticles } from '@/api/generated/admin-blog-articles/admin-blog-articles' export type BlogArticle = BlogArticleResponseDto export type BlogArticlePayload = CreateBlogArticleDto const adminBlogArticlesApi = getAdminBlogArticles() export const GET_ARTICLE = async (id: number, options?: ServiceCallOptions): Promise> => { try { const res = await adminBlogArticlesApi.adminBlogArticlesControllerFindOne(id) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.blog.errors.fetchFailed) } const article = unwrapApiData(body) if (!article?.id) { throw createServiceError(texts.common.invalidServerResponse) } return successResult(article) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const CREATE_ARTICLE = async (payload: BlogArticlePayload, options?: ServiceCallOptions): Promise> => { try { const res = await adminBlogArticlesApi.adminBlogArticlesControllerCreate(payload) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.blog.errors.createFailed) } const article = unwrapApiData(body) if (!article?.id) { throw createServiceError(texts.common.invalidServerResponse) } return successResult(article) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const UPDATE_ARTICLE = async ( id: number, payload: Partial, options?: ServiceCallOptions ): Promise> => { try { const res = await adminBlogArticlesApi.adminBlogArticlesControllerUpdate(id, payload) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.blog.errors.updateFailed) } const article = unwrapApiData(body) if (!article?.id) { throw createServiceError(texts.common.invalidServerResponse) } return successResult(article) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const DELETE_ARTICLE = async (id: number, options?: ServiceCallOptions): Promise> => { try { const res = await adminBlogArticlesApi.adminBlogArticlesControllerRemove(id) const body = res.data if (!body.success) { throw createServiceError(body.message || texts.blog.errors.deleteFailed) } return successResult(unwrapApiData(body) ?? { success: true }) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } }