Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
125 lines
3.5 KiB
TypeScript
125 lines
3.5 KiB
TypeScript
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<ServiceResult<BlogArticle>> => {
|
|
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<ServiceResult<BlogArticle>> => {
|
|
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<BlogArticlePayload>,
|
|
options?: ServiceCallOptions
|
|
): Promise<ServiceResult<BlogArticle>> => {
|
|
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<ServiceResult<{ success: boolean }>> => {
|
|
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)
|
|
}
|
|
}
|