2025-06-19 21:42:16 +00:00
|
|
|
import apiService from '@/services/api.service'
|
2025-06-19 14:51:10 +00:00
|
|
|
|
|
|
|
|
export interface BlogPost {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
title: string
|
|
|
|
|
slug: string
|
|
|
|
|
content?: string
|
|
|
|
|
summary: string
|
|
|
|
|
coverImage?: string
|
2025-06-19 14:51:10 +00:00
|
|
|
author: {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
avatar?: string
|
|
|
|
|
}
|
2025-06-19 14:51:10 +00:00
|
|
|
category: {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
slug: string
|
|
|
|
|
}
|
|
|
|
|
tags: string[]
|
|
|
|
|
viewCount: number
|
|
|
|
|
likeCount: number
|
|
|
|
|
commentCount: number
|
|
|
|
|
isPublished: boolean
|
|
|
|
|
publishedAt?: string
|
|
|
|
|
createdAt: string
|
|
|
|
|
updatedAt: string
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface BlogCategory {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
slug: string
|
|
|
|
|
description?: string
|
|
|
|
|
postCount: number
|
2025-06-20 11:11:42 +00:00
|
|
|
isActive: boolean
|
|
|
|
|
icon: string
|
|
|
|
|
displayOrder: number
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface BlogComment {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
postId: string
|
|
|
|
|
content: string
|
2025-06-19 14:51:10 +00:00
|
|
|
author: {
|
2025-06-19 21:42:16 +00:00
|
|
|
id: string
|
|
|
|
|
name: string
|
|
|
|
|
avatar?: string
|
|
|
|
|
}
|
|
|
|
|
parentId?: string
|
|
|
|
|
replies?: BlogComment[]
|
|
|
|
|
likeCount: number
|
|
|
|
|
isLiked?: boolean
|
|
|
|
|
createdAt: string
|
|
|
|
|
updatedAt: string
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CreateUpdateBlogPostDto {
|
|
|
|
|
title: string
|
2025-06-20 11:11:42 +00:00
|
|
|
slug: string
|
2025-06-19 14:51:10 +00:00
|
|
|
content: string
|
|
|
|
|
summary: string
|
|
|
|
|
categoryId: string
|
|
|
|
|
tags: string[]
|
|
|
|
|
coverImage?: string
|
|
|
|
|
isPublished: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CreateUpdateBlogCategoryDto {
|
|
|
|
|
name: string
|
|
|
|
|
slug: string
|
|
|
|
|
description: string
|
|
|
|
|
icon?: string
|
|
|
|
|
displayOrder: number
|
|
|
|
|
isActive: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CreateCommentDto {
|
2025-06-19 21:42:16 +00:00
|
|
|
postId: string
|
|
|
|
|
content: string
|
|
|
|
|
parentId?: string
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface BlogListParams {
|
2025-06-19 21:42:16 +00:00
|
|
|
page?: number
|
|
|
|
|
pageSize?: number
|
|
|
|
|
categoryId?: string
|
|
|
|
|
tag?: string
|
|
|
|
|
search?: string
|
|
|
|
|
authorId?: string
|
|
|
|
|
sortBy?: 'latest' | 'popular' | 'trending'
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface PaginatedResponse<T> {
|
2025-06-19 21:42:16 +00:00
|
|
|
items: T[]
|
|
|
|
|
totalCount: number
|
|
|
|
|
pageNumber: number
|
|
|
|
|
pageSize: number
|
|
|
|
|
totalPages: number
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BlogService {
|
|
|
|
|
async getPosts(params: BlogListParams = {}): Promise<PaginatedResponse<BlogPost>> {
|
|
|
|
|
const response = await apiService.fetchData<PaginatedResponse<BlogPost>>({
|
|
|
|
|
url: '/api/app/blog/posts',
|
|
|
|
|
method: 'GET',
|
2025-06-19 21:42:16 +00:00
|
|
|
params,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getPostBySlug(slug: string): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
|
|
|
|
url : `/api/app/blog/posts/post-by-slug/${slug}`,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getPost(idOrSlug: string): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
|
|
|
|
url: `/api/app/blog/posts/${idOrSlug}`,
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createPost(data: CreateUpdateBlogPostDto): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: '/api/app/blog/post',
|
2025-06-19 14:51:10 +00:00
|
|
|
method: 'POST',
|
2025-06-19 21:42:16 +00:00
|
|
|
data: data as any,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updatePost(id: string, data: CreateUpdateBlogPostDto): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/post`,
|
2025-06-19 14:51:10 +00:00
|
|
|
method: 'PUT',
|
2025-06-19 21:42:16 +00:00
|
|
|
data: data as any,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deletePost(id: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/post`,
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async publishPost(id: string): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/publish-post`,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unpublishPost(id: string): Promise<BlogPost> {
|
|
|
|
|
const response = await apiService.fetchData<BlogPost>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/unpublish-post`,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getCategories(): Promise<BlogCategory[]> {
|
|
|
|
|
const response = await apiService.fetchData<BlogCategory[]>({
|
|
|
|
|
url: '/api/app/blog/categories',
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getComments(postId: string): Promise<BlogComment[]> {
|
|
|
|
|
const response = await apiService.fetchData<BlogComment[]>({
|
|
|
|
|
url: `/api/app/blog/posts/${postId}/comments`,
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createComment(data: CreateCommentDto): Promise<BlogComment> {
|
|
|
|
|
const response = await apiService.fetchData<BlogComment>({
|
|
|
|
|
url: '/api/app/blog/comments',
|
|
|
|
|
method: 'POST',
|
2025-06-19 21:42:16 +00:00
|
|
|
data: data as any,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteComment(id: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
|
|
|
|
url: `/api/app/blog/comments/${id}`,
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async likePost(postId: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${postId}/like-post`,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unlikePost(postId: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${postId}/unlike-post`,
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async likeComment(commentId: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
|
|
|
|
url: `/api/app/blog/comments/${commentId}/like`,
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unlikeComment(commentId: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
|
|
|
|
url: `/api/app/blog/comments/${commentId}/like`,
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTags(): Promise<string[]> {
|
|
|
|
|
const response = await apiService.fetchData<string[]>({
|
|
|
|
|
url: '/api/app/blog/tags',
|
2025-06-19 21:42:16 +00:00
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Category methods
|
|
|
|
|
async getCategory(id: string): Promise<BlogCategory> {
|
|
|
|
|
const response = await apiService.fetchData<BlogCategory>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/category`,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createCategory(data: CreateUpdateBlogCategoryDto): Promise<BlogCategory> {
|
|
|
|
|
const response = await apiService.fetchData<BlogCategory>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: '/api/app/blog/category',
|
2025-06-19 14:51:10 +00:00
|
|
|
method: 'POST',
|
2025-06-19 21:42:16 +00:00
|
|
|
data: data as any,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateCategory(id: string, data: CreateUpdateBlogCategoryDto): Promise<BlogCategory> {
|
|
|
|
|
const response = await apiService.fetchData<BlogCategory>({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/category`,
|
2025-06-19 14:51:10 +00:00
|
|
|
method: 'PUT',
|
2025-06-19 21:42:16 +00:00
|
|
|
data: data as any,
|
|
|
|
|
})
|
|
|
|
|
return response.data
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteCategory(id: string): Promise<void> {
|
|
|
|
|
await apiService.fetchData({
|
2025-06-19 21:42:16 +00:00
|
|
|
url: `/api/app/blog/${id}/category`,
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
})
|
2025-06-19 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 21:42:16 +00:00
|
|
|
export const blogService = new BlogService()
|