feat: migrate admin content and moderation modules
This commit is contained in:
@@ -3,7 +3,22 @@ import type {
|
||||
AdminDashboardResponse,
|
||||
AdminSessionResponse,
|
||||
AdminSiteSettingsResponse,
|
||||
CommentListQuery,
|
||||
CommentRecord,
|
||||
CreatePostPayload,
|
||||
CreateReviewPayload,
|
||||
FriendLinkListQuery,
|
||||
FriendLinkPayload,
|
||||
FriendLinkRecord,
|
||||
MarkdownDeleteResponse,
|
||||
MarkdownDocumentResponse,
|
||||
PostListQuery,
|
||||
PostRecord,
|
||||
ReviewRecord,
|
||||
SiteSettingsPayload,
|
||||
UpdateCommentPayload,
|
||||
UpdatePostPayload,
|
||||
UpdateReviewPayload,
|
||||
} from '@/lib/types'
|
||||
|
||||
const API_BASE = import.meta.env.VITE_API_BASE?.trim() || ''
|
||||
@@ -33,6 +48,30 @@ async function readErrorMessage(response: Response) {
|
||||
}
|
||||
}
|
||||
|
||||
function appendQueryParams(path: string, params?: Record<string, unknown>) {
|
||||
if (!params) {
|
||||
return path
|
||||
}
|
||||
|
||||
const searchParams = new URLSearchParams()
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return
|
||||
}
|
||||
|
||||
if (typeof value === 'boolean') {
|
||||
searchParams.set(key, String(value))
|
||||
return
|
||||
}
|
||||
|
||||
searchParams.set(key, String(value))
|
||||
})
|
||||
|
||||
const queryString = searchParams.toString()
|
||||
return queryString ? `${path}?${queryString}` : path
|
||||
}
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const headers = new Headers(init?.headers)
|
||||
|
||||
@@ -84,4 +123,127 @@ export const adminApi = {
|
||||
request<AdminAiReindexResponse>('/api/admin/ai/reindex', {
|
||||
method: 'POST',
|
||||
}),
|
||||
listPosts: (query?: PostListQuery) =>
|
||||
request<PostRecord[]>(
|
||||
appendQueryParams('/api/posts', {
|
||||
slug: query?.slug,
|
||||
category: query?.category,
|
||||
tag: query?.tag,
|
||||
search: query?.search,
|
||||
type: query?.postType,
|
||||
pinned: query?.pinned,
|
||||
}),
|
||||
),
|
||||
getPostBySlug: (slug: string) => request<PostRecord>(`/api/posts/slug/${encodeURIComponent(slug)}`),
|
||||
createPost: (payload: CreatePostPayload) =>
|
||||
request<MarkdownDocumentResponse>('/api/posts/markdown', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
title: payload.title,
|
||||
slug: payload.slug,
|
||||
description: payload.description,
|
||||
content: payload.content,
|
||||
category: payload.category,
|
||||
tags: payload.tags,
|
||||
post_type: payload.postType,
|
||||
image: payload.image,
|
||||
pinned: payload.pinned,
|
||||
published: payload.published,
|
||||
}),
|
||||
}),
|
||||
updatePost: (id: number, payload: UpdatePostPayload) =>
|
||||
request<PostRecord>(`/api/posts/${id}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({
|
||||
title: payload.title,
|
||||
slug: payload.slug,
|
||||
description: payload.description,
|
||||
content: payload.content,
|
||||
category: payload.category,
|
||||
tags: payload.tags,
|
||||
post_type: payload.postType,
|
||||
image: payload.image,
|
||||
pinned: payload.pinned,
|
||||
}),
|
||||
}),
|
||||
getPostMarkdown: (slug: string) =>
|
||||
request<MarkdownDocumentResponse>(`/api/posts/slug/${encodeURIComponent(slug)}/markdown`),
|
||||
updatePostMarkdown: (slug: string, markdown: string) =>
|
||||
request<MarkdownDocumentResponse>(`/api/posts/slug/${encodeURIComponent(slug)}/markdown`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({ markdown }),
|
||||
}),
|
||||
deletePost: (slug: string) =>
|
||||
request<MarkdownDeleteResponse>(`/api/posts/slug/${encodeURIComponent(slug)}/markdown`, {
|
||||
method: 'DELETE',
|
||||
}),
|
||||
listComments: (query?: CommentListQuery) =>
|
||||
request<CommentRecord[]>(
|
||||
appendQueryParams('/api/comments', {
|
||||
post_id: query?.postId,
|
||||
post_slug: query?.postSlug,
|
||||
scope: query?.scope,
|
||||
paragraph_key: query?.paragraphKey,
|
||||
approved: query?.approved,
|
||||
}),
|
||||
),
|
||||
updateComment: (id: number, payload: UpdateCommentPayload) =>
|
||||
request<CommentRecord>(`/api/comments/${id}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
deleteComment: (id: number) =>
|
||||
request<void>(`/api/comments/${id}`, {
|
||||
method: 'DELETE',
|
||||
}),
|
||||
listFriendLinks: (query?: FriendLinkListQuery) =>
|
||||
request<FriendLinkRecord[]>(
|
||||
appendQueryParams('/api/friend_links', {
|
||||
status: query?.status,
|
||||
category: query?.category,
|
||||
}),
|
||||
),
|
||||
createFriendLink: (payload: FriendLinkPayload) =>
|
||||
request<FriendLinkRecord>('/api/friend_links', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
siteName: payload.siteName,
|
||||
siteUrl: payload.siteUrl,
|
||||
avatarUrl: payload.avatarUrl,
|
||||
description: payload.description,
|
||||
category: payload.category,
|
||||
status: payload.status,
|
||||
}),
|
||||
}),
|
||||
updateFriendLink: (id: number, payload: FriendLinkPayload) =>
|
||||
request<FriendLinkRecord>(`/api/friend_links/${id}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({
|
||||
site_name: payload.siteName,
|
||||
site_url: payload.siteUrl,
|
||||
avatar_url: payload.avatarUrl,
|
||||
description: payload.description,
|
||||
category: payload.category,
|
||||
status: payload.status,
|
||||
}),
|
||||
}),
|
||||
deleteFriendLink: (id: number) =>
|
||||
request<void>(`/api/friend_links/${id}`, {
|
||||
method: 'DELETE',
|
||||
}),
|
||||
listReviews: () => request<ReviewRecord[]>('/api/reviews'),
|
||||
createReview: (payload: CreateReviewPayload) =>
|
||||
request<ReviewRecord>('/api/reviews', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
updateReview: (id: number, payload: UpdateReviewPayload) =>
|
||||
request<ReviewRecord>(`/api/reviews/${id}`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
deleteReview: (id: number) =>
|
||||
request<void>(`/api/reviews/${id}`, {
|
||||
method: 'DELETE',
|
||||
}),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user